xbmc
Variant.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <map>
12 #include <stdint.h>
13 #include <string>
14 #include <vector>
15 #include <wchar.h>
16 
17 int64_t str2int64(const std::string &str, int64_t fallback = 0);
18 int64_t str2int64(const std::wstring &str, int64_t fallback = 0);
19 uint64_t str2uint64(const std::string &str, uint64_t fallback = 0);
20 uint64_t str2uint64(const std::wstring &str, uint64_t fallback = 0);
21 double str2double(const std::string &str, double fallback = 0.0);
22 double str2double(const std::wstring &str, double fallback = 0.0);
23 
24 #ifdef TARGET_WINDOWS_STORE
25 #pragma pack(push)
26 #pragma pack(8)
27 #endif
28 
29 class CVariant
30 {
31 public:
32  enum VariantType
33  {
34  VariantTypeInteger,
35  VariantTypeUnsignedInteger,
36  VariantTypeBoolean,
37  VariantTypeString,
38  VariantTypeWideString,
39  VariantTypeDouble,
40  VariantTypeArray,
41  VariantTypeObject,
42  VariantTypeNull,
43  VariantTypeConstNull
44  };
45 
46  CVariant();
47  CVariant(VariantType type);
48  CVariant(int integer);
49  CVariant(int64_t integer);
50  CVariant(unsigned int unsignedinteger);
51  CVariant(uint64_t unsignedinteger);
52  CVariant(double value);
53  CVariant(float value);
54  CVariant(bool boolean);
55  CVariant(const char *str);
56  CVariant(const char *str, unsigned int length);
57  CVariant(const std::string &str);
58  CVariant(std::string &&str);
59  CVariant(const wchar_t *str);
60  CVariant(const wchar_t *str, unsigned int length);
61  CVariant(const std::wstring &str);
62  CVariant(std::wstring &&str);
63  CVariant(const std::vector<std::string> &strArray);
64  CVariant(const std::map<std::string, std::string> &strMap);
65  CVariant(const std::map<std::string, CVariant> &variantMap);
66  CVariant(const CVariant &variant);
67  CVariant(CVariant&& rhs) noexcept;
68  ~CVariant();
69 
70 
71 
72  bool isInteger() const;
73  bool isSignedInteger() const;
74  bool isUnsignedInteger() const;
75  bool isBoolean() const;
76  bool isString() const;
77  bool isWideString() const;
78  bool isDouble() const;
79  bool isArray() const;
80  bool isObject() const;
81  bool isNull() const;
82 
83  VariantType type() const;
84 
85  int64_t asInteger(int64_t fallback = 0) const;
86  int32_t asInteger32(int32_t fallback = 0) const;
87  uint64_t asUnsignedInteger(uint64_t fallback = 0u) const;
88  uint32_t asUnsignedInteger32(uint32_t fallback = 0u) const;
89  bool asBoolean(bool fallback = false) const;
90  std::string asString(const std::string &fallback = "") const;
91  std::wstring asWideString(const std::wstring &fallback = L"") const;
92  double asDouble(double fallback = 0.0) const;
93  float asFloat(float fallback = 0.0f) const;
94 
95  CVariant &operator[](const std::string &key);
96  const CVariant &operator[](const std::string &key) const;
97  CVariant &operator[](unsigned int position);
98  const CVariant &operator[](unsigned int position) const;
99 
100  CVariant &operator=(const CVariant &rhs);
101  CVariant& operator=(CVariant&& rhs) noexcept;
102  bool operator==(const CVariant &rhs) const;
103  bool operator!=(const CVariant &rhs) const { return !(*this == rhs); }
104 
105  void reserve(size_t length);
106  void push_back(const CVariant &variant);
107  void push_back(CVariant &&variant);
108  void append(const CVariant &variant);
109  void append(CVariant &&variant);
110 
111  const char *c_str() const;
112 
113  void swap(CVariant &rhs);
114 
115 private:
116  typedef std::vector<CVariant> VariantArray;
117  typedef std::map<std::string, CVariant> VariantMap;
118 
119 public:
120  typedef VariantArray::iterator iterator_array;
121  typedef VariantArray::const_iterator const_iterator_array;
122 
123  typedef VariantMap::iterator iterator_map;
124  typedef VariantMap::const_iterator const_iterator_map;
125 
126  iterator_array begin_array();
127  const_iterator_array begin_array() const;
128  iterator_array end_array();
129  const_iterator_array end_array() const;
130 
131  iterator_map begin_map();
132  const_iterator_map begin_map() const;
133  iterator_map end_map();
134  const_iterator_map end_map() const;
135 
136  unsigned int size() const;
137  bool empty() const;
138  void clear();
139  void erase(const std::string &key);
140  void erase(unsigned int position);
141 
142  bool isMember(const std::string &key) const;
143 
144  static CVariant ConstNullVariant;
145 
146 private:
147  void cleanup();
148  union VariantUnion
149  {
150  int64_t integer;
151  uint64_t unsignedinteger;
152  bool boolean;
153  double dvalue;
154  std::string *string;
155  std::wstring *wstring;
156  VariantArray *array;
157  VariantMap *map;
158  };
159 
160  VariantType m_type;
161  VariantUnion m_data;
162 
163  static VariantArray EMPTY_ARRAY;
164  static VariantMap EMPTY_MAP;
165 };
166 
167 #ifdef TARGET_WINDOWS_STORE
168 #pragma pack(pop)
169 #endif
170 
Definition: Variant.h:29