xlnt - community edition
variant.hpp
1 // Copyright (c) 2017-2022 Thomas Fussell
2 // Copyright (c) 2024-2026 xlnt-community
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE
21 //
22 // @license: http://www.opensource.org/licenses/mit-license.php
23 // @author: see AUTHORS file
24 
25 #pragma once
26 
27 #include <cstdint>
28 #include <string>
29 #include <vector>
30 
31 #include <xlnt/xlnt_config.hpp>
32 
33 namespace xlnt {
34 
35 struct datetime;
36 
40 class XLNT_API variant
41 {
42 public:
43  // TODO: implement remaining types?
44 
48  enum class type
49  {
50  //variant = 0, // this variant type
51  vector = 1,
52  //array = 2,
53  //blob = 3, // XSD type xsd:base64Binary
54  //oblob = 4, // XSD type xsd:base64Binary
55  //empty = 5,
56  null = 6,
57  //i1 = 7, // XSD type xsd:byte (8-bit signed integer)
58  //i2 = 8, // XSD type xsd:short (16-bit signed integer)
59  i4 = 9, // XSD type xsd:int (32-bit signed integer)
60  //i8 = 10, // XSD type xsd:long (64-bit signed integer)
61  //integer = 11, // XSD type xsd:int (32-bit signed integer)
62  //ui1 = 12, // XSD type xsd:unsignedByte (8-bit unsigned integer)
63  //ui2 = 13, // XSD type xsd:unsignedShort (16-bit unsigned integer)
64  //ui4 = 14, // XSD type xsd:unsignedInt (32-bit unsigned integer)
65  //ui8 = 15, // XSD type xsd:unsignedLong (64-bit unsigned integer)
66  //uint = 16, // XSD type xsd:unsignedInt (32-bit unsigned integer)
67  //r4 = 17, // XSD type xsd:float (32-bit floating point number)
68  //r8 = 18, // XSD type xsd:double (64-bit floating point number)
69  //decimal = 19, XSD type xsd:decimal
70  lpstr = 20, // XSD type xsd:string. OOXML says: "This element specifies a string variant type."
71  //lpwstr = 21, // XSD type xsd:string. OOXML says: "This element specifies a string variant type."
72  //bstr = 22, // XSD type xsd:string. OOXML says: "This element defines a binary basic string variant type, which can store any valid Unicode character."
73  date = 23, // XSD type xsd:dateTime
74  //filetime = 24, // XSD type xsd:dateTime
75  boolean = 25, // XSD type xsd:boolean (any of: 'true', 'false', '1', '0')
76  //cy = 26,
77  //error = 27,
78  //stream = 28, // XSD type xsd:base64Binary
79  //ostream = 29, // XSD type xsd:base64Binary
80  //storage = 30, // XSD type xsd:base64Binary
81  //ostorage = 31, // XSD type xsd:base64Binary
82  //vstream = 32,
83  //clsid = 33
84  };
85 
89  static std::string get_type_string(type type);
90 
94  variant();
95 
99  variant(const std::string &value);
100 
104  variant(const char *value);
105 
109  variant(std::int32_t value);
110 
114  variant(bool value);
115 
119  variant(const datetime &value);
120 
124  variant(const std::initializer_list<std::int32_t> &value);
125 
129  variant(const std::vector<std::int32_t> &value);
130 
134  variant(const std::initializer_list<const char *> &value);
135 
139  variant(const std::vector<const char *> &value);
140 
144  variant(const std::initializer_list<std::string> &value);
145 
149  variant(const std::vector<std::string> &value);
150 
154  variant(const std::initializer_list<bool> &value);
155 
159  variant(const std::vector<bool> &value);
160 
164  variant(const std::initializer_list<datetime> &value);
165 
169  variant(const std::vector<datetime> &value);
170 
174  variant(const std::initializer_list<variant> &value);
175 
179  variant(const std::vector<variant> &value);
180 
184  bool is(type t) const;
185 
192  template <typename T>
193  T get() const;
194 
198  type value_type() const;
199 
200  bool operator==(const variant &rhs) const;
201 
202  bool operator!=(const variant &rhs) const;
203 
204 private:
205  template<typename T>
206  void construct_vector_internal(const T &vec);
207 
208  template<typename T>
209  std::vector<T> get_vector_internal() const;
210 
211  type type_;
212  std::vector<variant> vector_value_;
213  std::int32_t i4_value_;
214  std::string lpstr_value_;
215 };
216 
222 template <>
223 XLNT_API bool variant::get() const;
224 
230 template <>
231 XLNT_API std::int32_t variant::get() const;
232 
240 template <>
241 XLNT_API std::string variant::get() const;
242 
248 template <>
249 XLNT_API datetime variant::get() const;
250 
258 template <>
259 XLNT_API std::vector<variant> variant::get() const;
260 
267 template <>
268 XLNT_API std::vector<bool> variant::get() const;
269 
276 template <>
277 XLNT_API std::vector<std::int32_t> variant::get() const;
278 
287 template <>
288 XLNT_API std::vector<std::string> variant::get() const;
289 
296 template <>
297 XLNT_API std::vector<datetime> variant::get() const;
298 
299 } // namespace xlnt
type
The possible types a variant can hold.
Definition: variant.hpp:48
Represents an object that can have variable type.
Definition: variant.hpp:40
T get() const
Returns the value of this variant as type T. Assumes that the variant is of type T (please call is() ...
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:36
bool operator!=(std::nullptr_t, const cell &cell)
Returns true if this cell is initialized.
bool operator==(std::nullptr_t, const cell &cell)
Returns true if this cell is uninitialized.
A datetime is a combination of a date and a time. IMPORTANT: The datetime could be in an empty/invali...
Definition: datetime.hpp:41
A date is a specific day specified in terms of a year, month, and day. It can also be initialized as ...
Definition: date.hpp:37