2#ifndef _LOGIT_VARIABLE_VALUE_HPP_INCLUDED
3#define _LOGIT_VARIABLE_VALUE_HPP_INCLUDED
8#include <time_shield/time_formatting.hpp>
18#if __cplusplus >= 201703L
30 template <
typename EnumType>
33 typedef typename std::underlying_type<EnumType>::type UnderlyingType;
34 return std::to_string(
static_cast<UnderlyingType
>(value));
95 typename std::enable_if<std::is_same<T, bool>::value>
::type* =
nullptr)
100 template <
typename T>
102 typename std::enable_if<std::is_same<T, char>::value>
::type* =
nullptr)
114 template <
typename T>
116 typename std::enable_if<std::is_base_of<std::exception, T>::value>
::type* =
nullptr)
124 template <
typename T>
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);
141# pragma warning(push)
142# pragma warning(disable : 4127)
145 template <
typename T>
147 typename std::enable_if<
148 std::is_integral<T>::value && !std::is_same<T, bool>::value
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);
164 pod_value.int64_value =
static_cast<int64_t
>(value);
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);
178 pod_value.uint64_value =
static_cast<uint64_t
>(value);
191 template <
typename EnumType>
193 typename std::enable_if<std::is_enum<EnumType>::value>
::type* = 0)
198 template <
typename Rep,
typename Period>
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());
211# if __cplusplus >= 201703L
213 explicit VariableValue(
const std::string&
name,
const std::filesystem::path& path)
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>) {
226 std::ostringstream oss;
233 template <
typename T>
234 explicit VariableValue(
const std::string&
name,
const std::optional<T>& optional)
237 if constexpr (std::is_arithmetic_v<T>) {
239 }
else if constexpr (std::is_same_v<T, std::string>) {
242 std::ostringstream oss;
255 std::ostringstream oss;
260 template <
typename T>
263 std::ostringstream oss;
264 if (ptr) oss <<
"shared_ptr@" << ptr.get();
265 else oss <<
"nullptr";
269 template <
typename T>
272 std::ostringstream oss;
273 if (ptr) oss <<
"unique_ptr@" << ptr.get();
274 else oss <<
"nullptr";
290 if (
this == &other)
return *
this;
384 if (
name.empty())
return false;
385 return !std::isdigit(
static_cast<unsigned char>(
name[0]));
415 template <
typename Period>
417 if (std::is_same<Period, std::ratio<1>>::value) {
419 }
else if (std::is_same<Period, std::milli>::value) {
421 }
else if (std::is_same<Period, std::micro>::value) {
423 }
else if (std::is_same<Period, std::nano>::value) {
425 }
else if (std::is_same<Period, std::ratio<60>>::value) {
427 }
else if (std::is_same<Period, std::ratio<3600>>::value) {
The primary namespace for the LogIt++ library.
std::string format(const char *fmt,...)
Formats a string according to the specified format.
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.
@ 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).
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)