Chi-Tech
varying.cc
Go to the documentation of this file.
1#include "varying.h"
2
3#include <algorithm>
4#include <sstream>
5
7
8/**Provides a string-name for an enumerated VaryingDataType.*/
9std::string
11{
12 switch (type)
13 {
14 case VaryingDataType::VOID:
15 return "VOID";
16 case VaryingDataType::ARBITRARY_BYTES:
17 return "ARBITRARY_BYTES";
18 case VaryingDataType::STRING:
19 return "STRING";
20 case VaryingDataType::BOOL:
21 return "BOOL";
22 case VaryingDataType::INTEGER:
23 return "INTEGER";
24 case VaryingDataType::FLOAT:
25 return "FLOAT";
26 default:
27 return "UNKNOWN";
28 }
29}
30
31namespace chi_data_types
32{
33//=============================================== VaryingType Base
35{
36 ChiLogicalError("Method not implemented");
37}
39{
40 ChiLogicalError("Method not implemented");
41}
43{
44 ChiLogicalError("Method not implemented");
45}
47{
48 ChiLogicalError("Method not implemented");
49}
50std::vector<std::byte> Varying::VaryingType::BytesValue() const
51{
52 ChiLogicalError("Method not implemented");
53}
54
55//=============================================== VaryingByteArray
56template <>
57std::string
59{
60 ChiLogicalError("Method not implemented");
61}
62template <>
64{
65 ChiLogicalError("Method not implemented");
66}
67template <>
68int64_t
70{
71 ChiLogicalError("Method not implemented");
72}
73template <>
75{
76 ChiLogicalError("Method not implemented");
77}
78
79//=============================================== VaryingString
80template <>
82{
83 return value_;
84}
85template <>
87{
88 ChiLogicalError("Method not implemented");
89}
90template <>
92{
93 ChiLogicalError("Method not implemented");
94}
95template <>
97{
98 ChiLogicalError("Method not implemented");
99}
100
101//=============================================== VaryingBool
102template <>
104{
105 ChiLogicalError("Method not implemented");
106}
107template <>
109{
110 return value_;
111}
112template <>
114{
115 ChiLogicalError("Method not implemented");
116}
117template <>
119{
120 ChiLogicalError("Method not implemented");
121}
122
123//=============================================== VaryingInteger
124template <>
126{
127 ChiLogicalError("Method not implemented");
128}
129template <>
131{
132 ChiLogicalError("Method not implemented");
133}
134template <>
136{
137 return value_;
138}
139template <>
141{
142 ChiLogicalError("Method not implemented");
143}
144
145//=============================================== VaryingFloat
146template <>
148{
149 ChiLogicalError("Method not implemented");
150}
151template <>
153{
154 ChiLogicalError("Method not implemented");
155}
156template <>
158{
159 ChiLogicalError("Method not implemented");
160}
161template <>
163{
164 return value_;
165}
166
167} // namespace chi_data_types
168
169/**Checks if two VaryingDataType values match.
170 * Type A is matched against type B.*/
172 const VaryingDataType type_A, const VaryingDataType type_B_required) const
173{
174 if (type_A != type_B_required)
175 throw std::logic_error("Varying data type " + TypeName() +
176 " does not "
177 "correspond to the required type, " +
178 VaryingDataTypeStringName(type_B_required));
179}
180
181// ###################################################################
182// Constructors
183/**Constructor for an arbitrary sequence of bytes value.*/
184chi_data_types::Varying::Varying(const std::vector<std::byte>& value)
186{
187 // raw_data_ = value;
188 // data_initialized_ = true;
189 data_ = std::make_unique<VaryingArbitraryType<std::vector<std::byte>>>(value);
190}
191
192/**Constructor for a string value.*/
193chi_data_types::Varying::Varying(const std::string& value)
194 : type_(VaryingDataType::STRING),
195 data_(std::make_unique<VaryingArbitraryType<std::string>>(value))
196{
197}
198
199/**Copy constructor.*/
201{
202 data_ = other.data_->Clone();
203 type_ = other.type_;
204}
205
206/**Move constructor.*/
208{
209 std::swap(data_, other.data_);
210 std::swap(type_, other.type_);
211}
212
213/**Assignment operator. i.e., type_A = type_B*/
216{
217 if (this != &other)
218 {
219 data_ = other.data_->Clone();
220 type_ = other.type_;
221 }
222 return *this;
223}
224
225// ###################################################################
226// Assignments
227/**Assigns an arbitrary sequence of bytes value.*/
229chi_data_types::Varying::operator=(const std::vector<std::byte>& value)
230{
232 data_ = std::make_unique<VaryingArbitraryType<std::vector<std::byte>>>(value);
233 return *this;
234}
235
236/**Assigns a string value.*/
238chi_data_types::Varying::operator=(const std::string& value)
239{
241 data_ = std::make_unique<VaryingArbitraryType<std::string>>(value);
242 return *this;
243}
244
245// ###################################################################
246// Get values
247/**Returns the string value if valid. Otherwise throws std::logic_error.*/
249{
250 CheckTypeMatch(type_, VaryingDataType::STRING);
251
252 return data_->StringValue();
253}
254
255/**Returns the bool value if valid. Otherwise throws std::logic_error.*/
257{
258 CheckTypeMatch(type_, VaryingDataType::BOOL);
259
260 return data_->BoolValue();
261}
262
263/**Returns the integer value if valid. Otherwise throws std::logic_error.*/
265{
266 CheckTypeMatch(type_, VaryingDataType::INTEGER);
267
268 return data_->IntegerValue();
269}
270
271/**Returns the float value if valid. Otherwise throws std::logic_error.*/
273{
274 CheckTypeMatch(type_, VaryingDataType::FLOAT);
275
276 return data_->FloatValue();
277}
278
279// ###################################################################
280/**Returns the raw byte size associated with the type.*/
281size_t chi_data_types::Varying::ByteSize() const { return data_->Size(); }
282
283// ###################################################################
284/**Returns a string value for the value.*/
285std::string chi_data_types::Varying::PrintStr(bool with_type/*=false*/) const
286{
287 std::stringstream outstr;
288
290 outstr << "\"" << this->StringValue() << "\"";
292 outstr << this->FloatValue() << (with_type ? "(double)" : "");
294 outstr << this->IntegerValue();
296 outstr << (this->BoolValue() ? "true" : "false");
297
298 return outstr.str();
299}
300
301// ###################################################################
302/**Stream operator*/
303std::ostream& operator<<(std::ostream& outstr,
304 const chi_data_types::Varying& value)
305{
306 outstr << value.PrintStr(false);
307 return outstr;
308}
#define ChiLogicalError(message)
std::string StringValue() const override
virtual double FloatValue() const
Definition: varying.cc:46
virtual int64_t IntegerValue() const
Definition: varying.cc:42
virtual std::vector< std::byte > BytesValue() const
Definition: varying.cc:50
virtual std::string StringValue() const
Definition: varying.cc:34
virtual bool BoolValue() const
Definition: varying.cc:38
VaryingDataType type_
Definition: varying.h:216
std::string PrintStr(bool with_type=true) const
Definition: varying.cc:285
double FloatValue() const
Definition: varying.cc:272
std::string StringValue() const
Definition: varying.cc:248
Varying & operator=(const Varying &other)
Definition: varying.cc:215
int64_t IntegerValue() const
Definition: varying.cc:264
void CheckTypeMatch(VaryingDataType type_A, VaryingDataType type_B_required) const
Definition: varying.cc:171
std::unique_ptr< VaryingType > data_
Definition: varying.h:217
size_t ByteSize() const
Definition: varying.cc:281
Varying(const T &value)
Definition: varying.h:232
bool BoolValue() const
Definition: varying.cc:256
@ INTEGER
Datatype mapping to int64_t.
@ STRING
Datatype mapping to std::string.
@ BOOL
Datatype mapping to bool.
@ ARBITRARY_BYTES
Basic sequence of bytes.
@ FLOAT
Datatype mapping to double.
std::string VaryingDataTypeStringName(VaryingDataType type)
Definition: varying.cc:10
std::ostream & operator<<(std::ostream &outstr, const chi_data_types::Varying &value)
Definition: varying.cc:303