Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Foundation/DataExchange/VariantValue.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
5
7
8 /**
9 */
10 struct VariantValue::IRep_ {
11 virtual ~IRep_ () = default;
12 virtual Type GetType () const = 0;
13 };
14
15 /*
16 ********************************************************************************
17 ********************************** VariantValue ********************************
18 ********************************************************************************
19 */
20 inline VariantValue::VariantValue (nullptr_t)
21 : VariantValue{}
22 {
23 }
24 inline VariantValue::VariantValue (nullopt_t)
25 : VariantValue{}
26 {
27 }
28 template <same_as<bool> BOOL_T>
29 inline VariantValue::VariantValue (BOOL_T val)
30 : fVal_{val ? kTrueRep_ : kFalseRep_}
31 {
32 }
33 inline VariantValue::VariantValue (const vector<VariantValue>& val)
35 {
36 }
37 template <Characters::IConvertibleToString STRINGISH_T>
38 inline VariantValue::VariantValue (STRINGISH_T&& val)
39 requires (not same_as<remove_cvref_t<STRINGISH_T>, String>)
40 : VariantValue{String{forward<STRINGISH_T> (val)}}
41 {
42 }
43 template <typename T>
44 inline VariantValue::VariantValue (const optional<T>& val)
45 requires (is_convertible_v<T, VariantValue>)
46 : VariantValue{val.has_value () ? VariantValue{*val} : VariantValue{}}
47 {
48 }
49 template <typename T>
50 inline VariantValue& VariantValue::operator= (T&& val)
51 requires (requires (T x) { VariantValue{x}; })
52 {
53 *this = VariantValue{forward<T> (val)};
54 return *this;
55 }
56 inline VariantValue::Type VariantValue::GetType () const
57 {
58 if (fVal_ == nullptr) {
59 return Type::eNull;
60 }
61 return fVal_->GetType ();
62 }
63 inline VariantValue::operator bool () const
64 {
65 return fVal_ != nullptr;
66 }
67 template <typename RETURNTYPE>
68 inline RETURNTYPE VariantValue::As () const
69 requires (Private_::IVariantValueAsBasic_<RETURNTYPE> or
70 (Common::IOptional<RETURNTYPE> and Private_::IVariantValueAsBasic_<Common::ExtractValueType_t<RETURNTYPE>>))
71 {
72 if constexpr (same_as<RETURNTYPE, bool>) {
73 return this->AsBool_ ();
74 }
75 else if constexpr (same_as<RETURNTYPE, Memory::BLOB>) {
76 return this->AsBLOB_ ();
77 }
78 else if constexpr (signed_integral<RETURNTYPE>) {
79 return static_cast<RETURNTYPE> (this->AsInteger_ ());
80 }
81 else if constexpr (unsigned_integral<RETURNTYPE>) {
82 return static_cast<RETURNTYPE> (this->AsUnsignedInteger_ ());
83 }
84 else if constexpr (floating_point<RETURNTYPE>) {
85 return static_cast<RETURNTYPE> (this->AsFloatType_ ());
86 }
87 else if constexpr (same_as<RETURNTYPE, Time::Date>) {
88 return this->AsDate_ ();
89 }
90 else if constexpr (same_as<RETURNTYPE, Time::DateTime>) {
91 return this->AsDateTime_ ();
92 }
93 else if constexpr (same_as<RETURNTYPE, wstring>) {
94 return this->AsString_ ().As<wstring> ();
95 }
96 else if constexpr (same_as<RETURNTYPE, String>) {
97 return this->AsString_ ();
98 }
100 return this->AsMapping_ ();
101 }
102 else if constexpr (same_as<RETURNTYPE, Sequence<VariantValue>>) {
103 return this->AsSequence_ ();
104 }
106 return this->AsMapping_ ().Map<map<wstring, VariantValue>> (
107 [] (auto v) -> pair<wstring, VariantValue> { return {v.fKey.template As<wstring> (), v.fValue}; });
108 }
109 else if constexpr (same_as<RETURNTYPE, vector<VariantValue>>) {
110 return this->AsSequence_ ().As<vector<VariantValue>> ();
111 }
112#if qStroika_HasComponent_boost
113 else if constexpr (same_as<RETURNTYPE, boost::json::value>) {
114 return this->AsBoostJSONValue_ ();
115 }
116#endif
117 else if constexpr (Common::IOptional<RETURNTYPE>) {
118 if (this->empty ()) {
119 return nullopt;
120 }
121 else {
123 }
124 }
125 }
126 inline strong_ordering VariantValue::operator<=> (const VariantValue& rhs) const
127 {
128 return ThreeWayComparer{}(*this, rhs);
129 }
130 inline bool VariantValue::operator== (const VariantValue& rhs) const
131 {
132 Ensure (EqualsComparer{}(*this, rhs) == (ThreeWayComparer{}(*this, rhs) == strong_ordering::equal)); // These must return the same answer
133 return EqualsComparer{}(*this, rhs);
134 }
135
136}
137
139 template <>
140 constexpr EnumNames<DataExchange::VariantValue::Type> DefaultNames<DataExchange::VariantValue::Type>::k{{{
141 {DataExchange::VariantValue::eNull, L"Null"},
142 {DataExchange::VariantValue::eBLOB, L"BLOB"},
143 {DataExchange::VariantValue::eBoolean, L"Boolean"},
144 {DataExchange::VariantValue::eInteger, L"Integer"},
145 {DataExchange::VariantValue::eUnsignedInteger, L"Unsigned-Integer"},
146 {DataExchange::VariantValue::eFloat, L"Float"},
147 {DataExchange::VariantValue::eDate, L"Date"},
148 {DataExchange::VariantValue::eDateTime, L"DateTime"},
149 {DataExchange::VariantValue::eString, L"String"},
150 {DataExchange::VariantValue::eArray, L"Array"},
151 {DataExchange::VariantValue::eMap, L"Map"},
152 }}};
153}
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
nonvirtual bool operator==(const VariantValue &rhs) const
compares as if first normalized with Normalize()
VariantValue()=default
construct a VariantValue from most any 'basic type' you would expect to find in a weakly typed langua...
nonvirtual VariantValue & operator=(VariantValue &&rhs) noexcept=default
nonvirtual strong_ordering operator<=>(const VariantValue &rhs) const
compares as if first normalized with Normalize()
Compares values as if first normalized with Normalize () method.
Compares values as if first normalized with Normalize () method.