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