icinga2/base/variant.h

94 lines
3.2 KiB
C
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2012-04-18 15:22:25 +02:00
#ifndef VARIANT_H
#define VARIANT_H
namespace icinga
{
/**
* The type of a Variant object.
2012-05-18 22:21:28 +02:00
*
* @ingroup base
*/
2012-05-18 23:24:00 +02:00
enum VariantType
2012-04-18 15:22:25 +02:00
{
2012-05-18 23:24:00 +02:00
VariantEmpty, /**< Denotes that the Variant is empty. */
VariantInteger, /**< Denotes that the Variant is holding an integer. */
VariantString, /**< Denotes that the Variant is holding a string. */
VariantObject /**< Denotes that the Variant is holding an object
that inherits from the Object class. */
2012-04-18 15:22:25 +02:00
};
/**
* A type that can hold an arbitrary value.
2012-05-18 22:21:28 +02:00
*
* @ingroup base
*/
2012-04-18 15:22:25 +02:00
class I2_BASE_API Variant
{
private:
mutable VariantType m_Type; /**< The type of the Variant. */
mutable long m_IntegerValue; /**< The value of the Variant
if m_Type == VariantInteger */
mutable string m_StringValue; /**< The value of the Variant
if m_Type == VariantString */
mutable Object::Ptr m_ObjectValue; /**< The value of the Variant
if m_Type == VariantObject */
2012-04-18 15:22:25 +02:00
void Convert(VariantType newType) const;
public:
2012-04-20 13:52:32 +02:00
inline Variant(void) : m_Type(VariantEmpty) { }
2012-04-20 13:49:04 +02:00
2012-05-16 11:30:54 +02:00
inline Variant(int value)
: m_Type(VariantInteger), m_IntegerValue(value) { }
2012-05-10 13:12:25 +02:00
inline Variant(long value)
: m_Type(VariantInteger), m_IntegerValue(value) { }
2012-04-20 13:49:04 +02:00
2012-05-10 13:12:25 +02:00
inline Variant(const char *value)
: m_Type(VariantString), m_StringValue(string(value)) { }
2012-04-20 13:49:04 +02:00
2012-05-10 13:12:25 +02:00
inline Variant(string value)
: m_Type(VariantString), m_StringValue(value) { }
2012-04-20 13:49:04 +02:00
template<typename T>
2012-05-10 13:12:25 +02:00
Variant(const shared_ptr<T>& value)
: m_Type(VariantObject), m_ObjectValue(value) { }
2012-04-18 15:22:25 +02:00
VariantType GetType(void) const;
long GetInteger(void) const;
string GetString(void) const;
Object::Ptr GetObject(void) const;
2012-04-20 13:49:04 +02:00
bool IsEmpty(void) const;
2012-04-18 15:22:25 +02:00
operator long(void) const;
operator string(void) const;
operator Object::Ptr(void) const;
};
}
#endif /* VARIANT_H */