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 <string>
9#include <iostream>
10#include <cstdint>
11#include <type_traits>
12#include <exception>
13
14namespace logit {
15
19 std::string name;
21
42
43 union {
44 int8_t int8_value;
45 uint8_t uint8_value;
46 int16_t int16_value;
47 uint16_t uint16_value;
48 int32_t int32_value;
49 uint32_t uint32_value;
50 int64_t int64_value;
51 uint64_t uint64_value;
57
58 std::string string_value;
59
60 // Constructors for each type.
61 VariableValue(const std::string& name, int8_t value)
63 pod_value.int8_value = value;
64 }
65
66 VariableValue(const std::string& name, uint8_t value)
68 pod_value.uint8_value = value;
69 }
70
71 VariableValue(const std::string& name, int16_t value)
73 pod_value.int16_value = value;
74 }
75
76 VariableValue(const std::string& name, uint16_t value)
78 pod_value.uint16_value = value;
79 }
80
81 VariableValue(const std::string& name, int32_t value)
83 pod_value.int32_value = value;
84 }
85
86 VariableValue(const std::string& name, uint32_t value)
88 pod_value.uint32_value = value;
89 }
90
91 VariableValue(const std::string& name, int64_t value)
93 pod_value.int64_value = value;
94 }
95
96 VariableValue(const std::string& name, uint64_t value)
98 pod_value.uint64_value = value;
99 }
100
101 VariableValue(const std::string& name, bool value)
103 pod_value.bool_value = value;
104 }
105
106 VariableValue(const std::string& name, char value)
108 pod_value.char_value = value;
109 }
110
111 VariableValue(const std::string& name, float value)
113 pod_value.float_value = value;
114 }
115
116 VariableValue(const std::string& name, double value)
118 pod_value.double_value = value;
119 }
120
121 VariableValue(const std::string& name, const std::string& value)
123 }
124
125 VariableValue(const std::string& name, const char* value)
127 }
128
129 VariableValue(const std::string& name, const std::exception& ex)
131 }
132
137 template <typename EnumType>
138 VariableValue(const std::string& name, EnumType value,
139 typename std::enable_if<std::is_enum<EnumType>::value>::type* = 0)
141 }
142
145 : name(other.name), is_literal(other.is_literal), type(other.type), string_value(other.string_value) {
146 if (is_pod_type(type)) {
147 pod_value = other.pod_value;
148 }
149 }
150
153 if (this == &other) return *this; // Self-assignment check.
154
155 name = other.name;
156 is_literal = other.is_literal;
157 type = other.type;
159
160 if (is_pod_type(type)) {
161 pod_value = other.pod_value;
162 }
163
164 return *this;
165 }
166
168 ~VariableValue() = default;
169
172 std::string to_string() const {
173 switch (type) {
174 case ValueType::INT8_VAL: return std::to_string(pod_value.int8_value);
175 case ValueType::UINT8_VAL: return std::to_string(pod_value.uint8_value);
176 case ValueType::INT16_VAL: return std::to_string(pod_value.int16_value);
177 case ValueType::UINT16_VAL: return std::to_string(pod_value.uint16_value);
178 case ValueType::INT32_VAL: return std::to_string(pod_value.int32_value);
179 case ValueType::UINT32_VAL: return std::to_string(pod_value.uint32_value);
180 case ValueType::INT64_VAL: return std::to_string(pod_value.int64_value);
181 case ValueType::UINT64_VAL: return std::to_string(pod_value.uint64_value);
182 case ValueType::BOOL_VAL: return pod_value.bool_value ? "true" : "false";
183 case ValueType::CHAR_VAL: return std::string(1, pod_value.char_value);
184 case ValueType::FLOAT_VAL: return std::to_string(pod_value.float_value);
185 case ValueType::DOUBLE_VAL: return std::to_string(pod_value.double_value);
189 return string_value;
190 default:
191 return "unknown";
192 }
193 }
194
198 std::string to_string(const char* fmt) const {
199 switch (type) {
200 case ValueType::INT8_VAL: return format(fmt, pod_value.int8_value);
201 case ValueType::UINT8_VAL: return format(fmt, pod_value.uint8_value);
202 case ValueType::INT16_VAL: return format(fmt, pod_value.int16_value);
203 case ValueType::UINT16_VAL: return format(fmt, pod_value.uint16_value);
204 case ValueType::INT32_VAL: return format(fmt, pod_value.int32_value);
205 case ValueType::UINT32_VAL: return format(fmt, pod_value.uint32_value);
206 case ValueType::INT64_VAL: return format(fmt, pod_value.int64_value);
207 case ValueType::UINT64_VAL: return format(fmt, pod_value.uint64_value);
208 case ValueType::BOOL_VAL: return format(fmt, pod_value.bool_value);
209 case ValueType::CHAR_VAL: return format(fmt, pod_value.char_value);
210 case ValueType::FLOAT_VAL: return format(fmt, pod_value.float_value);
211 case ValueType::DOUBLE_VAL: return format(fmt, pod_value.double_value);
215 return format(fmt, string_value.c_str());
216 default:
217 return "unknown";
218 }
219 }
220
221 private:
225 static bool is_valid_literal_name(const std::string& name) {
226 if (name.empty()) return false;
227 return !std::isdigit(static_cast<unsigned char>(name[0]));
228 }
229
234 template <typename EnumType>
235 std::string enum_to_string(EnumType value) {
236 // Convert enum to underlying integral value and then to string.
237 typedef typename std::underlying_type<EnumType>::type UnderlyingType;
238 return std::to_string(static_cast<UnderlyingType>(value));
239 }
240
245 switch (type) {
258 return true;
259 default:
260 return false;
261 }
262 }
263 };
264
265} // namespace logit
266
267#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
Structure for storing values of various types, including enumerations.
VariableValue(const std::string &name, int8_t value)
VariableValue(const std::string &name, uint64_t value)
VariableValue(const std::string &name, char value)
std::string enum_to_string(EnumType value)
Helper function to convert an enumeration to a string.
std::string name
Variable name.
VariableValue & operator=(const VariableValue &other)
Assignment operator.
VariableValue(const std::string &name, uint32_t value)
VariableValue(const std::string &name, int16_t value)
VariableValue(const std::string &name, float value)
~VariableValue()=default
Destructor.
VariableValue(const std::string &name, int64_t value)
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
std::string string_value
Variable to store string, exception messages, and enums.
VariableValue(const std::string &name, bool value)
VariableValue(const std::string &name, double value)
VariableValue(const std::string &name, const std::string &value)
VariableValue(const VariableValue &other)
Copy constructor.
ValueType
Enumeration of possible value types.
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.
VariableValue(const std::string &name, const std::exception &ex)
static bool is_valid_literal_name(const std::string &name)
Helper function to check if a name is a valid literal.
static bool is_pod_type(ValueType type)
Helper function to determine if a ValueType represents a POD type.
VariableValue(const std::string &name, uint16_t value)
VariableValue(const std::string &name, uint8_t value)
std::string to_string(const char *fmt) const
Method to get the value as a formatted string.
VariableValue(const std::string &name, int32_t value)
VariableValue(const std::string &name, const char *value)