sptk2 logo
SPTK Home Page
Variant.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/Buffer.h>
30#include <sptk5/DateTime.h>
31#include <sptk5/Exception.h>
32#include <sptk5/VariantData.h>
33#include <sptk5/sptk.h>
34
35#include <utility>
36
37namespace sptk {
38
39namespace xdoc {
40class Node;
41}
42
48class Field;
49
50class SP_EXPORT BaseVariant
51{
52 friend class VariantAdaptors;
53
54public:
58 BaseVariant() = default;
59
64 BaseVariant(const BaseVariant& other) = default;
65
70 BaseVariant(BaseVariant&& other) noexcept = default;
71
75 virtual ~BaseVariant() = default;
76
80 [[nodiscard]] VariantDataType dataType() const;
81
85 [[nodiscard]] size_t dataSize() const;
86
91 void dataSize(size_t newDataSize);
92
96 [[nodiscard]] size_t bufferSize() const;
97
103 [[nodiscard]] bool isNull() const;
104
109 [[nodiscard]] static String typeName(VariantDataType type);
110
115 [[nodiscard]] static VariantDataType nameType(const char* name);
116
122 template<typename T>
123 const T& get() const
124 {
125 return m_data.get<T>();
126 }
127
133 template<typename T>
134 T& get()
135 {
136 return m_data.get<T>();
137 }
138
142 [[nodiscard]] virtual const MoneyData& getMoney() const;
143
147 [[nodiscard]] virtual const char* getString() const;
148
152 [[nodiscard]] virtual const uint8_t* getExternalBuffer() const;
153
157 [[nodiscard]] virtual const char* getText() const;
158
162 [[nodiscard]] virtual const uint8_t* getImagePtr() const;
163
167 [[nodiscard]] virtual uint32_t getImageNdx() const;
168
169protected:
173 void dataType(VariantType newDataType);
174
178 void dataType(VariantDataType newDataType);
179
183 [[nodiscard]] bool isExternalBuffer() const
184 {
185 return m_data.type().isExternalBuffer;
186 }
187
192 [[nodiscard]] virtual String moneyDataToString() const;
193
195};
196
201class SP_EXPORT VariantAdaptors
202 : public BaseVariant
203{
204public:
208 VariantAdaptors() = default;
209
214 VariantAdaptors(const VariantAdaptors& other) = default;
215
220 VariantAdaptors(VariantAdaptors&& other) noexcept = default;
221
225 ~VariantAdaptors() override = default;
226
230 virtual void setBool(bool value);
231
235 virtual void setInteger(int32_t value);
236
240 virtual void setInt64(int64_t value);
241
245 virtual void setFloat(double value);
246
250 virtual void setMoney(int64_t value, unsigned scale);
251
255 virtual void setString(const String& value);
256
260 virtual void setBuffer(const uint8_t* value, size_t valueSize, VariantDataType type = VariantDataType::VAR_BUFFER);
261
265 virtual void setExternalBuffer(uint8_t* value, size_t valueSize, VariantDataType type = VariantDataType::VAR_BUFFER);
266
270 virtual void setDateTime(DateTime value, bool dateOnly = false);
271
275 virtual void setImagePtr(const uint8_t* value);
276
280 virtual void setImageNdx(uint32_t value);
281
285 virtual void setMoney(const MoneyData& value);
286
296
302 [[nodiscard]] int asInteger() const;
303
309 [[nodiscard]] int64_t asInt64() const;
310
318 [[nodiscard]] bool asBool() const;
319
325 [[nodiscard]] double asFloat() const;
326
332 [[nodiscard]] virtual String asString() const;
333
339 [[nodiscard]] DateTime asDate() const;
340
346 [[nodiscard]] DateTime asDateTime() const;
347
354 const uint8_t* asImagePtr() const;
355
356protected:
360 void setData(const BaseVariant& other);
361 [[nodiscard]] const char* getBufferPtr() const;
362};
363
370class SP_EXPORT Variant
371 : public VariantAdaptors
372{
373public:
378
382 Variant(bool value);
383
387 Variant(int32_t value);
388
392 Variant(int64_t value, unsigned scale = 1);
393
397 Variant(double value);
398
402 Variant(const char* value);
403
407 Variant(const String& v);
408
412 Variant(const DateTime& v);
413
419 Variant(const uint8_t* value, size_t sz);
420
425 Variant(const Buffer& value);
426
431 explicit Variant(const Variant& other) = default;
432
437 Variant(Variant&& other) noexcept = default;
438
442 ~Variant() override;
443
448 Variant& operator=(const Variant& other);
449
454 Variant& operator=(Variant&& other) noexcept;
455
460 virtual Variant& operator=(bool value);
461
466 virtual Variant& operator=(int32_t value);
467
472 virtual Variant& operator=(int64_t value);
473
478 virtual Variant& operator=(double value);
479
484 virtual Variant& operator=(const MoneyData& value);
485
490 virtual Variant& operator=(const char* value);
491
496 virtual Variant& operator=(const String& value);
497
502 virtual Variant& operator=(DateTime value);
503
508 virtual Variant& operator=(const uint8_t* value);
509
514 virtual Variant& operator=(const Buffer& value);
515
519 virtual explicit operator bool() const;
520
524 virtual explicit operator int() const;
525
529 virtual explicit operator int64_t() const;
530
534 virtual explicit operator uint64_t() const;
535
539 virtual explicit operator double() const;
540
544 virtual explicit operator String() const;
545
549 virtual explicit operator DateTime() const;
550
555 virtual void load(const std::shared_ptr<xdoc::Node>& element);
556
561 void save(const std::shared_ptr<xdoc::Node>& node) const;
562};
563
567using VariantVector = std::vector<Variant>;
568
572} // namespace sptk
Definition: Variant.h:51
virtual String moneyDataToString() const
virtual const char * getText() const
VariantDataType dataType() const
size_t bufferSize() const
T & get()
Direct and fast access to variant data.
Definition: Variant.h:134
bool isExternalBuffer() const
Definition: Variant.h:183
void dataType(VariantDataType newDataType)
size_t dataSize() const
virtual const uint8_t * getExternalBuffer() const
virtual const MoneyData & getMoney() const
bool isNull() const
static String typeName(VariantDataType type)
void dataSize(size_t newDataSize)
const T & get() const
Direct and fast const access to variant data.
Definition: Variant.h:123
BaseVariant(BaseVariant &&other) noexcept=default
Move constructor.
virtual const uint8_t * getImagePtr() const
static VariantDataType nameType(const char *name)
void dataType(VariantType newDataType)
virtual uint32_t getImageNdx() const
BaseVariant(const BaseVariant &other)=default
Copy constructor.
virtual ~BaseVariant()=default
Destructor.
VariantData m_data
Internal variant data storage.
Definition: Variant.h:194
BaseVariant()=default
Default constructor.
virtual const char * getString() const
Definition: Buffer.h:51
Definition: DateTime.h:86
Definition: VariantData.h:73
Definition: String.h:49
Definition: Variant.h:203
const uint8_t * asImagePtr() const
virtual void setImagePtr(const uint8_t *value)
VariantAdaptors(VariantAdaptors &&other) noexcept=default
Move constructor.
void setData(const BaseVariant &other)
virtual void setBool(bool value)
virtual void setFloat(double value)
virtual void setMoney(const MoneyData &value)
virtual void setDateTime(DateTime value, bool dateOnly=false)
virtual void setBuffer(const uint8_t *value, size_t valueSize, VariantDataType type=VariantDataType::VAR_BUFFER)
virtual void setNull(VariantDataType vtype=VariantDataType::VAR_NONE)
virtual void setInt64(int64_t value)
int64_t asInt64() const
virtual void setMoney(int64_t value, unsigned scale)
virtual void setImageNdx(uint32_t value)
virtual void setInteger(int32_t value)
VariantAdaptors()=default
Default constructor.
virtual String asString() const
double asFloat() const
virtual void setString(const String &value)
virtual void setExternalBuffer(uint8_t *value, size_t valueSize, VariantDataType type=VariantDataType::VAR_BUFFER)
DateTime asDateTime() const
DateTime asDate() const
VariantAdaptors(const VariantAdaptors &other)=default
Copy constructor.
~VariantAdaptors() override=default
Default destructor.
Definition: VariantData.h:112
Definition: Variant.h:372
virtual Variant & operator=(const MoneyData &value)
Variant(const Variant &other)=default
virtual Variant & operator=(double value)
~Variant() override
virtual void load(const std::shared_ptr< xdoc::Node > &element)
Variant(const Buffer &value)
Variant(const String &v)
virtual Variant & operator=(int64_t value)
Variant & operator=(Variant &&other) noexcept
virtual Variant & operator=(int32_t value)
Variant(int64_t value, unsigned scale=1)
Variant(bool value)
virtual Variant & operator=(const Buffer &value)
virtual Variant & operator=(bool value)
Variant & operator=(const Variant &other)
Variant(const char *value)
Variant(int32_t value)
virtual Variant & operator=(DateTime value)
Variant(Variant &&other) noexcept=default
void save(const std::shared_ptr< xdoc::Node > &node) const
Variant(const uint8_t *value, size_t sz)
Variant(double value)
virtual Variant & operator=(const char *value)
Variant(const DateTime &v)
virtual Variant & operator=(const uint8_t *value)
virtual Variant & operator=(const String &value)
VariantDataType
Definition: VariantData.h:44
std::vector< Variant > VariantVector
Definition: Variant.h:567
@ VAR_BUFFER
Data pointer, corresponding to BLOBS in database.
Definition: VariantData.h:60

Fri Oct 14 2022 09:58:32: SPTK 5.4.1