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
4
7
8#include <time_shield/time_formatting.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 = static_cast<float>(value);
131 } else if (std::is_same<T, double>::value) {
133 pod_value.double_value = static_cast<double>(value);
134 } else if (std::is_same<T, long double>::value) {
136 pod_value.long_double_value = static_cast<long double>(value);
137 }
138 }
139
140#ifdef _MSC_VER
141# pragma warning(push)
142# pragma warning(disable : 4127)
143#endif
144
145 template <typename T>
146 VariableValue(const std::string& name, T value,
147 typename std::enable_if<
148 std::is_integral<T>::value && !std::is_same<T, bool>::value
149 >::type* = nullptr)
151
152 if (std::is_signed<T>::value) {
153 if (sizeof(T) <= sizeof(int8_t)) {
155 pod_value.int8_value = static_cast<int8_t>(value);
156 } else if (sizeof(T) <= sizeof(int16_t)) {
158 pod_value.int16_value = static_cast<int16_t>(value);
159 } else if (sizeof(T) <= sizeof(int32_t)) {
161 pod_value.int32_value = static_cast<int32_t>(value);
162 } else {
164 pod_value.int64_value = static_cast<int64_t>(value);
165 }
166 } else {
167 if (sizeof(T) <= sizeof(uint8_t)) {
169 pod_value.uint8_value = static_cast<uint8_t>(value);
170 } else if (sizeof(T) <= sizeof(uint16_t)) {
172 pod_value.uint16_value = static_cast<uint16_t>(value);
173 } else if (sizeof(T) <= sizeof(uint32_t)) {
175 pod_value.uint32_value = static_cast<uint32_t>(value);
176 } else {
178 pod_value.uint64_value = static_cast<uint64_t>(value);
179 }
180 }
181 }
182
183#ifdef _MSC_VER
184# pragma warning(pop)
185#endif
186
191 template <typename EnumType>
192 VariableValue(const std::string& name, EnumType value,
193 typename std::enable_if<std::is_enum<EnumType>::value>::type* = 0)
196 }
197
198 template <typename Rep, typename Period>
199 VariableValue(const std::string& name, const std::chrono::duration<Rep, Period>& duration)
201 string_value = std::to_string(duration.count()) + " " + duration_units<Period>();
202 }
203
204 template <typename Clock, typename Duration>
205 VariableValue(const std::string& name, const std::chrono::time_point<Clock, Duration>& time_point)
207 auto ts_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch());
208 //string_value = time_shield::to_human_readable_ms(ts_ms.count());
209 }
210
211# if __cplusplus >= 201703L
212
213 explicit VariableValue(const std::string& name, const std::filesystem::path& path)
215 }
216
217 template <typename... Ts>
218 explicit VariableValue(const std::string& name, const std::variant<Ts...>& variant)
220 string_value = std::visit([](const auto& value) -> std::string {
221 if constexpr (std::is_arithmetic_v<decltype(value)>) {
222 return std::to_string(value);
223 } else if constexpr (std::is_same_v<decltype(value), std::string>) {
224 return value;
225 } else {
226 std::ostringstream oss;
227 oss << value;
228 return oss.str();
229 }
230 }, variant);
231 }
232
233 template <typename T>
234 explicit VariableValue(const std::string& name, const std::optional<T>& optional)
236 if (optional) {
237 if constexpr (std::is_arithmetic_v<T>) {
238 string_value = std::to_string(*optional);
239 } else if constexpr (std::is_same_v<T, std::string>) {
240 string_value = *optional;
241 } else {
242 std::ostringstream oss;
243 oss << *optional;
244 string_value = oss.str();
245 }
246 } else {
247 string_value = "nullopt";
248 }
249 }
250
251# endif
252
253 explicit VariableValue(const std::string& name, void* ptr)
255 std::ostringstream oss;
256 oss << ptr;
257 string_value = oss.str();
258 }
259
260 template <typename T>
261 explicit VariableValue(const std::string& name, const std::shared_ptr<T>& ptr)
263 std::ostringstream oss;
264 if (ptr) oss << "shared_ptr@" << ptr.get();
265 else oss << "nullptr";
266 string_value = oss.str();
267 }
268
269 template <typename T>
270 explicit VariableValue(const std::string& name, const std::unique_ptr<T>& ptr)
272 std::ostringstream oss;
273 if (ptr) oss << "unique_ptr@" << ptr.get();
274 else oss << "nullptr";
275 string_value = oss.str();
276 }
277
280 : name(other.name), is_literal(other.is_literal), type(other.type),
283 if (is_pod_type(type)) {
284 pod_value = other.pod_value;
285 }
286 }
287
290 if (this == &other) return *this; // Self-assignment check.
291
292 name = other.name;
293 is_literal = other.is_literal;
294 type = other.type;
297
298 if (is_pod_type(type)) {
299 pod_value = other.pod_value;
300 }
301
302 return *this;
303 }
304
306 ~VariableValue() = default;
307
310 std::string to_string() const {
311 switch (type) {
312 case ValueType::INT8_VAL: return std::to_string(pod_value.int8_value);
313 case ValueType::UINT8_VAL: return std::to_string(pod_value.uint8_value);
314 case ValueType::INT16_VAL: return std::to_string(pod_value.int16_value);
315 case ValueType::UINT16_VAL: return std::to_string(pod_value.uint16_value);
316 case ValueType::INT32_VAL: return std::to_string(pod_value.int32_value);
317 case ValueType::UINT32_VAL: return std::to_string(pod_value.uint32_value);
318 case ValueType::INT64_VAL: return std::to_string(pod_value.int64_value);
319 case ValueType::UINT64_VAL: return std::to_string(pod_value.uint64_value);
320 case ValueType::BOOL_VAL: return pod_value.bool_value ? "true" : "false";
321 case ValueType::CHAR_VAL: return std::string(1, pod_value.char_value);
322 case ValueType::FLOAT_VAL: return std::to_string(pod_value.float_value);
323 case ValueType::DOUBLE_VAL: return std::to_string(pod_value.double_value);
324 case ValueType::LONG_DOUBLE_VAL: return std::to_string(static_cast<double>(pod_value.long_double_value));
335 return string_value;
337 return error_code_value.message() + " (" + std::to_string(error_code_value.value()) + ")";
338 default: break;
339 }
340 return "unknown";
341 }
342
346 std::string to_string(const char* fmt) const {
347 switch (type) {
348 case ValueType::INT8_VAL: return logit::format(fmt, pod_value.int8_value);
349 case ValueType::UINT8_VAL: return format(fmt, pod_value.uint8_value);
350 case ValueType::INT16_VAL: return format(fmt, pod_value.int16_value);
351 case ValueType::UINT16_VAL: return format(fmt, pod_value.uint16_value);
352 case ValueType::INT32_VAL: return format(fmt, pod_value.int32_value);
353 case ValueType::UINT32_VAL: return format(fmt, pod_value.uint32_value);
354 case ValueType::INT64_VAL: return format(fmt, pod_value.int64_value);
355 case ValueType::UINT64_VAL: return format(fmt, pod_value.uint64_value);
356 case ValueType::BOOL_VAL: return format(fmt, pod_value.bool_value);
357 case ValueType::CHAR_VAL: return format(fmt, pod_value.char_value);
358 case ValueType::FLOAT_VAL: return format(fmt, pod_value.float_value);
359 case ValueType::DOUBLE_VAL: return format(fmt, pod_value.double_value);
360 case ValueType::LONG_DOUBLE_VAL: return format(fmt, static_cast<double>(pod_value.long_double_value));
371 return format(fmt, string_value.c_str());
373 return format(fmt, error_code_value.message().c_str(), error_code_value.value());
374 default: break;
375 }
376 return "unknown";
377 }
378
379 private:
383 static bool is_valid_literal_name(const std::string& name) {
384 if (name.empty()) return false;
385 return !std::isdigit(static_cast<unsigned char>(name[0]));
386 }
387
392 switch (type) {
406 return true;
407 default:
408 return false;
409 }
410 }
411
415 template <typename Period>
416 static std::string duration_units() {
417 if (std::is_same<Period, std::ratio<1>>::value) {
418 return "s"; // seconds
419 } else if (std::is_same<Period, std::milli>::value) {
420 return "ms"; // milliseconds
421 } else if (std::is_same<Period, std::micro>::value) {
422 return "us"; // microseconds
423 } else if (std::is_same<Period, std::nano>::value) {
424 return "ns"; // nanoseconds
425 } else if (std::is_same<Period, std::ratio<60>>::value) {
426 return "min"; // minutes
427 } else if (std::is_same<Period, std::ratio<3600>>::value) {
428 return "h"; // hours
429 } else {
430 return "custom"; // Custom units
431 }
432 }
433 }; // VariableValue
434
435} // namespace logit
436
437#endif // _LOGIT_VARIABLE_VALUE_HPP_INCLUDED
The primary namespace for the LogIt++ library.
std::string format(const char *fmt,...)
Formats a string according to the specified format.
Definition format.hpp:28
std::string enum_to_string(EnumType value)
Helper function to convert an enumeration to a string.
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.
union logit::VariableValue::@221262170071251241321162055167102053137316342217 pod_value
Union to store POD types.
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.
@ 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).
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_integral< T >::value &&!std::is_same< T, bool >::value >::type *=nullptr)
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)