sptk2 logo
SPTK Home Page
WSBasicTypes.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/Field.h>
30#include <sptk5/wsdl/WSFieldIndex.h>
31#include <sptk5/wsdl/WSType.h>
32#include <sptk5/xdoc/Node.h>
33
34namespace sptk {
35
39class SP_EXPORT WSBasicType
40 : public WSType
41{
42public:
46 WSBasicType() = default;
47
53 WSBasicType(const char* name, bool optional)
54 : WSType(name)
55 , m_optional(optional)
56 {
57 }
58
59 WSBasicType(const WSBasicType& other) = default;
60
61 WSBasicType(WSBasicType&& other) noexcept = default;
62
63 ~WSBasicType() override = default;
64
65 WSBasicType& operator=(const WSBasicType& other)
66 {
67 m_value = other.m_value;
68 m_optional = other.m_optional;
69 return *this;
70 }
71
72 WSBasicType& operator=(WSBasicType&& other) noexcept
73 {
74 m_value = std::move(other.m_value);
75 m_optional = other.m_optional;
76 return *this;
77 }
78
83 bool optional() const
84 {
85 return m_optional;
86 }
87
92 void optional(bool opt)
93 {
94 m_optional = opt;
95 }
96
100 void clear() override
101 {
102 m_value.setNull(defaultDataType());
103 }
104
109 void load(const xdoc::SNode& attr, bool nullLargeData) override
110 {
111 if (nullLargeData && attr->getString().length() > 256)
112 {
113 m_value.setNull(VariantDataType::VAR_STRING);
114 }
115 else
116 {
117 m_value.load(attr);
118 }
119 }
120
125 virtual void load(const String& attr)
126 {
127 m_value.setString(attr);
128 }
129
134 virtual void load(const Field& field)
135 {
136 m_value = *static_cast<const Variant*>(&field);
137 }
138
142 virtual operator String() const
143 {
144 return m_value.asString();
145 }
146
150 String asString() const override
151 {
152 return m_value.asString();
153 }
154
155 auto asInteger() const
156 {
157 return value().asInteger();
158 }
159
160 auto asInt64() const
161 {
162 return value().asInt64();
163 }
164
165 auto asFloat() const
166 {
167 return value().asFloat();
168 }
169
170 auto asBool() const
171 {
172 return m_value.asBool();
173 }
174
175 void setInteger(int32_t _value)
176 {
177 m_value.setInteger(_value);
178 }
179
180 void setInt64(int64_t _value)
181 {
182 m_value.setInt64(_value);
183 }
184
185 void setFloat(bool _value)
186 {
187 m_value.setFloat(_value);
188 }
189
190 void setBool(bool _value)
191 {
192 m_value.setBool(_value);
193 }
194
195 void setBuffer(const char* buffer, size_t size)
196 {
197 m_value.setBuffer((const uint8_t*) buffer, size);
198 }
199
200 bool isNull() const override
201 {
202 return m_value.isNull();
203 }
204
209 void throwIfNull(const String& parentTypeName) const;
210
211 size_t dataSize() const
212 {
213 return m_value.dataSize();
214 }
215
216 VariantDataType dataType() const
217 {
218 return m_value.dataType();
219 }
220
221 void setNull()
222 {
223 m_value.setNull(defaultDataType());
224 }
225
226 Variant& value()
227 {
228 return m_value;
229 }
230
231 const Variant& value() const
232 {
233 return m_value;
234 }
235
241 void exportTo(const xdoc::SNode& parent, const char* name = nullptr) const override;
242
243protected:
244 void setNull(VariantDataType dataType)
245 {
246 m_value.setNull(dataType);
247 }
248
255 {
257 }
258
259private:
260 Variant m_value;
261 bool m_optional {false};
262};
263
267class SP_EXPORT WSString
268 : public WSBasicType
269{
270public:
275 {
277 }
278
284 WSString(const String& name, bool optional)
285 : WSBasicType(name.c_str(), optional)
286 {
288 }
289
294 explicit WSString(const String& _value)
295 {
296 value().setString(_value);
297 }
298
302 String className() const override
303 {
304 return "WSString";
305 }
306
313 {
315 };
316
322 void load(const xdoc::SNode& attr, bool nullLargeData) override;
323
328 void load(const String& attr) override;
329
334 void load(const Field& field) override;
335
339 WSString& operator=(const char* _value)
340 {
341 value().setString(_value);
342 return *this;
343 }
344
348 WSString& operator=(const String& _value)
349 {
350 value().setBuffer((const uint8_t*) _value.c_str(), _value.length(), VariantDataType::VAR_STRING);
351 return *this;
352 }
353
357 WSString& operator=(const Buffer& _value)
358 {
359 value().setBuffer(_value.data(), _value.bytes(), VariantDataType::VAR_BUFFER);
360 return *this;
361 }
362
366 WSString& operator=(int32_t _value)
367 {
368 value().setInteger(_value);
369 return *this;
370 }
371
375 WSString& operator=(int64_t _value)
376 {
377 value().setInt64(_value);
378 return *this;
379 }
380
384 operator String() const override
385 {
386 return value().asString();
387 }
388
389 const char* getString() const
390 {
391 return value().getString();
392 }
393};
394
398class SP_EXPORT WSBool
399 : public WSBasicType
400{
401public:
406 {
408 }
409
415 WSBool(const String& name, bool optional)
416 : WSBasicType(name.c_str(), optional)
417 {
419 }
420
426 explicit WSBool(bool _value)
427 {
428 value().setBool(_value);
429 }
430
434 String className() const override
435 {
436 return "WSBool";
437 }
438
445 {
447 };
448
454 void load(const xdoc::SNode& attr, bool nullLargeData) override;
455
460 void load(const String& attr) override;
461
466 void load(const Field& field) override;
467
471 virtual WSBool& operator=(bool _value)
472 {
473 value().setBool(_value);
474 return *this;
475 }
476
480 operator bool() const
481 {
482 return value().asBool();
483 }
484};
485
489class SP_EXPORT WSDate
490 : public WSBasicType
491{
492public:
497 {
499 }
500
506 WSDate(const String& name, bool optional)
507 : WSBasicType(name.c_str(), optional)
508 {
510 }
511
516 explicit WSDate(const DateTime& _value)
517 {
518 value().setDateTime(_value, true);
519 }
520
524 String className() const override
525 {
526 return "WSDate";
527 }
528
535 {
537 };
538
544 void load(const xdoc::SNode& attr, bool nullLargeData) override;
545
550 void load(const String& attr) override;
551
556 void load(const Field& field) override;
557
562 {
563 value().setDateTime(_value, true);
564 return *this;
565 }
566
570 auto asDate() const
571 {
572 return value().asDate();
573 }
574
578 auto asDateTime() const
579 {
580 return value().asDateTime();
581 }
582
586 operator DateTime() const
587 {
588 return value().asDate();
589 }
590};
591
595class SP_EXPORT WSDateTime
596 : public WSBasicType
597{
598public:
603 {
605 }
606
612 WSDateTime(const String& name, bool optional)
613 : WSBasicType(name.c_str(), optional)
614 {
616 }
617
622 explicit WSDateTime(const DateTime& _value)
623 {
624 value().setDateTime(_value);
625 }
626
630 String className() const override
631 {
632 return "WSDateTime";
633 }
634
641 {
643 };
644
650 void load(const xdoc::SNode& attr, bool nullLargeData) override;
651
656 void load(const String& attr) override;
657
662 void load(const Field& field) override;
663
667 String asString() const override;
668
672 auto asDate() const
673 {
674 return value().asDate();
675 }
676
680 auto asDateTime() const
681 {
682 return value().asDateTime();
683 }
684
689 {
690 value().setDateTime(_value);
691 return *this;
692 }
693
697 operator DateTime() const
698 {
699 return value().asDateTime();
700 }
701};
702
706class SP_EXPORT WSDouble
707 : public WSBasicType
708{
709public:
714 {
716 }
717
723 WSDouble(const String& name, bool optional)
724 : WSBasicType(name.c_str(), optional)
725 {
727 }
728
729 WSDouble(double _value)
730 {
731 value().setFloat(_value);
732 }
733
737 String className() const override
738 {
739 return "WSDouble";
740 }
741
748 {
750 };
751
757 void load(const xdoc::SNode& attr, bool nullLargeData) override;
758
763 void load(const String& attr) override;
764
769 void load(const Field& field) override;
770
774 WSDouble& operator=(double _value)
775 {
776 value().setFloat(_value);
777 return *this;
778 }
779
783 operator double() const
784 {
785 return value().asFloat();
786 }
787};
788
792class SP_EXPORT WSInteger
793 : public WSBasicType
794{
795public:
800 {
802 }
803
809 WSInteger(const String& name, bool optional)
810 : WSBasicType(name.c_str(), optional)
811 {
813 }
814
819 WSInteger(int _value)
820 {
821 value().setInteger(_value);
822 }
823
827 String className() const override
828 {
829 return "WSInteger";
830 }
831
838 {
840 };
841
847 void load(const xdoc::SNode& attr, bool nullLargeData) override;
848
853 void load(const String& attr) override;
854
859 void load(const Field& field) override;
860
864 WSInteger& operator=(int64_t _value)
865 {
866 value().setInt64(_value);
867 return *this;
868 }
869
873 WSInteger& operator=(int _value)
874 {
875 value().setInteger(_value);
876 return *this;
877 }
878
882 operator int32_t() const
883 {
884 return value().asInteger();
885 }
886
890 operator int64_t() const
891 {
892 return value().asInt64();
893 }
894
898 operator uint64_t() const
899 {
900 return (uint64_t) value().asInt64();
901 }
902};
903
909String SP_EXPORT wsTypeIdToName(const String& typeIdName);
910
915} // namespace sptk
size_t bytes() const
Definition: BufferStorage.h:213
uint8_t * data()
Definition: BufferStorage.h:106
Definition: Buffer.h:51
Definition: DateTime.h:86
Definition: Field.h:54
Definition: String.h:49
Definition: Variant.h:372
Definition: WSBasicTypes.h:41
virtual VariantDataType defaultDataType() const
Default data type.
Definition: WSBasicTypes.h:254
bool optional() const
Definition: WSBasicTypes.h:83
virtual void load(const String &attr)
Definition: WSBasicTypes.h:125
WSBasicType()=default
virtual void load(const Field &field)
Definition: WSBasicTypes.h:134
WSBasicType(const char *name, bool optional)
Definition: WSBasicTypes.h:53
void exportTo(const xdoc::SNode &parent, const char *name=nullptr) const override
void optional(bool opt)
Definition: WSBasicTypes.h:92
void load(const xdoc::SNode &attr, bool nullLargeData) override
Definition: WSBasicTypes.h:109
void clear() override
Definition: WSBasicTypes.h:100
void throwIfNull(const String &parentTypeName) const
String asString() const override
Definition: WSBasicTypes.h:150
Definition: WSBasicTypes.h:400
void load(const xdoc::SNode &attr, bool nullLargeData) override
VariantDataType defaultDataType() const override
Default data type.
Definition: WSBasicTypes.h:444
WSBool()
Definition: WSBasicTypes.h:405
String className() const override
Definition: WSBasicTypes.h:434
WSBool(const String &name, bool optional)
Definition: WSBasicTypes.h:415
void load(const Field &field) override
void load(const String &attr) override
virtual WSBool & operator=(bool _value)
Definition: WSBasicTypes.h:471
WSBool(bool _value)
Definition: WSBasicTypes.h:426
Definition: WSBasicTypes.h:597
void load(const xdoc::SNode &attr, bool nullLargeData) override
String className() const override
Definition: WSBasicTypes.h:630
void load(const Field &field) override
String asString() const override
WSDateTime(const String &name, bool optional)
Definition: WSBasicTypes.h:612
void load(const String &attr) override
auto asDateTime() const
Definition: WSBasicTypes.h:680
WSDateTime(const DateTime &_value)
Definition: WSBasicTypes.h:622
WSDateTime & operator=(DateTime _value)
Definition: WSBasicTypes.h:688
WSDateTime()
Definition: WSBasicTypes.h:602
auto asDate() const
Definition: WSBasicTypes.h:672
VariantDataType defaultDataType() const override
Default data type.
Definition: WSBasicTypes.h:640
Definition: WSBasicTypes.h:491
VariantDataType defaultDataType() const override
Default data type.
Definition: WSBasicTypes.h:534
WSDate(const DateTime &_value)
Definition: WSBasicTypes.h:516
void load(const Field &field) override
WSDate()
Definition: WSBasicTypes.h:496
String className() const override
Definition: WSBasicTypes.h:524
void load(const String &attr) override
auto asDate() const
Definition: WSBasicTypes.h:570
WSDate(const String &name, bool optional)
Definition: WSBasicTypes.h:506
auto asDateTime() const
Definition: WSBasicTypes.h:578
WSDate & operator=(DateTime _value)
Definition: WSBasicTypes.h:561
void load(const xdoc::SNode &attr, bool nullLargeData) override
Definition: WSBasicTypes.h:708
String className() const override
Definition: WSBasicTypes.h:737
WSDouble(const String &name, bool optional)
Definition: WSBasicTypes.h:723
void load(const String &attr) override
void load(const xdoc::SNode &attr, bool nullLargeData) override
void load(const Field &field) override
WSDouble()
Definition: WSBasicTypes.h:713
VariantDataType defaultDataType() const override
Default data type.
Definition: WSBasicTypes.h:747
WSDouble & operator=(double _value)
Definition: WSBasicTypes.h:774
Definition: WSBasicTypes.h:794
WSInteger & operator=(int64_t _value)
Definition: WSBasicTypes.h:864
WSInteger(int _value)
Definition: WSBasicTypes.h:819
String className() const override
Definition: WSBasicTypes.h:827
WSInteger(const String &name, bool optional)
Definition: WSBasicTypes.h:809
VariantDataType defaultDataType() const override
Default data type.
Definition: WSBasicTypes.h:837
WSInteger & operator=(int _value)
Definition: WSBasicTypes.h:873
void load(const Field &field) override
void load(const xdoc::SNode &attr, bool nullLargeData) override
void load(const String &attr) override
WSInteger()
Definition: WSBasicTypes.h:799
Definition: WSBasicTypes.h:269
WSString(const String &name, bool optional)
Definition: WSBasicTypes.h:284
WSString & operator=(const char *_value)
Definition: WSBasicTypes.h:339
WSString & operator=(const Buffer &_value)
Definition: WSBasicTypes.h:357
WSString()
Definition: WSBasicTypes.h:274
void load(const String &attr) override
void load(const Field &field) override
WSString & operator=(const String &_value)
Definition: WSBasicTypes.h:348
WSString(const String &_value)
Definition: WSBasicTypes.h:294
WSString & operator=(int32_t _value)
Definition: WSBasicTypes.h:366
void load(const xdoc::SNode &attr, bool nullLargeData) override
VariantDataType defaultDataType() const override
Default data type.
Definition: WSBasicTypes.h:312
WSString & operator=(int64_t _value)
Definition: WSBasicTypes.h:375
String className() const override
Definition: WSBasicTypes.h:302
Definition: WSType.h:42
VariantDataType
Definition: VariantData.h:44
@ VAR_DATE
DateTime (double)
@ VAR_FLOAT
Floating-point (double)
@ VAR_STRING
String pointer.
@ VAR_DATE_TIME
DateTime (double)
@ VAR_BUFFER
Data pointer, corresponding to BLOBS in database.

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