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 static const int version_;
254 bool isSpecialType()
const {
return type_ >=
Type::String; }
256 static BASE_EXPORT bool claimUserType(
int type,
const string &name);
258 template <
class T> T &conv() {
return *
reinterpret_cast<T *
>(data_); }
259 template <
class T>
const T &conv()
const {
return *
reinterpret_cast<const T *
>(data_); }
260 void del() {
if (isSpecialType()) conv<UserTypeBase>().~UserTypeBase(); }
276 char data_[dataSize_];
294 void copy(
const UserTypeBase *base)
override {
308 VUserType(const T &t = T()) : itasca::conditional_container<sizeof(T) + sizeof(VUserType<void *>) - sizeof(void *) <= Variant::dataSize_, T>(t) {};
310 VUserType(T &&t) : itasca::conditional_container<sizeof(T) + sizeof(VUserType<void *>) - sizeof(void *) <= Variant::dataSize_, T>(std::move(t)) {};
312 ~VUserType() override {}
314 VUserType<T> *clone(char *data) const override { return new(data) VUserType<T>(this->value()); }
316 void copy(const UserTypeBase *base) override {
317 const VUserType<T> *p = reinterpret_cast<const VUserType<T> *>(base);
322 static int typeNumber_;
326template <class T> inline void Variant::setToUserType(const T &t) {
327 if (getType() != VUserType<T>::typeNumber_) {
329 new(data_) VUserType<T>(t);
330 type_ = static_cast<Type>(VUserType<T>::typeNumber_);
333 conv<VUserType<T>>() = t;
336template <class T> inline void Variant::moveToUserType(T &&t) {
337 if (getType() != VUserType<T>::typeNumber_) {
339 new(data_) VUserType<T>(std::move(t));
340 type_ = static_cast<Type>(VUserType<T>::typeNumber_);
343 conv<VUserType<T>>() = std::move(t);
346template <class T> inline T Variant::toUserType() const {
347 if (getType() != VUserType<T>::typeNumber_)
349 return conv<VUserType<T>>().VUserType<T>::value();
352template <class T> T &Variant::convertToUserType() {
353 if (getType() != VUserType<T>::typeNumber_) {
355 new(data_) VUserType<T>();
356 type_ = static_cast<Type>(VUserType<T>::typeNumber_);
358 VUserType<T> &v = conv<VUserType<T>>();
362template <class T> bool Variant::registerType(int typeNumber, const string &name) {
364 static_assert(sizeof(VUserType<T>) <= dataSize_, "ERROR: Type too large for variant.");
365 if (!claimUserType(typeNumber, name)) return false;
366 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:59
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:372
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:219
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:177
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:324
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:300
const char * getRawData() const
Definition variant.h:194
BASE_EXPORT const Variant & operator=(const Variant &mv)
Equality operator.
Definition variant.cpp:73
~VUserType() override
Destructor.
Definition variant.h:290
BASE_EXPORT uint8 toUByte(bool *success=0) const
Definition variant.cpp:155
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:408
BASE_EXPORT float toFloat(bool *success=0) const
Definition variant.cpp:303
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:292
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:282
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:302
BASE_EXPORT int8 toByte(bool *success=0) const
Definition variant.cpp:134
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:294
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:198
BASE_EXPORT bool toBool(bool *success=0) const
Definition variant.cpp:105
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:345
BASE_EXPORT void setToRawData()
Definition variant.cpp:444
void * t_
The actual value being stored.
Definition variant.h:301
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:261
static BASE_EXPORT bool getUserTypeClaimed(int type)
Definition variant.cpp:480
BASE_EXPORT DVect3 toDVect3(bool *success=0) const
Definition variant.cpp:421
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:69
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:382
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:287
BASE_EXPORT DVect2 toDVect2(bool *success=0) const
Definition variant.cpp:395
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:240
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.