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

Contains general utility functions used throughout the irsol library. More...

Namespaces

namespace  internal
 Contains internal helper constants and functions.
 

Classes

class  SafeQueue
 A thread-safe, optionally bounded queue with blocking push and pop operations. More...
 

Functions

int toInt (const irsol::types::protocol_value_t &x)
 Converts a protocol value to an integer.
 
double toDouble (const irsol::types::protocol_value_t &x)
 Converts a protocol value to a double.
 
std::string toString (const irsol::types::protocol_value_t &x)
 Converts a protocol value to a string.
 
std::string uuid ()
 Generates a new UUID string.
 
std::vector< std::string > split (const std::string &s, char delimiter)
 Splits a string into tokens based on a delimiter.
 
std::string strip (const std::string &s, const std::string &delimiters=" \t\r\n")
 Removes leading and trailing characters from a string.
 
std::string stripString (const std::string &s, const std::string &strippedString)
 Removes all occurrences of a specific substring from the start and end of a string.
 
std::string timestampToString (irsol::types::timepoint_t tp)
 Converts a steady_clock time point to a human-readable string.
 
std::string durationToString (irsol::types::duration_t dr)
 Converts a duration to a human-readable string.
 
std::vector< irsol::types::byte_tstringToBytes (const std::string &s)
 Converts a std::string to a std::vector of irsol::types::byte_t.
 
std::string bytesToString (const std::vector< irsol::types::byte_t > &input)
 Converts a std::vector of irsol::types::byte_t to a std::string.
 
NeoAPI::Cam loadDefaultCamera ()
 Loads the default camera device.
 
NeoAPI::CamInfoList & discoverCameras ()
 Discovers all cameras connected to the system.
 

Detailed Description

Contains general utility functions used throughout the irsol library.

This namespace provides helper functions for type conversions, string manipulations, timestamp formatting, and camera device management.

Function Documentation

◆ bytesToString()

std::string irsol::utils::bytesToString ( const std::vector< irsol::types::byte_t > &  input)

Converts a std::vector of irsol::types::byte_t to a std::string.

Each byte is reinterpreted as a character. This assumes that the bytes represent valid character data (e.g., UTF-8 or ASCII).

Parameters
inputThe input irsol::types::byte_t vector to convert.
Returns
std::string A string constructed from the byte-wise content.

Definition at line 189 of file utils.cpp.

190{
191 const auto* data = reinterpret_cast<const char*>(input.data());
192 return std::string(data, data + input.size());
193}
std::string input

◆ discoverCameras()

NeoAPI::CamInfoList & irsol::utils::discoverCameras ( )

Discovers all cameras connected to the system.

Uses NeoAPI to scan and retrieve information about all connected cameras. The returned reference remains valid until the next call to discoverCameras().

Returns
A reference to a list of all discovered cameras.

Definition at line 216 of file utils.cpp.

217{
218 IRSOL_LOG_TRACE("Discovering cameras");
219 NeoAPI::CamInfoList& infoList = NeoAPI::CamInfoList::Get();
220 IRSOL_LOG_TRACE("Refreshing camera list");
221 infoList.Refresh();
222
223 return infoList;
224}
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90

◆ durationToString()

std::string irsol::utils::durationToString ( irsol::types::duration_t  dr)

Converts a duration to a human-readable string.

Converts a irsol::types::duration_t into a formatted string representing the duration.

Parameters
drThe duration to convert.
Returns
A human-readable string representation of the duration.

Definition at line 133 of file utils.cpp.

134{
135 std::stringstream ss;
136 if(dr.count() == 0) {
137 return "0ns";
138 }
139
140 if(dr >= std::chrono::hours(1)) {
141 auto hours = std::chrono::duration_cast<std::chrono::hours>(dr);
142 ss << std::to_string(hours.count());
143 ss << "hours ";
144 dr -= hours;
145 }
146 if(dr >= std::chrono::minutes(1)) {
147 auto minutes = std::chrono::duration_cast<std::chrono::minutes>(dr);
148 ss << std::setw(2) << std::setfill('0') << std::to_string(minutes.count());
149 ss << "minutes ";
150 dr -= minutes;
151 }
152 if(dr >= std::chrono::seconds(1)) {
153 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(dr);
154 ss << std::setw(2) << std::setfill('0') << std::to_string(seconds.count());
155 ss << "s ";
156 dr -= seconds;
157 }
158 if(dr >= std::chrono::milliseconds(1)) {
159 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(dr);
160 ss << std::setw(3) << std::setfill('0') << std::to_string(milliseconds.count());
161 ss << "ms ";
162 dr -= milliseconds;
163 }
164 if(dr >= std::chrono::microseconds(1)) {
165 auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(dr);
166 ss << std::setw(3) << std::setfill('0') << std::to_string(microseconds.count());
167 ss << "us ";
168 dr -= microseconds;
169 }
170 if(dr >= std::chrono::nanoseconds(1)) {
171 auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(dr);
172 ss << std::setw(3) << std::setfill('0') << std::to_string(nanoseconds.count());
173 ss << "ns ";
174 dr -= nanoseconds;
175 }
176
177 auto result = ss.str();
178 return strip(result, " 0");
179}
std::string strip(const std::string &s, const std::string &delimiters=" \t\r\n")
Removes leading and trailing characters from a string.
Definition utils.cpp:71
auto result

◆ loadDefaultCamera()

NeoAPI::Cam irsol::utils::loadDefaultCamera ( )

Loads the default camera device.

Queries the NeoAPI for connected cameras and attempts to open the camera with the default serial number defined in irsol::utils::internal::defaultCameraSerialNumber().

Exceptions
std::runtime_errorIf no matching camera is found or initialization fails.
Returns
A handle to the opened NeoAPI camera device.

Definition at line 196 of file utils.cpp.

197{
198 IRSOL_LOG_DEBUG("Loading default camera");
199 NeoAPI::Cam cam = NeoAPI::Cam();
200
201 const auto cameraSerialNumber = internal::defaultCameraSerialNumber();
202 IRSOL_LOG_TRACE("Trying to connect to default camera with SN '{0:s}'.", cameraSerialNumber);
203 try {
204 cam.Connect(cameraSerialNumber);
205 } catch(NeoAPI::NotConnectedException& e) {
206 IRSOL_ASSERT_FATAL(false, "Camera connection failed: %s", e.GetDescription());
207 throw e;
208 }
209 IRSOL_ASSERT_FATAL(cam.IsConnected(), "Camera is not connected");
210 IRSOL_LOG_DEBUG("Camera connection successful");
211
212 return cam;
213}
#define IRSOL_ASSERT_FATAL
Fatal-level assertion macro.
Definition assert.hpp:135
#define IRSOL_LOG_DEBUG(...)
Logs a debug-level message using the default logger.
Definition logging.hpp:91

◆ split()

std::vector< std::string > irsol::utils::split ( const std::string &  s,
char  delimiter 
)

Splits a string into tokens based on a delimiter.

Parses the input string s and splits it into substrings each time the character delimiter is encountered.

Parameters
sThe string to split.
delimiterThe character to use as the delimiter.
Returns
A vector of token substrings.

Definition at line 50 of file utils.cpp.

51{
52 std::vector<std::string> tokens;
53 std::string token;
54 for(char c : s) {
55 if(c == delimiter) {
56 if(!token.empty()) {
57 tokens.push_back(token);
58 token.clear();
59 }
60 } else {
61 token += c;
62 }
63 }
64 if(!token.empty()) {
65 tokens.push_back(token);
66 }
67 return tokens;
68}

◆ stringToBytes()

std::vector< irsol::types::byte_t > irsol::utils::stringToBytes ( const std::string &  s)

Converts a std::string to a std::vector of irsol::types::byte_t.

Each character in the input string is reinterpreted as a irsol::types::byte_t.

Parameters
sThe input string to convert.
Returns
A vector containing the byte-wise representation of the string.

Definition at line 182 of file utils.cpp.

183{
184 const auto* data = reinterpret_cast<const irsol::types::byte_t*>(s.data());
185 return {data, data + s.size()};
186}
std::byte byte_t
Alias for a single byte used in serialization or binary data handling.
Definition types.hpp:165

◆ strip()

std::string irsol::utils::strip ( const std::string &  s,
const std::string &  delimiters = " \t\r\n" 
)

Removes leading and trailing characters from a string.

Removes any characters contained in the delimiters string from both the start and the end of the input string s. Useful for trimming whitespace or custom characters.

Parameters
sThe input string to process.
delimitersA string containing all characters to strip (default is whitespace characters).
Returns
A new string with specified characters removed from both ends.

Definition at line 71 of file utils.cpp.

72{
73 size_t start = 0;
74 size_t end = s.size();
75 IRSOL_LOG_TRACE("Stripping delimiters '{0:s}' from string '{1:s}'", delimiters, s);
76 while((start < end) && (delimiters.find(s[start]) != std::string::npos)) {
77 start++;
78 }
79 while((start < end) && (delimiters.find(s[end - 1]) != std::string::npos)) {
80 end--;
81 }
82
83 auto result = s.substr(start, end - start);
84 IRSOL_LOG_TRACE("Stripped string '{0:s}' is '{1:s}'", s, result);
85 return result;
86}

◆ stripString()

std::string irsol::utils::stripString ( const std::string &  s,
const std::string &  strippedString 
)

Removes all occurrences of a specific substring from the start and end of a string.

If the string s begins or ends with the substring strippedString, those occurrences are removed. This is repeated until s no longer starts or ends with that substring.

Parameters
sThe original string.
strippedStringThe substring to remove from both ends.
Returns
A new string with the specified substring removed from start and end.

Definition at line 89 of file utils.cpp.

90{
91 // Check if the stripped string is present in the input string at the beginning
92 std::string result{s};
93 IRSOL_LOG_TRACE("Stripping string '{0:s}' from string '{1:s}'", strippedString, s);
94
95 if(result.find(strippedString) == 0) {
96 result = result.substr(strippedString.length());
97 }
98
99 // Check if the stripped string is present in the input string at the end
100 if(result.rfind(strippedString) == result.size() - strippedString.size()) {
101 result = result.substr(0, result.size() - strippedString.size());
102 }
103
105 "Stripped string '{0:s}' from string '{1:s}' is '{2:s}'", s, strippedString, result);
106
107 return result;
108}

◆ timestampToString()

std::string irsol::utils::timestampToString ( irsol::types::timepoint_t  tp)

Converts a steady_clock time point to a human-readable string.

Converts a irsol::types::timepoint_t into a formatted string representation.

Parameters
tpThe time point to convert.
Returns
A human-readable string representation of the time point.

Definition at line 111 of file utils.cpp.

112{
113 using system_clock_t = std::chrono::system_clock;
114 auto now_sys = std::chrono::time_point_cast<system_clock_t::duration>(
115 tp - irsol::types::clock_t::now() + system_clock_t::now());
116
117 // Extract microseconds from time since epoch
118 auto us = std::chrono::duration_cast<std::chrono::microseconds>(now_sys.time_since_epoch()) %
119 std::chrono::seconds(1);
120
121 // Convert to time_t for formatting
122 std::time_t t_c = system_clock_t::to_time_t(now_sys);
123 std::tm tm = *std::localtime(&t_c);
124
125 std::stringstream ss;
126 ss << std::put_time(&tm, "%F %T"); // Date and time to seconds
127 ss << '.' << std::setfill('0') << std::setw(6) << us.count(); // Append microseconds
128
129 return ss.str();
130}

◆ toDouble()

double irsol::utils::toDouble ( const irsol::types::protocol_value_t x)
inline

Converts a protocol value to a double.

Extracts and returns the underlying double from the variant type irsol::types::protocol_value_t.

Parameters
xThe protocol value to convert.
Returns
The double representation of the input value.
Exceptions
std::bad_variant_accessif the variant does not hold a double.

Definition at line 55 of file utils.hpp.

56{
57 return std::get<double>(x);
58}

◆ toInt()

int irsol::utils::toInt ( const irsol::types::protocol_value_t x)
inline

Converts a protocol value to an integer.

Extracts and returns the underlying int from the variant type irsol::types::protocol_value_t.

Parameters
xThe protocol value to convert.
Returns
The integer representation of the input value.
Exceptions
std::bad_variant_accessif the variant does not hold an int.

Definition at line 39 of file utils.hpp.

40{
41 return std::get<int>(x);
42}

◆ toString()

std::string irsol::utils::toString ( const irsol::types::protocol_value_t x)
inline

Converts a protocol value to a string.

Extracts and returns the underlying std::string from the variant type irsol::types::protocol_value_t.

Parameters
xThe protocol value to convert.
Returns
The string representation of the input value.
Exceptions
std::bad_variant_accessif the variant does not hold a string.

Definition at line 71 of file utils.hpp.

72{
73 return std::get<std::string>(x);
74}

◆ uuid()

std::string irsol::utils::uuid ( )

Generates a new UUID string.

Generates a version 4 (random) UUID and returns it as a string.

Returns
A newly generated UUID string, e.g., "123e4567-e89b-12d3-a456-426614174000".

Definition at line 16 of file utils.cpp.

17{
18 static std::random_device rd;
19 static std::mt19937 gen(rd());
20 std::uniform_int_distribution<> dis(0, 15);
21 std::uniform_int_distribution<> dis2(8, 11);
22
23 std::stringstream ss;
24 ss << std::hex;
25
26 for(int i = 0; i < 8; i++) {
27 ss << dis(gen);
28 }
29 ss << "-";
30 for(int i = 0; i < 4; i++) {
31 ss << dis(gen);
32 }
33 ss << "-4"; // Version 4
34 for(int i = 0; i < 3; i++) {
35 ss << dis(gen);
36 }
37 ss << "-";
38 ss << dis2(gen);
39 for(int i = 0; i < 3; i++) {
40 ss << dis(gen);
41 }
42 ss << "-";
43 for(int i = 0; i < 12; i++) {
44 ss << dis(gen);
45 }
46 return ss.str();
47}