11#include "type_selector.h"
27#pragma warning(disable:26450)
28#pragma warning(disable:26495)
29#pragma warning(disable:26812)
30#pragma warning(disable:26495)
33 template <
class T>
friend class VUserType;
42 virtual UserTypeBase *
clone(
char *data)
const = 0;
44 virtual void copy(
const UserTypeBase *base) = 0;
49 static const int dataSize_ = 80;
51 static const int dataSize_ = 72;
55 static const int dataSize_ = 40;
57 static const int dataSize_ = 36;
221 template <
class T>
const T &
fastToUserType()
const {
return conv<VUserType<T> >().value(); }
250 template <
class T>
static bool registerType(
int type,
const string &name);
253 bool isSpecialType()
const {
return type_ >=
Type::String; }
255 static BASE_EXPORT bool claimUserType(
int type,
const string &name);
257 template <
class T> T &conv() {
return *
reinterpret_cast<T *
>(data_); }
258 template <
class T>
const T &conv()
const {
return *
reinterpret_cast<const T *
>(data_); }
259 void del() {
if (isSpecialType()) conv<UserTypeBase>().~UserTypeBase(); }
275 char data_[dataSize_];
293 void copy(
const UserTypeBase *base)
override {
307 VUserType(const T &t = T()) : itasca::conditional_container<sizeof(T) + sizeof(VUserType<void *>) - sizeof(void *) <= Variant::dataSize_, T>(t) {};
309 VUserType(T &&t) : itasca::conditional_container<sizeof(T) + sizeof(VUserType<void *>) - sizeof(void *) <= Variant::dataSize_, T>(std::move(t)) {};
311 ~VUserType() override {}
313 VUserType<T> *clone(char *data) const override { return new(data) VUserType<T>(this->value()); }
315 void copy(const UserTypeBase *base) override {
316 const VUserType<T> *p = reinterpret_cast<const VUserType<T> *>(base);
321 static int typeNumber_;
325template <class T> inline void Variant::setToUserType(const T &t) {
326 if (getType() != VUserType<T>::typeNumber_) {
328 new(data_) VUserType<T>(t);
329 type_ = static_cast<Type>(VUserType<T>::typeNumber_);
332 conv<VUserType<T>>() = t;
335template <class T> inline void Variant::moveToUserType(T &&t) {
336 if (getType() != VUserType<T>::typeNumber_) {
338 new(data_) VUserType<T>(std::move(t));
339 type_ = static_cast<Type>(VUserType<T>::typeNumber_);
342 conv<VUserType<T>>() = std::move(t);
345template <class T> inline T Variant::toUserType() const {
346 if (getType() != VUserType<T>::typeNumber_)
348 return conv<VUserType<T>>().VUserType<T>::value();
351template <class T> T &Variant::convertToUserType() {
352 if (getType() != VUserType<T>::typeNumber_) {
354 new(data_) VUserType<T>();
355 type_ = static_cast<Type>(VUserType<T>::typeNumber_);
357 VUserType<T> &v = conv<VUserType<T>>();
361template <class T> bool Variant::registerType(int typeNumber, const string &name) {
363 static_assert(sizeof(VUserType<T>) <= dataSize_, "ERROR: Type too large for variant.");
364 if (!claimUserType(typeNumber, name)) return false;
365 VUserType<T>::typeNumber_ = typeNumber;
QString helper functions, plus some additions.
This is a helper class, designed to make it possible to allow user-defined types to be encoded in a V...
Definition variant.h:68
A simpler (and slightly faster) version of a QVariant, added for interfaces that do not use Qt.
Definition variant.h:32
A container, which either stores its value as a value or a pointer depending on the condition.
Definition type_selector.h:61
char * getRawData()
Definition variant.h:193
const T & fastToUserType() const
Definition variant.h:221
static bool registerType(int type, const string &name)
This method allows user-defined types to be encoded in a variant.
BASE_EXPORT Variant(const IVect3 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:116
BASE_EXPORT void * toVoid(bool *success=0) const
Definition variant.cpp:368
const Variant & operator=(const double &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:173
BASE_EXPORT Variant(const int16 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:105
BASE_EXPORT Variant(const IVect2 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:114
const Variant & operator=(const int64 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:170
BASE_EXPORT int32 toInt(bool *success=0) const
Definition variant.cpp:215
const Variant & operator=(void *v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:174
BASE_EXPORT string getTypeName() const
A string indicating the Variant type.
Definition variant.h:128
BASE_EXPORT int16 toShort(bool *success=0) const
Definition variant.cpp:173
std::basic_string< char8 > String
std::string of type Char
Definition basebool.h:9
BASE_EXPORT Variant(const DVect2 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:115
BASE_EXPORT const DVect3 & fastToDVect3() const
Definition variant.h:159
BASE_EXPORT double toDouble(bool *success=0) const
Definition variant.cpp:320
const Variant & operator=(const IVect2 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:176
BASE_EXPORT ~Variant()
Destructor.
Definition variant.h:121
const Variant & operator=(const IVect3 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:179
static bool isValue()
Checks if the user type holds the literal value of T, or a pointer to it.
Definition variant.h:299
const char * getRawData() const
Definition variant.h:194
BASE_EXPORT const Variant & operator=(const Variant &mv)
Equality operator.
Definition variant.cpp:69
~VUserType() override
Destructor.
Definition variant.h:289
BASE_EXPORT uint8 toUByte(bool *success=0) const
Definition variant.cpp:151
BASE_EXPORT Variant(const uint16 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:106
BASE_EXPORT IVect3 toIVect3(bool *success=0) const
Definition variant.cpp:404
BASE_EXPORT float toFloat(bool *success=0) const
Definition variant.cpp:299
const Variant & operator=(const DVect2 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:177
const Variant & operator=(const int16 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:166
BASE_EXPORT Variant(const Variant &mv)
Copy constructor.
Definition variant.h:96
VUserType< void * > * clone(char *data) const override
in-place clone
Definition variant.h:291
const Variant & operator=(const void *v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:175
BASE_EXPORT Variant(const bool &v)
Definition variant.h:102
BASE_EXPORT Variant(const uint8 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:104
void setToUserType(const T &t)
BASE_EXPORT Variant(const int32 &v=0)
Definition variant.h:99
const Variant & operator=(const int8 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:164
bool isUserType() const
Definition variant.h:226
T & fastToUserType()
Definition variant.h:220
UserTypeBase()
Default constructor, no data initialization.
Definition variant.h:38
BASE_EXPORT uint64 toULong(bool *success=0) const
Definition variant.cpp:278
BASE_EXPORT Variant(const uint64 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:109
virtual void copy(const UserTypeBase *base)=0
Copy from base pointer.
static int typeNumber_
The typeNumber_ used to uniquely identify the type.
Definition variant.h:301
BASE_EXPORT int8 toByte(bool *success=0) const
Definition variant.cpp:130
BASE_EXPORT const DVect2 & fastToDVect2() const
Definition variant.h:158
const Variant & operator=(const uint64 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:171
const Variant & operator=(const uint16 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:167
BASE_EXPORT Variant(const int64 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:108
void copy(const UserTypeBase *base) override
copy from base pointer, uses reinterpret_cast to upcast (careful!).
Definition variant.h:293
BASE_EXPORT Variant(const int8 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:103
#define BASE_EXPORT
Definition basedef.h:24
void moveToUserType(T &&t)
BASE_EXPORT uint16 toUShort(bool *success=0) const
Definition variant.cpp:194
BASE_EXPORT bool toBool(bool *success=0) const
Definition variant.cpp:101
BASE_EXPORT Variant(void *v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:112
BASE_EXPORT string toString(bool *success=0) const
Definition variant.cpp:341
BASE_EXPORT void setToRawData()
Definition variant.cpp:440
void * t_
The actual value being stored.
Definition variant.h:300
BASE_EXPORT const int64 & fastToLong() const
Definition variant.h:156
const Variant & operator=(const char *v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:182
BASE_EXPORT Type getType() const
The type of the Variant. Note that this may be a user-defined value.
Definition variant.h:126
const Variant & operator=(const DAVect2 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:178
const Variant & operator=(const uint8 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:165
BASE_EXPORT const int32 & fastToInt() const
Definition variant.h:154
const Variant & operator=(const bool &v)
Assignment operator for a specific type, converts the Variant to the same type and value.
Definition variant.h:163
BASE_EXPORT int64 toLong(bool *success=0) const
Definition variant.cpp:257
static BASE_EXPORT bool getUserTypeClaimed(int type)
Definition variant.cpp:476
BASE_EXPORT DVect3 toDVect3(bool *success=0) const
Definition variant.cpp:417
BASE_EXPORT Variant(const float &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:110
BASE_EXPORT Variant(const DVect3 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:117
BASE_EXPORT const string & fastToString() const
Definition variant.cpp:65
static BASE_EXPORT int getRawDataSize()
Definition variant.h:231
const Variant & operator=(const uint32 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:169
BASE_EXPORT IVect2 toIVect2(bool *success=0) const
Definition variant.cpp:378
virtual UserTypeBase * clone(char *data) const =0
Clone function.
BASE_EXPORT Variant(const uint32 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:107
const Variant & operator=(const int32 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:168
VUserType()
Default constructor.
Definition variant.h:286
BASE_EXPORT DVect2 toDVect2(bool *success=0) const
Definition variant.cpp:391
BASE_EXPORT const bool & fastToBool() const
Definition variant.h:155
BASE_EXPORT Variant(const double &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:111
BASE_EXPORT Variant(const void *v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:113
virtual ~UserTypeBase()
Deconstructor.
Definition variant.h:40
BASE_EXPORT uint32 toUInt(bool *success=0) const
Definition variant.cpp:236
const Variant & operator=(const float &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:172
const Variant & operator=(const DVect3 &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition variant.h:180
friend BASE_EXPORT QDataStream & operator<<(QDataStream &, const Variant &)
Allows writing a Variant to a QDataStream.
Definition basetoqt.cpp:195
Type
Indicates the type of the variant. 18 types are predefined.
Definition variant.h:74
BASE_EXPORT const double & fastToDouble() const
Definition variant.h:157
@ UInt
unsigned 32 bit integer.
@ Int
signed 32 bit integer.
@ Float
32 bit floating point value.
@ String
A String() class.
@ Short
signed 16 bit integer.
@ ULong
unsigned 64 bit integer.
@ UByte
unsigned 8 bit integer.
@ DVect2
A 2D vector of 64 bit floating point values.
@ Long
signed 64 bit integer.
@ User
A user-defined type. See the Variant::registerType() method.
@ Byte
signed 8 bit integer.
@ IVect3
A 3D vector of 32 bit signed integers.
@ DVect3
A 3D vector of 64 bit floating point values.
@ IVect2
A 2D vector of 32 bit signed integers.
@ Bool
A value of type bool.
@ RawData
Raw binary data – see the Variant::setToRawData() and Variant::getRawData() methods.
@ Double
64 bit floating point value.
@ UShort
unsigned 16 bit integer.
2D and 3D vector utility classes.