sptk2 logo
SPTK Home Page
VariantData.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/sptk.h>
30#include <variant>
31#include <utility>
32
33namespace sptk {
34
43enum class VariantDataType : uint16_t
44{
45 VAR_NONE = 0,
46 VAR_INT = 1,
47 VAR_FLOAT = 2,
48 VAR_MONEY = 4,
49 VAR_STRING = 8,
50 VAR_TEXT = 16,
51 VAR_BUFFER = 32,
52 VAR_DATE = 64,
53 VAR_DATE_TIME = 128,
54 VAR_IMAGE_PTR = 256,
55 VAR_IMAGE_NDX = 512,
56 VAR_INT64 = 1024,
57 VAR_BOOL = 2048
58};
59
61 VariantDataType type;
62 bool isNull : 1;
63 bool isExternalBuffer : 1;
64};
65
72class SP_EXPORT MoneyData
73{
74public:
75 static std::array<int64_t, 16> dividers;
76 int64_t quantity;
77 uint8_t scale;
78
84 MoneyData(int64_t quantity, uint8_t scale)
85 : quantity(quantity)
86 , scale(scale)
87 {
88 }
89
93 explicit operator double() const;
94
98 explicit operator int64_t() const;
99
103 explicit operator int32_t() const;
104
108 explicit operator bool() const;
109};
110
111class SP_EXPORT VariantData
112{
113 friend class Variant_SetMethods;
114
115public:
116 using Storage = std::variant<bool, int32_t, int64_t, double, const uint8_t*, const void*, DateTime, Buffer, MoneyData>;
117
121 VariantData() = default;
122
127 VariantData(const VariantData& other) = default;
128
133 VariantData(VariantData&& other) noexcept
134 : m_storage(std::move(other.m_storage))
135 , m_dataType(std::exchange(other.m_dataType, emptyType))
136 , m_dataSize(std::exchange(other.m_dataSize, 0))
137 {
138 }
139
140 virtual ~VariantData() = default;
141
146 VariantData& operator=(const VariantData& other) = default;
147
152 VariantData& operator=(VariantData&& other) noexcept = default;
153
158 template<typename T>
159 T& get()
160 {
161 return std::get<T>(m_storage);
162 }
163
168 template<typename T>
169 const T& get() const
170 {
171 return std::get<T>(m_storage);
172 }
173
178 template<typename T>
179 void set(const T& value)
180 {
181 m_storage = value;
182 }
183
184 void type(VariantType dataType)
185 {
186 m_dataType = dataType;
187 }
188
189 void type(VariantDataType dataType)
190 {
191 m_dataType.type = dataType;
192 }
193
194 void setNull(bool isNull)
195 {
196 m_dataType.isNull = isNull;
197 }
198
199 VariantType type() const
200 {
201 return m_dataType;
202 }
203
204 void size(size_t dataSize)
205 {
206 m_dataSize = dataSize;
207 }
208
209 size_t size() const
210 {
211 return m_dataSize;
212 }
213
214private:
215 static constexpr VariantType emptyType {VariantDataType::VAR_NONE, true, false};
216
217 Storage m_storage;
218 VariantType m_dataType {emptyType};
219 size_t m_dataSize {0};
220};
221
225} // namespace sptk
Definition: VariantData.h:73
uint8_t scale
Scale.
Definition: VariantData.h:77
int64_t quantity
Integer value.
Definition: VariantData.h:76
MoneyData(int64_t quantity, uint8_t scale)
Definition: VariantData.h:84
static std::array< int64_t, 16 > dividers
Dividers that help formatting money data.
Definition: VariantData.h:75
Definition: VariantData.h:112
const T & get() const
Access to variant data.
Definition: VariantData.h:169
VariantData & operator=(VariantData &&other) noexcept=default
void set(const T &value)
Definition: VariantData.h:179
VariantData()=default
VariantData(VariantData &&other) noexcept
Definition: VariantData.h:133
T & get()
Access to variant data.
Definition: VariantData.h:159
VariantData & operator=(const VariantData &other)=default
VariantData(const VariantData &other)=default
VariantDataType
Definition: VariantData.h:44
@ VAR_INT64
64bit integer
@ VAR_MONEY
Special (integer quantity and scale) money.
@ VAR_TEXT
String pointer, corresponding to BLOBS in database.
@ VAR_DATE
DateTime (double)
@ VAR_FLOAT
Floating-point (double)
@ VAR_IMAGE_PTR
Image pointer.
@ VAR_IMAGE_NDX
Image index in object-specific table of image pointers.
@ VAR_STRING
String pointer.
@ VAR_DATE_TIME
DateTime (double)
@ VAR_BUFFER
Data pointer, corresponding to BLOBS in database.
Definition: VariantData.h:60

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