1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
29 * A type that can hold an arbitrary value.
33 class I2_BASE_API Value
40 inline Value(int value)
44 inline Value(long value)
45 : m_Value(double(value))
48 inline Value(double value)
52 inline Value(const String& value)
56 inline Value(const char *value)
57 : m_Value(String(value))
61 inline Value(const shared_ptr<T>& value)
67 Object::Ptr object = dynamic_pointer_cast<Object>(value);
70 throw_exception(invalid_argument("shared_ptr value type must inherit from Object class."));
75 operator double(void) const
77 if (m_Value.type() != typeid(double)) {
78 return boost::lexical_cast<double>(m_Value);
80 return boost::get<double>(m_Value);
84 operator String(void) const
89 if (m_Value.type() != typeid(String)) {
90 String result = boost::lexical_cast<String>(m_Value);
94 return boost::get<String>(m_Value);
98 operator shared_ptr<T>(void) const
101 return shared_ptr<T>();
103 shared_ptr<T> object = dynamic_pointer_cast<T>(boost::get<Object::Ptr>(m_Value));
106 throw_exception(bad_cast());
111 bool IsEmpty(void) const;
112 bool IsScalar(void) const;
113 bool IsObject(void) const;
116 bool IsObjectType(void) const
121 return (dynamic_pointer_cast<T>(boost::get<Object::Ptr>(m_Value)));
124 static Value FromJson(cJSON *json);
125 cJSON *ToJson(void) const;
127 String Serialize(void) const;
128 static Value Deserialize(const String& jsonString);
131 mutable boost::variant<boost::blank, double, String, Object::Ptr> m_Value;