Unity Scopes API
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 #pragma once
20 
21 #include <memory>
22 #include <string>
23 #include <map>
24 #include <vector>
25 
26 namespace unity
27 {
28 
29 namespace scopes
30 {
31 
32 class Variant;
33 
38 typedef std::map<std::string, Variant> VariantMap;
39 
43 typedef std::vector<Variant> VariantArray;
44 
45 namespace internal
46 {
47 
48 struct VariantImpl;
49 struct NullVariant;
50 
51 } // namespace internal
52 
57 class Variant final // LCOV_EXCL_LINE // lcov incorrectly reports this line as uncovered
58 {
59 public:
63  enum Type { Null, Int, Bool, String, Double, Dict, Array, Int64 };
64 
70  Variant() noexcept;
71 
75  explicit Variant(int val) noexcept;
76 
77  explicit Variant(int64_t val) noexcept;
78 
82  explicit Variant(double val) noexcept;
83 
87  explicit Variant(bool val) noexcept;
88 
92  explicit Variant(std::string const& val);
93 
97  explicit Variant(char const* val); // Required to prevent Variant("Hello") from storing a bool
98 
99  explicit Variant(VariantMap const& val);
100 
101  explicit Variant(VariantArray const& val);
102 
106  static Variant const& null();
107 
111  ~Variant();
112 
116  //{@
117  Variant(Variant const&);
118  Variant(Variant&&);
119  Variant& operator=(Variant const&);
120  Variant& operator=(Variant&&);
122 
128  //{@
129  Variant& operator=(int val) noexcept;
130  Variant& operator=(int64_t val) noexcept;
131  Variant& operator=(double val) noexcept;
132  Variant& operator=(bool val) noexcept;
133  Variant& operator=(std::string const& val);
134  Variant& operator=(char const* val); // Required to prevent v = "Hello" from storing a bool
135  Variant& operator=(VariantMap const& val);
136  Variant& operator=(VariantArray const& val);
138 
146  //{@
147  bool operator==(Variant const&) const noexcept;
148  bool operator<(Variant const&) const noexcept;
150 
156  //{@
157  int get_int() const;
158  int64_t get_int64_t() const;
159  double get_double() const;
160  bool get_bool() const;
161  std::string get_string() const;
162  VariantMap get_dict() const;
163  VariantArray get_array() const;
164 
169  bool is_null() const;
171 
177  Type which() const noexcept;
178 
184  void swap(Variant& other) noexcept;
185 
192  std::string serialize_json() const;
196  static Variant deserialize_json(std::string const& json_string);
198 
199 private:
200  Variant(internal::NullVariant const&);
201 
202  std::unique_ptr<internal::VariantImpl> p;
203  friend class VariantImpl;
204 };
205 
209 void swap(Variant&, Variant&) noexcept;
210 
211 } // namespace scopes
212 
213 } // namespace unity
std::vector< Variant > VariantArray
An array of variants.
Definition: Variant.h:43
Simple variant class that can hold an integer, boolean, string, double, dictionary, array or null value.
Definition: Variant.h:57
bool is_null() const
Test if variant holds null value.
Definition: Variant.cpp:274
void swap(Variant &other) noexcept
Swaps the contents of this Variant with other.
Definition: Variant.cpp:285
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:301
Top-level namespace for all things Unity-related.
Definition: Version.h:49
std::map< std::string, Variant > VariantMap
A dictionary of (string, Variant) pairs.
Definition: Variant.h:38
Definition: ActionMetadata.h:31
Type
Type of value held by a Variant instance.
Definition: Variant.h:63
std::string serialize_json() const
Serializes the variant to a JSON encoded string.
Definition: Variant.cpp:295
Type which() const noexcept
Returns the type of value currently stored by this Variant.
Definition: Variant.cpp:279
static Variant const & null()
Construct a null variant.
Definition: Variant.cpp:111