LogIt++
Loading...
Searching...
No Matches
VariableValue.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _LOGIT_VARIABLE_VALUE_HPP_INCLUDED
3#define _LOGIT_VARIABLE_VALUE_HPP_INCLUDED
6
7#include "format.hpp"
8#include <time_shield_cpp/time_shield.hpp>
9#include <string>
10#include <iostream>
11#include <cstdint>
12#include <type_traits>
13#include <exception>
14#include <iomanip> // std::put_time
15#include <chrono>
16#include <sstream>
17#include <memory>
18#if __cplusplus >= 201703L
19#include <filesystem>
20#include <optional>
21#include <variant>
22#endif
23
24namespace logit {
25
30 template <typename EnumType>
31 std::string enum_to_string(EnumType value) {
32 // Convert enum to underlying integral value and then to string.
33 typedef typename std::underlying_type<EnumType>::type UnderlyingType;
34 return std::to_string(static_cast<UnderlyingType>(value));
35 }
36
40 std::string name;
42
72
73 union {
74 int8_t int8_value;
75 uint8_t uint8_value;
76 int16_t int16_value;
77 uint16_t uint16_value;
78 int32_t int32_value;
79 uint32_t uint32_value;
80 int64_t int64_value;
81 uint64_t uint64_value;
86 long double long_double_value;
88
89 std::string string_value;
90 std::error_code error_code_value;
91
92 // Constructors for each type.
93 template <typename T>
94 VariableValue(const std::string& name, T value,
95 typename std::enable_if<std::is_same<T, bool>::value>::type* = nullptr)
97 pod_value.bool_value = value;
98 }
99
100 template <typename T>
101 VariableValue(const std::string& name, T value,
102 typename std::enable_if<std::is_same<T, char>::value>::type* = nullptr)
104 pod_value.char_value = value;
105 }
106
107 explicit VariableValue(const std::string& name, const std::string& value)
109 }
110
111 explicit VariableValue(const std::string& name, const char* value) :
112 VariableValue(name, std::string(value)) {}
113
114 template <typename T>
115 VariableValue(const std::string& name, const T& value,
116 typename std::enable_if<std::is_base_of<std::exception, T>::value>::type* = nullptr)
118 }
119
120 explicit VariableValue(const std::string& name, const std::error_code& ec)
122 }
123
124 template <typename T>
125 VariableValue(const std::string& name, T value,
126 typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr)
128 if (std::is_same<T, float>::value) {
130 pod_value.float_value = value;
131 } else if (std::is_same<T, double>::value) {
133 pod_value.double_value = value;
134 } else if (std::is_same<T, long double>::value) {
136 pod_value.long_double_value = value;
137 }
138 }
139
140 template <typename T>
141 VariableValue(const std::string& name, T value,
142 typename std::enable_if<std::is_integral<T>::value>::type* = nullptr)
144 if (std::is_signed<T>::value) {
145 if (sizeof(T) <= sizeof(int8_t)) {
147 pod_value.int8_value = static_cast<int8_t>(value);
148 } else if (sizeof(T) <= sizeof(int16_t)) {
150 pod_value.int16_value = static_cast<int16_t>(value);
151 } else if (sizeof(T) <= sizeof(int32_t)) {
153 pod_value.int32_value = static_cast<int32_t>(value);
154 } else {
156 pod_value.int64_value = static_cast<int64_t>(value);
157 }
158 } else {
159 if (sizeof(T) <= sizeof(uint8_t)) {
161 pod_value.uint8_value = static_cast<uint8_t>(value);
162 } else if (sizeof(T) <= sizeof(uint16_t)) {
164 pod_value.uint16_value = static_cast<uint16_t>(value);
165 } else if (sizeof(T) <= sizeof(uint32_t)) {
167 pod_value.uint32_value = static_cast<uint32_t>(value);
168 } else {
170 pod_value.uint64_value = static_cast<uint64_t>(value);
171 }
172 }
173 }
174
179 template <typename EnumType>
180 VariableValue(const std::string& name, EnumType value,
181 typename std::enable_if<std::is_enum<EnumType>::value>::type* = 0)
184 }
185
186 template <typename Rep, typename Period>
187 VariableValue(const std::string& name, const std::chrono::duration<Rep, Period>& duration)
189 string_value = std::to_string(duration.count()) + " " + duration_units<Period>();
190 }
191
192 template <typename Clock, typename Duration>
193 VariableValue(const std::string& name, const std::chrono::time_point<Clock, Duration>& time_point)
195 auto ts_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch());
196 string_value = time_shield::to_human_readable_ms(ts_ms.count());
197 }
198
199# if __cplusplus >= 201703L
200
201 explicit VariableValue(const std::string& name, const std::filesystem::path& path)
203 }
204
205 template <typename... Ts>
206 explicit VariableValue(const std::string& name, const std::variant<Ts...>& variant)
208 string_value = std::visit([](const auto& value) -> std::string {
209 if constexpr (std::is_arithmetic_v<decltype(value)>) {
210 return std::to_string(value);
211 } else if constexpr (std::is_same_v<decltype(value), std::string>) {
212 return value;
213 } else {
214 std::ostringstream oss;
215 oss << value;
216 return oss.str();
217 }
218 }, variant);
219 }
220
221 template <typename T>
222 explicit VariableValue(const std::string& name, const std::optional<T>& optional)
224 if (optional) {
225 if constexpr (std::is_arithmetic_v<T>) {
226 string_value = std::to_string(*optional);
227 } else if constexpr (std::is_same_v<T, std::string>) {
228 string_value = *optional;
229 } else {
230 std::ostringstream oss;
231 oss << *optional;
232 string_value = oss.str();
233 }
234 } else {
235 string_value = "nullopt";
236 }
237 }
238
239# endif
240
241 explicit VariableValue(const std::string& name, void* ptr)
243 std::ostringstream oss;
244 oss << ptr;
245 string_value = oss.str();
246 }
247
248 template <typename T>
249 explicit VariableValue(const std::string& name, const std::shared_ptr<T>& ptr)
251 std::ostringstream oss;
252 if (ptr) oss << "shared_ptr@" << ptr.get();
253 else oss << "nullptr";
254 string_value = oss.str();
255 }
256
257 template <typename T>
258 explicit VariableValue(const std::string& name, const std::unique_ptr<T>& ptr)
260 std::ostringstream oss;
261 if (ptr) oss << "unique_ptr@" << ptr.get();
262 else oss << "nullptr";
263 string_value = oss.str();
264 }
265
268 : name(other.name), is_literal(other.is_literal), type(other.type),
271 if (is_pod_type(type)) {
272 pod_value = other.pod_value;
273 }
274 }
275
278 if (this == &other) return *this; // Self-assignment check.
279
280 name = other.name;
281 is_literal = other.is_literal;
282 type = other.type;
285
286 if (is_pod_type(type)) {
287 pod_value = other.pod_value;
288 }
289
290 return *this;
291 }
292
294 ~VariableValue() = default;
295
298 std::string to_string() const {
299 switch (type) {
300 case ValueType::INT8_VAL: return std::to_string(pod_value.int8_value);
301 case ValueType::UINT8_VAL: return std::to_string(pod_value.uint8_value);
302 case ValueType::INT16_VAL: return std::to_string(pod_value.int16_value);
303 case ValueType::UINT16_VAL: return std::to_string(pod_value.uint16_value);
304 case ValueType::INT32_VAL: return std::to_string(pod_value.int32_value);
305 case ValueType::UINT32_VAL: return std::to_string(pod_value.uint32_value);
306 case ValueType::INT64_VAL: return std::to_string(pod_value.int64_value);
307 case ValueType::UINT64_VAL: return std::to_string(pod_value.uint64_value);
308 case ValueType::BOOL_VAL: return pod_value.bool_value ? "true" : "false";
309 case ValueType::CHAR_VAL: return std::string(1, pod_value.char_value);
310 case ValueType::FLOAT_VAL: return std::to_string(pod_value.float_value);
311 case ValueType::DOUBLE_VAL: return std::to_string(pod_value.double_value);
312 case ValueType::LONG_DOUBLE_VAL: return std::to_string(static_cast<double>(pod_value.long_double_value));
323 return string_value;
325 return error_code_value.message() + " (" + std::to_string(error_code_value.value()) + ")";
326 default: break;
327 }
328 return "unknown";
329 }
330
334 std::string to_string(const char* fmt) const {
335 switch (type) {
336 case ValueType::INT8_VAL: return format(fmt, pod_value.int8_value);
337 case ValueType::UINT8_VAL: return format(fmt, pod_value.uint8_value);
338 case ValueType::INT16_VAL: return format(fmt, pod_value.int16_value);
339 case ValueType::UINT16_VAL: return format(fmt, pod_value.uint16_value);
340 case ValueType::INT32_VAL: return format(fmt, pod_value.int32_value);
341 case ValueType::UINT32_VAL: return format(fmt, pod_value.uint32_value);
342 case ValueType::INT64_VAL: return format(fmt, pod_value.int64_value);
343 case ValueType::UINT64_VAL: return format(fmt, pod_value.uint64_value);
344 case ValueType::BOOL_VAL: return format(fmt, pod_value.bool_value);
345 case ValueType::CHAR_VAL: return format(fmt, pod_value.char_value);
346 case ValueType::FLOAT_VAL: return format(fmt, pod_value.float_value);
347 case ValueType::DOUBLE_VAL: return format(fmt, pod_value.double_value);
348 case ValueType::LONG_DOUBLE_VAL: return format(fmt, static_cast<double>(pod_value.long_double_value));
359 return format(fmt, string_value.c_str());
361 return format(fmt, error_code_value.message().c_str(), error_code_value.value());
362 default: break;
363 }
364 return "unknown";
365 }
366
367 private:
371 static bool is_valid_literal_name(const std::string& name) {
372 if (name.empty()) return false;
373 return !std::isdigit(static_cast<unsigned char>(name[0]));
374 }
375
380 switch (type) {
394 return true;
395 default:
396 return false;
397 }
398 }
399
403 template <typename Period>
404 static std::string duration_units() {
405 if (std::is_same<Period, std::ratio<1>>::value) {
406 return "s"; // seconds
407 } else if (std::is_same<Period, std::milli>::value) {
408 return "ms"; // milliseconds
409 } else if (std::is_same<Period, std::micro>::value) {
410 return "us"; // microseconds
411 } else if (std::is_same<Period, std::nano>::value) {
412 return "ns"; // nanoseconds
413 } else if (std::is_same<Period, std::ratio<60>>::value) {
414 return "min"; // minutes
415 } else if (std::is_same<Period, std::ratio<3600>>::value) {
416 return "h"; // hours
417 } else {
418 return "custom"; // Custom units
419 }
420 }
421 }; // VariableValue
422
423} // namespace logit
424
425#endif // _LOGIT_VARIABLE_VALUE_HPP_INCLUDED
Function for formatting strings according to a specified format.
The primary namespace for the LogIt++ library.
std::string format(const char *fmt,...)
Formats a string according to the specified format.
Definition format.hpp:27
std::string enum_to_string(EnumType value)
Helper function to convert an enumeration to a string.
Structure for storing values of various types, including enumerations.
VariableValue(const std::string &name, T value, typename std::enable_if< std::is_integral< T >::value >::type *=nullptr)
VariableValue(const std::string &name, const std::chrono::duration< Rep, Period > &duration)
long double long_double_value
std::error_code error_code_value
Variable to store std::error_code.
std::string name
Variable name.
VariableValue & operator=(const VariableValue &other)
Assignment operator.
VariableValue(const std::string &name, const T &value, typename std::enable_if< std::is_base_of< std::exception, T >::value >::type *=nullptr)
static std::string duration_units()
Helper function to get the unit of the duration.
VariableValue(const std::string &name, const std::chrono::time_point< Clock, Duration > &time_point)
VariableValue(const std::string &name, T value, typename std::enable_if< std::is_same< T, bool >::value >::type *=nullptr)
~VariableValue()=default
Destructor.
bool is_literal
Flag indicating if the variable is a literal.
std::string to_string() const
Method to get the value as a string.
enum logit::VariableValue::ValueType type
Specifies the type of the stored value in the VariableValue structure.
std::string string_value
Variable to store string, exception messages, and enums.
VariableValue(const std::string &name, const std::unique_ptr< T > &ptr)
VariableValue(const std::string &name, const std::string &value)
VariableValue(const VariableValue &other)
Copy constructor.
VariableValue(const std::string &name, const std::shared_ptr< T > &ptr)
ValueType
Enumeration of possible value types for VariableValue.
@ BOOL_VAL
Value of type bool.
@ FLOAT_VAL
Value of type float (single-precision floating point).
@ ENUM_VAL
Value of any enumeration type (converted to string or integral value).
@ INT16_VAL
Value of type int16_t (signed 16-bit integer).
@ CHAR_VAL
Value of type char (single character).
@ OPTIONAL_VAL
Value of type std::optional (optional value holder).
@ STRING_VAL
Value of type std::string (dynamic-length string).
@ POINTER_VAL
Value of type void* (raw pointer).
@ UINT64_VAL
Value of type uint64_t (unsigned 64-bit integer).
@ UINT32_VAL
Value of type uint32_t (unsigned 32-bit integer).
@ VARIANT_VAL
Value of type std::variant (type-safe union).
@ SMART_POINTER_VAL
Value of type std::shared_ptr or std::unique_ptr (smart pointers).
@ INT64_VAL
Value of type int64_t (signed 64-bit integer).
@ PATH_VAL
Value of type std::filesystem::path (filesystem path).
@ DURATION_VAL
Value of type std::chrono::duration (time duration).
@ DOUBLE_VAL
Value of type double (double-precision floating point).
@ INT32_VAL
Value of type int32_t (signed 32-bit integer).
@ UINT8_VAL
Value of type uint8_t (unsigned 8-bit integer).
@ LONG_DOUBLE_VAL
Value of type long double (extended-precision floating point).
@ TIME_POINT_VAL
Value of type std::chrono::time_point (specific point in time).
@ INT8_VAL
Value of type int8_t (signed 8-bit integer).
@ UINT16_VAL
Value of type uint16_t (unsigned 16-bit integer).
@ UNKNOWN_VAL
Unknown or unsupported value type.
@ EXCEPTION_VAL
Value representing an exception (derived from std::exception).
@ ERROR_CODE_VAL
Value of type std::error_code (system error code).
union logit::VariableValue::@0 pod_value
Union to store POD types.
VariableValue(const std::string &name, EnumType value, typename std::enable_if< std::is_enum< EnumType >::value >::type *=0)
Constructor for enumerations.
static bool is_valid_literal_name(const std::string &name)
Helper function to check if a name is a valid literal.
VariableValue(const std::string &name, const std::error_code &ec)
static bool is_pod_type(ValueType type)
Helper function to determine if a ValueType represents a POD type.
VariableValue(const std::string &name, T value, typename std::enable_if< std::is_same< T, char >::value >::type *=nullptr)
std::string to_string(const char *fmt) const
Method to get the value as a formatted string.
VariableValue(const std::string &name, T value, typename std::enable_if< std::is_floating_point< T >::value >::type *=nullptr)
VariableValue(const std::string &name, const char *value)
VariableValue(const std::string &name, void *ptr)