Unity Scopes API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Variant.h
1 /*
2  * Copyright (C) 2013 Canonical Ltd
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Michi Henning <michi.henning@canonical.com>
17  */
18 
19 #ifndef UNITY_SCOPES_VARIANT_H
20 #define UNITY_SCOPES_VARIANT_H
21 
22 #include <memory>
23 #include <string>
24 #include <map>
25 #include <vector>
26 
27 namespace unity
28 {
29 
30 namespace scopes
31 {
32 
33 class Variant;
34 
39 typedef std::map<std::string, Variant> VariantMap;
40 
44 typedef std::vector<Variant> VariantArray;
45 
46 namespace internal
47 {
48 
49 struct VariantImpl;
50 struct NullVariant;
51 
52 } // namespace internal
53 
58 class Variant final // LCOV_EXCL_LINE // lcov incorrectly reports this line as uncovered
59 {
60 public:
64  enum Type { Null, Int, Bool, String, Double, Dict, Array };
65 
71  Variant() noexcept;
72 
76  explicit Variant(int val) noexcept;
77 
81  explicit Variant(double val) noexcept;
82 
86  explicit Variant(bool val) noexcept;
87 
91  explicit Variant(std::string const& val);
92 
96  explicit Variant(char const* val); // Required to prevent Variant("Hello") from storing a bool
97 
98  explicit Variant(VariantMap const& val);
99 
100  explicit Variant(VariantArray const& val);
101 
105  static Variant const& null();
106 
110  ~Variant();
111 
115  //{@
116  Variant(Variant const&);
117  Variant(Variant&&);
118  Variant& operator=(Variant const&);
119  Variant& operator=(Variant&&);
121 
127  //{@
128  Variant& operator=(int val) noexcept;
129  Variant& operator=(double val) noexcept;
130  Variant& operator=(bool val) noexcept;
131  Variant& operator=(std::string const& val);
132  Variant& operator=(char const* val); // Required to prevent v = "Hello" from storing a bool
133  Variant& operator=(VariantMap const& val);
134  Variant& operator=(VariantArray const& val);
136 
144  //{@
145  bool operator==(Variant const&) const noexcept;
146  bool operator<(Variant const&) const noexcept;
148 
154  //{@
155  int get_int() const;
156  double get_double() const;
157  bool get_bool() const;
158  std::string get_string() const;
159  VariantMap get_dict() const;
160  VariantArray get_array() const;
161 
166  bool is_null() const;
168 
174  Type which() const noexcept;
175 
181  void swap(Variant& other) noexcept;
182 
189  std::string serialize_json() const;
193  static Variant deserialize_json(std::string const& json_string);
195 
196 private:
197  Variant(internal::NullVariant const&);
198 
199  std::unique_ptr<internal::VariantImpl> p;
200  friend class VariantImpl;
201 };
202 
206 void swap(Variant&, Variant&) noexcept;
207 
208 } // namespace scopes
209 
210 } // namespace unity
211 
212 #endif
std::vector< Variant > VariantArray
An array of variants.
Definition: Variant.h:44
Simple variant class that can hold an integer, boolean, string, double, dictionary, array or null value.
Definition: Variant.h:58
bool is_null() const
Test if variant holds null value.
Definition: Variant.cpp:251
void swap(Variant &other) noexcept
Swaps the contents of this Variant with other.
Definition: Variant.cpp:262
Variant() noexcept
The default constructor creates a Variant instance containing a null.
Definition: Variant.cpp:57
static Variant deserialize_json(std::string const &json_string)
Deserializes a JSON encoded string to a Variant.
Definition: Variant.cpp:278
std::map< std::string, Variant > VariantMap
A dictionary of (string, Variant) pairs.
Definition: Variant.h:39
Type
Type of value held by a Variant instance.
Definition: Variant.h:64
std::string serialize_json() const
Serializes the variant to a JSON encoded string.
Definition: Variant.cpp:272
Type which() const noexcept
Returns the type of value currently stored by this Variant.
Definition: Variant.cpp:256
static Variant const & null()
Construct a null variant.
Definition: Variant.cpp:106