]> granicus.if.org Git - icinga2/blob - lib/base/value.h
5c52e79cc44631598299cf02a4782d3b661a22cf
[icinga2] / lib / base / value.h
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef VALUE_H
21 #define VALUE_H
22
23 struct cJSON;
24
25 namespace icinga
26 {
27
28 /**
29  * A type that can hold an arbitrary value.
30  *
31  * @ingroup base
32  */
33 class I2_BASE_API Value
34 {
35 public:
36         inline Value(void)
37                 : m_Value()
38         { }
39
40         inline Value(int value)
41                 : m_Value(value)
42         { }
43
44         inline Value(long value)
45                 : m_Value(double(value))
46         { }
47
48         inline Value(double value)
49                 : m_Value(value)
50         { }
51
52         inline Value(const String& value)
53                 : m_Value(value)
54         { }
55
56         inline Value(const char *value)
57                 : m_Value(String(value))
58         { }
59
60         template<typename T>
61         inline Value(const shared_ptr<T>& value)
62                 : m_Value()
63         {
64                 if (!value)
65                         return;
66
67                 Object::Ptr object = dynamic_pointer_cast<Object>(value);
68
69                 if (!object)
70                         throw_exception(invalid_argument("shared_ptr value type must inherit from Object class."));
71
72                 m_Value = object;
73         }
74
75         operator double(void) const
76         {
77                 if (m_Value.type() != typeid(double)) {
78                         return boost::lexical_cast<double>(m_Value);
79                 } else {
80                         return boost::get<double>(m_Value);
81                 }
82         }
83
84         operator String(void) const
85         {
86                 if (IsEmpty())
87                         return String();
88
89                 if (m_Value.type() != typeid(String)) {
90                         String result = boost::lexical_cast<String>(m_Value);
91                         m_Value = result;
92                 }
93
94                 return boost::get<String>(m_Value);
95         }
96
97         template<typename T>
98         operator shared_ptr<T>(void) const
99         {
100                 if (IsEmpty())
101                         return shared_ptr<T>();
102
103                 shared_ptr<T> object = dynamic_pointer_cast<T>(boost::get<Object::Ptr>(m_Value));
104
105                 if (!object)
106                         throw_exception(bad_cast());
107
108                 return object;
109         }
110
111         bool IsEmpty(void) const;
112         bool IsScalar(void) const;
113         bool IsObject(void) const;
114
115         template<typename T>
116         bool IsObjectType(void) const
117         {
118                 if (!IsObject())
119                         return false;
120
121                 return (dynamic_pointer_cast<T>(boost::get<Object::Ptr>(m_Value)));
122         }
123
124         static Value FromJson(cJSON *json);
125         cJSON *ToJson(void) const;
126
127         String Serialize(void) const;
128         static Value Deserialize(const String& jsonString);
129
130 private:
131         mutable boost::variant<boost::blank, double, String, Object::Ptr> m_Value;
132 };
133
134 static Value Empty;
135
136 }
137
138 #endif /* VALUE_H */