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 };
64 
70  Variant() noexcept;
71 
75  explicit Variant(int val) noexcept;
76 
80  explicit Variant(double val) noexcept;
81 
85  explicit Variant(bool val) noexcept;
86 
90  explicit Variant(std::string const& val);
91 
95  explicit Variant(char const* val); // Required to prevent Variant("Hello") from storing a bool
96 
97  explicit Variant(VariantMap const& val);
98 
99  explicit Variant(VariantArray const& val);
100 
104  static Variant const& null();
105 
109  ~Variant();
110 
114  //{@
115  Variant(Variant const&);
116  Variant(Variant&&);
117  Variant& operator=(Variant const&);
118  Variant& operator=(Variant&&);
120 
126  //{@
127  Variant& operator=(int val) noexcept;
128  Variant& operator=(double val) noexcept;
129  Variant& operator=(bool val) noexcept;
130  Variant& operator=(std::string const& val);
131  Variant& operator=(char const* val); // Required to prevent v = "Hello" from storing a bool
132  Variant& operator=(VariantMap const& val);
133  Variant& operator=(VariantArray const& val);
135 
143  //{@
144  bool operator==(Variant const&) const noexcept;
145  bool operator<(Variant const&) const noexcept;
147 
153  //{@
154  int get_int() const;
155  double get_double() const;
156  bool get_bool() const;
157  std::string get_string() const;
158  VariantMap get_dict() const;
159  VariantArray get_array() const;
160 
165  bool is_null() const;
167 
173  Type which() const noexcept;
174 
180  void swap(Variant& other) noexcept;
181 
188  std::string serialize_json() const;
192  static Variant deserialize_json(std::string const& json_string);
194 
195 private:
196  Variant(internal::NullVariant const&);
197 
198  std::unique_ptr<internal::VariantImpl> p;
199  friend class VariantImpl;
200 };
201 
205 void swap(Variant&, Variant&) noexcept;
206 
207 } // namespace scopes
208 
209 } // 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: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
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: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