IRSOL
C++ code implementing socket server for interacting with Baumer camera.
irsol::protocol::utils Namespace Reference

Utility functions for string manipulation and validation. More...

Functions

std::string validateIdentifier (const std::string &identifier)
 Validate that a string is a valid protocol identifier.
 
template<typename T >
fromString (const std::string &str)
 Convert a string to a value of type T.
 
std::string trim (const std::string &s)
 Remove leading and trailing whitespace from a string.
 

Detailed Description

Utility functions for string manipulation and validation.

Provides low-level helpers for validating protocol identifiers, converting strings to typed values, and trimming whitespace. These functions underpin the protocol parsing logic and ensure consistent input validation and error handling.

Function Documentation

◆ fromString()

template<typename T >
T irsol::protocol::utils::fromString ( const std::string &  str)

Convert a string to a value of type T.

Parses the input string str and converts it to the requested type T. Supported types:

  • Integral types (e.g., int, long)
  • Floating point types (e.g., double, float)
  • std::string (returns the string as-is)

Throws std::invalid_argument if the string contains extraneous characters after the parsed value or if conversion fails.

Template Parameters
TThe desired output type.
Parameters
strThe string to convert.
Returns
The parsed value of type T.
Exceptions
std::invalid_argumenton parsing errors.
Note
Requires explicit template specializations for unsupported types.

Definition at line 87 of file utils.hpp.

88{
89 std::size_t pos = 0;
90
91 if constexpr(std::is_integral_v<T>) {
92 IRSOL_LOG_TRACE("Converting string '{}' to integer", str);
93 int res = std::stoi(str, &pos);
94 if(pos != str.length())
95 throw std::invalid_argument("Extra characters after integer");
96 return res;
97 } else if constexpr(std::is_floating_point_v<T>) {
98 IRSOL_LOG_TRACE("Converting string '{}' to double", str);
99 double res = std::stod(str, &pos);
100 if(pos != str.length())
101 throw std::invalid_argument("Extra characters after double");
102 return res;
103 } else if constexpr(std::is_same_v<T, std::string>) {
104 return str;
105 } else {
106 IRSOL_MISSING_TEMPLATE_SPECIALIZATION(T, "utils::fromString()");
107 }
108}
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90
#define IRSOL_MISSING_TEMPLATE_SPECIALIZATION(T, funcNameLiteral)
Emits a compile-time error when no template specialization is available.
Definition macros.hpp:173

◆ trim()

std::string irsol::protocol::utils::trim ( const std::string &  s)
inline

Remove leading and trailing whitespace from a string.

Trims spaces, tabs, carriage returns, and newline characters from both ends of the input string s.

Parameters
sThe input string to trim.
Returns
A copy of s with leading and trailing whitespace removed.
Note
Does not modify the original string.

Definition at line 122 of file utils.hpp.

123{
124 size_t start = s.find_first_not_of(" \t\r\n");
125 size_t end = s.find_last_not_of(" \t\r\n");
126 return (start == std::string::npos) ? "" : s.substr(start, end - start + 1);
127}

◆ validateIdentifier()

std::string irsol::protocol::utils::validateIdentifier ( const std::string &  identifier)
inline

Validate that a string is a valid protocol identifier.

Checks that the given identifier string meets the following rules:

  • Starts with an alphabetic character (a-z, A-Z) or underscore (_).
  • Contains only alphanumeric characters, underscores (_), and optionally array indexing expressions of the form [number] (e.g., param[0]).

If validation fails, logs an error and throws std::invalid_argument.

Parameters
identifierThe string to validate as an identifier.
Returns
The validated identifier string.
Exceptions
std::invalid_argumentif the string is not a valid identifier.
Note
This function is intended for validating parameter or field names in protocol messages.

Definition at line 55 of file utils.hpp.

56{
57 static const std::regex re(R"(^[a-zA-Z_][a-zA-Z0-9_]*(?:\[\d+\])*$)");
58 std::smatch m;
59 if(!std::regex_match(identifier, m, re)) {
60 IRSOL_LOG_ERROR("Invalid identifier '{}'", identifier);
61 throw std::invalid_argument("Invalid identifier");
62 }
63 return identifier;
64}
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94
irsol::protocol::Assignment m(identifier, value)