cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
Variant.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26
27#include <string.h>
28#include <cassert>
29#include <string>
30
31namespace cppcore {
32
33//-------------------------------------------------------------------------------------------------
45//-------------------------------------------------------------------------------------------------
46class Variant {
47public:
64
66 Variant() = default;
67
73 Variant(Type type, void *pData, size_t numItems);
74
77 explicit Variant(bool value);
78
81 Variant(const Variant &other);
82
84 ~Variant();
85
88 Type getType() const;
89
92 size_t getSize() const;
93
96 void *getPtr() const;
97
100 void setByte(uint8_t value);
101
104 uint8_t getByte() const;
105
108 void setInt(int32_t val);
109
112 int32_t getInt() const;
113
118 void setInt3(int32_t val1, int32_t val2, int32_t val3);
119
123 int32_t *getInt3() const;
124
130 void setInt4(int32_t val1, int32_t val2, int32_t val3, int32_t val4);
131
135 int32_t *getInt4() const;
136
139 void setFloat(float val);
140
143 float getFloat() const;
144
149 void setFloat3(float val1, float val2, float val3);
150
154 float *getFloat3() const;
155
161 void setFloat4(float val1, float val2, float val3, float val4);
162
166 float *getFloat4() const;
167
170 void setFloat4x4(float *pData);
171
174 float *getFloat4x4() const;
175
178 void setStdString(const std::string &value);
179
182 const char *getString() const;
183
186 void setBool(bool value);
187
190 bool getBool() const;
191
193 void clear();
194
198 static Variant *createFromString(const std::string &value);
199
201 bool operator==(const Variant &rOther) const;
202 Variant &operator=(const Variant &rOther);
203
204protected:
209 bool isValid(Type type, size_t numItems) const;
210
214 void reserve(Type type, size_t size);
215
216private:
217 Type mType{Invalid};
218 size_t mBufferSize{0};
219 void *mData{nullptr};
220};
221
222inline Variant::Variant(Type type, void *pData, size_t numItems) {
223 if (isValid(type, numItems)) {
224 size_t size = 0;
225 mType = type;
226 if (type == String) {
227 assert(nullptr != pData);
228 std::string str((char *)pData);
229 setStdString(str);
230 } else {
231 reserve(type, size);
232 ::memcpy(mData, pData, mBufferSize);
233 }
234 }
235}
236
237inline Variant::Variant(bool value) : mType(Boolean) {
238 reserve(Boolean, 0);
239 ::memcpy(mData, &value, mBufferSize);
240}
241
242inline Variant::Variant(const Variant &other) :
243 mType(Invalid),
244 mBufferSize(0),
245 mData(nullptr) {
246 mType = other.mType;
247 if (String == mType) {
248 setStdString(other.getString());
249 } else {
250 reserve(mType, 0);
251 ::memcpy(mData, other.mData, mBufferSize);
252 }
253}
254
256 clear();
257}
258
260 return mType;
261}
262
263inline size_t Variant::getSize() const {
264 return mBufferSize;
265}
266
267inline void *Variant::getPtr() const {
268 return mData;
269}
270
271inline void Variant::setByte(unsigned char value) {
272 clear();
273 reserve(Byte, 0);
274 ::memcpy(mData, &value, sizeof(unsigned char));
275}
276
277inline unsigned char Variant::getByte() const {
278 return (*reinterpret_cast<unsigned char *>(mData));
279}
280
281inline void Variant::setInt(int32_t val) {
282 clear();
283 reserve(Int, 0);
284 ::memcpy(mData, &val, sizeof(int32_t));
285}
286
287inline int Variant::getInt() const {
288 assert(mType == Int);
289
290 return (*reinterpret_cast<int *>(mData));
291}
292
293inline void Variant::setInt3(int val1, int val2, int val3) {
294 clear();
295 reserve(Int3, 0);
296 int *ptr = reinterpret_cast<int *>(mData);
297 *ptr = val1;
298 ++ptr;
299 *ptr = val2;
300 ++ptr;
301 *ptr = val3;
302}
303
304inline int *Variant::getInt3() const {
305 assert(mType == Int3);
306 return (reinterpret_cast<int *>(mData));
307}
308
309inline void Variant::setInt4(int val1, int val2, int val3, int val4) {
310 clear();
311 reserve(Int4, 0);
312 int *ptr = reinterpret_cast<int *>(mData);
313 *ptr = val1;
314 ++ptr;
315 *ptr = val2;
316 ++ptr;
317 *ptr = val3;
318 ++ptr;
319 *ptr = val4;
320}
321
322inline int *Variant::getInt4() const {
323 assert(mType == Int4);
324
325 return (reinterpret_cast<int *>(mData));
326}
327
328inline void Variant::setFloat(float val) {
329 clear();
330 reserve(Float, 0);
331 ::memcpy(mData, &val, sizeof(float));
332}
333
334inline float Variant::getFloat() const {
335 assert(mType == Float);
336 return (*reinterpret_cast<float *>(mData));
337}
338
339inline void Variant::setFloat3(float val1, float val2, float val3) {
340 clear();
341 reserve(Float3, 0);
342 float *ptr = reinterpret_cast<float *>(mData);
343 *ptr = val1;
344 ++ptr;
345 *ptr = val2;
346 ++ptr;
347 *ptr = val3;
348}
349
350inline float *Variant::getFloat3() const {
351 assert(mType == Float3);
352 return (reinterpret_cast<float *>(mData));
353}
354
355inline void Variant::setFloat4(float val1, float val2, float val3, float val4) {
356 clear();
357 reserve(Float4, 0);
358 float *ptr = reinterpret_cast<float *>(mData);
359 *ptr = val1;
360 ++ptr;
361 *ptr = val2;
362 ++ptr;
363 *ptr = val3;
364 ++ptr;
365 *ptr = val4;
366}
367
368inline float *Variant::getFloat4() const {
369 assert(mType == Float4);
370 return (reinterpret_cast<float *>(mData));
371}
372
373inline void Variant::setFloat4x4(float *pData) {
374 clear();
375 reserve(Float4x4, 0);
376 ::memcpy(mData, pData, sizeof(float) * 16);
377}
378
379inline float *Variant::getFloat4x4() const {
380 assert(mType == Float4x4);
381 return (reinterpret_cast<float *>(mData));
382}
383
384inline void Variant::setStdString(const std::string &value) {
385 clear();
386 mType = String;
387 if (value.empty()) {
388 mData = nullptr;
389 mBufferSize = 0;
390 }
391 mBufferSize = sizeof(char) * (value.size() + 1);
392 mData = (char*) ::malloc(mBufferSize);
393 if (mData == nullptr) {
394 clear();
395 return;
396 }
397
398 ::memcpy(mData, value.c_str(), sizeof(char) * value.size());
399 char *ptr = (char *) mData;
400 ptr[value.size()] = '\0';
401}
402
403inline const char *Variant::getString() const {
404 assert(mType == String);
405
406 return static_cast<char *>(mData);
407}
408
409inline void Variant::setBool(bool value) {
410 clear();
411 reserve(Boolean, 0);
412 ::memcpy(mData, &value, sizeof(bool));
413}
414
415inline bool Variant::getBool() const {
416 assert(Boolean == mType);
417
418 return (*reinterpret_cast<bool *>(mData));
419}
420
421inline void Variant::clear() {
422 if (Invalid == mType) {
423 return;
424 }
425
426 ::free(mData);
427 mData = nullptr;
428 mType = Invalid;
429}
430
431inline Variant *Variant::createFromString( const std::string &value ) {
432 Variant *v = new Variant();
433 v->setStdString(value);
434
435 return v;
436}
437
438inline bool Variant::operator==(const Variant &rOther) const {
439 if (rOther.mType != mType) {
440 return false;
441 }
442
443 if (rOther.mBufferSize != mBufferSize) {
444 return false;
445 }
446
447 if (0 != ::memcmp(rOther.mData, mData, mBufferSize)) {
448 return false;
449 }
450
451 return true;
452}
453
454inline Variant &Variant::operator=(const Variant &rOther) {
455 if (!(rOther == *this)) {
456 mType = rOther.mType;
457 reserve(mType, 0);
458 ::memcpy(mData, rOther.mData, mBufferSize);
459 }
460
461 return *this;
462}
463
464inline bool Variant::isValid(Type type, size_t numItems) const {
465 bool res = false;
466 if (type == Byte) {
467 if (1 == numItems) {
468 res = true;
469 }
470 } else if (type == Int || type == Float) {
471 if (1 == numItems) {
472 res = true;
473 }
474 } else if (type == Int3 || type == Float3) {
475 if (3 == numItems) {
476 res = true;
477 }
478 } else if (type == Int4 || type == Float4) {
479 if (4 == numItems) {
480 res = true;
481 }
482 } else if (type == String) {
483 if (0 != numItems) {
484 res = true;
485 }
486 } else if (type == Invalid) {
487 res = true;
488 }
489
490 return res;
491}
492
493inline void Variant::reserve(Type type, size_t size) {
494 if (0 == size) {
495 if (type == Int3) {
496 size = sizeof(int) * 3;
497 } else if (type == Float3) {
498 size = sizeof(float) * 3;
499 } else if (type == Int4) {
500 size = sizeof(int) * 4;
501 } else if (type == Float4) {
502 size = sizeof(float) * 4;
503 } else if (type == Float4x4) {
504 size = sizeof(float) * 4 * 4;
505 } else if (type == Byte) {
506 size = sizeof(unsigned char);
507 } else if (type == Int) {
508 size = sizeof(int);
509 } else if (type == Float) {
510 size = sizeof(float);
511 } else if (type == Boolean) {
512 size = sizeof(bool);
513 }
514 }
515
516 mBufferSize = size;
517 mData = ::malloc(size);
518 mType = type;
519}
520
521} // Namespace cppcore
Type
This enum describes the variable type of the variant instance.
Definition Variant.h:50
@ Float4x4
Float matrix, 4 x 4 components.
Definition Variant.h:59
@ Int3
Integer vector, 3 components.
Definition Variant.h:54
@ Invalid
Initialization value.
Definition Variant.h:51
@ String
String value.
Definition Variant.h:60
@ Int
Integer value.
Definition Variant.h:53
@ Int4
Integer vector, 4 components.
Definition Variant.h:55
@ Byte
1 Byte type.
Definition Variant.h:52
@ Float
Float value.
Definition Variant.h:56
@ Count
Upper limit.
Definition Variant.h:62
@ Float4
Float vector, 4 components.
Definition Variant.h:58
@ Float3
Float vector, 3 components.
Definition Variant.h:57
@ Boolean
Boolean type.
Definition Variant.h:61
Type getType() const
Returns the type of the instance.
Definition Variant.h:259
bool operator==(const Variant &rOther) const
Operator implementations.
Definition Variant.h:438
Variant()=default
The class default constructor.
void clear()
Clears the variant data, type will set back to None.
Definition Variant.h:421
void setInt3(int32_t val1, int32_t val2, int32_t val3)
Set a new integer vector, the old data will be released and destroyed.
Definition Variant.h:293
static Variant * createFromString(const std::string &value)
Static helper method to generate a new variant storing a std::string.
Definition Variant.h:431
size_t getSize() const
Will return the size in bytes of the stored type.
Definition Variant.h:263
float * getFloat4x4() const
Returns the pointer to the float 4x4 value.
Definition Variant.h:379
~Variant()
The class destructor.
Definition Variant.h:255
uint8_t getByte() const
Will return the current payload as a byte value.
Definition Variant.h:277
float * getFloat3() const
Returns a pointer to the first element of the float vector.
Definition Variant.h:350
void * getPtr() const
Will return a raw pointer showing to the data.
Definition Variant.h:267
const char * getString() const
Returns a constant reference to the string value.
Definition Variant.h:403
Variant & operator=(const Variant &rOther)
Definition Variant.h:454
float getFloat() const
Returns the float value of the instance.
Definition Variant.h:334
bool isValid(Type type, size_t numItems) const
Performs a validation.
Definition Variant.h:464
void setFloat4x4(float *pData)
Assigns a new float 4x4 item.
Definition Variant.h:373
void setFloat(float val)
Returns the float value of the instance.
Definition Variant.h:328
void setByte(uint8_t value)
Will set the payload to the given value.
Definition Variant.h:271
float * getFloat4() const
Returns a pointer to the first element of the float vector.
Definition Variant.h:368
void reserve(Type type, size_t size)
Will reserve a buffer for the requested types.
Definition Variant.h:493
int32_t * getInt3() const
Returns a pointer to the first element of the integer vector.
Definition Variant.h:304
int32_t getInt() const
Returns the integer value of the instance.
Definition Variant.h:287
void setFloat3(float val1, float val2, float val3)
Set a new float vector, the old data will be released and destroyed.
Definition Variant.h:339
void setStdString(const std::string &value)
Sets a string value.
Definition Variant.h:384
void setInt4(int32_t val1, int32_t val2, int32_t val3, int32_t val4)
Set a new integer vector, the old data will be released and destroyed.
Definition Variant.h:309
void setFloat4(float val1, float val2, float val3, float val4)
Set a new float vector, the old data will be released and destroyed.
Definition Variant.h:355
int32_t * getInt4() const
Returns a pointer to the first element of the integer vector.
Definition Variant.h:322
void setBool(bool value)
Will set a new bool value.
Definition Variant.h:409
void setInt(int32_t val)
Sets a new integer value, old values will be released and destroyed.
Definition Variant.h:281
bool getBool() const
Will return the bool value.
Definition Variant.h:415