2013-10-26 09:41:45 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-10-26 09:41:45 +02:00
|
|
|
* *
|
|
|
|
* 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 *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
#ifndef TYPE_H
|
|
|
|
#define TYPE_H
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
2014-10-19 14:48:19 +02:00
|
|
|
#include "base/string.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/object.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
2015-08-04 14:47:44 +02:00
|
|
|
#include <boost/function.hpp>
|
2015-02-25 14:03:15 +01:00
|
|
|
#include <vector>
|
2013-10-26 09:41:45 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2015-03-11 16:06:58 +01:00
|
|
|
/* keep this in sync with tools/mkclass/classcompiler.hpp */
|
2014-10-26 19:59:49 +01:00
|
|
|
enum FieldAttribute
|
|
|
|
{
|
2015-03-11 16:06:58 +01:00
|
|
|
FAEphemeral = 1,
|
|
|
|
FAConfig = 2,
|
|
|
|
FAState = 4,
|
|
|
|
FAInternal = 64,
|
2014-11-30 23:32:13 +01:00
|
|
|
FARequired = 512
|
2014-10-26 19:59:49 +01:00
|
|
|
};
|
2014-10-31 08:49:14 +01:00
|
|
|
|
|
|
|
class Type;
|
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
struct Field
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
int ID;
|
2014-11-10 20:06:28 +01:00
|
|
|
const char *TypeName;
|
2013-12-03 09:59:21 +01:00
|
|
|
const char *Name;
|
2014-11-30 23:32:13 +01:00
|
|
|
const char *RefTypeName;
|
2013-10-26 09:41:45 +02:00
|
|
|
int Attributes;
|
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
Field(int id, const char *type, const char *name, const char *reftype, int attributes)
|
|
|
|
: ID(id), TypeName(type), Name(name), RefTypeName(reftype), Attributes(attributes)
|
2013-10-26 09:41:45 +02:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
enum TypeAttribute
|
|
|
|
{
|
2014-10-25 09:14:56 +02:00
|
|
|
TAAbstract = 1
|
2013-12-18 10:18:57 +01:00
|
|
|
};
|
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
class ValidationUtils
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool ValidateName(const String& type, const String& name) const = 0;
|
|
|
|
};
|
|
|
|
|
2014-11-03 00:44:04 +01:00
|
|
|
class I2_BASE_API Type : public Object
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-08-04 14:47:44 +02:00
|
|
|
DECLARE_OBJECT(Type);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2015-08-18 07:46:04 +02:00
|
|
|
virtual String ToString(void) const override;
|
2014-12-08 09:12:40 +01:00
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
virtual String GetName(void) const = 0;
|
2014-11-03 00:44:04 +01:00
|
|
|
virtual Type::Ptr GetBaseType(void) const = 0;
|
2013-12-18 10:18:57 +01:00
|
|
|
virtual int GetAttributes(void) const = 0;
|
2013-10-26 09:41:45 +02:00
|
|
|
virtual int GetFieldId(const String& name) const = 0;
|
2013-11-04 23:14:34 +01:00
|
|
|
virtual Field GetFieldInfo(int id) const = 0;
|
2013-10-26 09:41:45 +02:00
|
|
|
virtual int GetFieldCount(void) const = 0;
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
String GetPluralName(void) const;
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
Object::Ptr Instantiate(void) const;
|
|
|
|
|
2014-11-03 00:44:04 +01:00
|
|
|
bool IsAssignableFrom(const Type::Ptr& other) const;
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
bool IsAbstract(void) const;
|
|
|
|
|
2014-12-12 15:19:23 +01:00
|
|
|
Object::Ptr GetPrototype(void) const;
|
|
|
|
void SetPrototype(const Object::Ptr& object);
|
|
|
|
|
2014-11-03 00:44:04 +01:00
|
|
|
static void Register(const Type::Ptr& type);
|
|
|
|
static Type::Ptr GetByName(const String& name);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2015-08-18 07:46:04 +02:00
|
|
|
virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
|
|
|
|
virtual Value GetField(int id) const override;
|
2015-01-14 09:51:44 +01:00
|
|
|
|
2015-02-25 12:43:03 +01:00
|
|
|
virtual std::vector<String> GetLoadDependencies(void) const;
|
2015-08-04 14:47:44 +02:00
|
|
|
|
|
|
|
typedef boost::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
|
|
|
|
virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
|
2015-02-25 12:43:03 +01:00
|
|
|
|
2014-11-07 12:32:25 +01:00
|
|
|
protected:
|
|
|
|
virtual ObjectFactory GetFactory(void) const = 0;
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Object::Ptr m_Prototype;
|
2013-11-08 11:17:46 +01:00
|
|
|
};
|
|
|
|
|
2015-01-14 09:51:44 +01:00
|
|
|
class I2_BASE_API TypeType : public Type
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DECLARE_PTR_TYPEDEFS(Type);
|
|
|
|
|
2015-08-18 09:12:49 +02:00
|
|
|
virtual String GetName(void) const override;
|
|
|
|
virtual Type::Ptr GetBaseType(void) const override;
|
|
|
|
virtual int GetAttributes(void) const override;
|
|
|
|
virtual int GetFieldId(const String& name) const override;
|
|
|
|
virtual Field GetFieldInfo(int id) const override;
|
|
|
|
virtual int GetFieldCount(void) const override;
|
2015-08-04 14:47:44 +02:00
|
|
|
|
|
|
|
static Object::Ptr GetPrototype(void);
|
2015-01-14 09:51:44 +01:00
|
|
|
|
|
|
|
protected:
|
2015-08-18 09:12:49 +02:00
|
|
|
virtual ObjectFactory GetFactory(void) const override;
|
2015-01-14 09:51:44 +01:00
|
|
|
};
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
template<typename T>
|
|
|
|
class TypeImpl
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2013-11-08 16:07:21 +01:00
|
|
|
#define REGISTER_TYPE(type) \
|
2014-08-31 11:01:37 +02:00
|
|
|
namespace { namespace UNIQUE_NAME(rt) { \
|
2014-10-31 08:49:14 +01:00
|
|
|
void RegisterType ## type(void) \
|
2013-11-08 11:17:46 +01:00
|
|
|
{ \
|
2014-11-08 21:17:16 +01:00
|
|
|
icinga::Type::Ptr t = new TypeImpl<type>(); \
|
2014-11-07 12:32:25 +01:00
|
|
|
type::TypeInstance = t; \
|
2013-11-08 21:37:21 +01:00
|
|
|
icinga::Type::Register(t); \
|
2013-11-08 11:17:46 +01:00
|
|
|
} \
|
|
|
|
\
|
2015-03-18 13:24:31 +01:00
|
|
|
INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
|
2014-11-07 12:32:25 +01:00
|
|
|
} } \
|
|
|
|
DEFINE_TYPE_INSTANCE(type)
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
#define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
|
|
|
|
namespace { namespace UNIQUE_NAME(rt) { \
|
|
|
|
void RegisterType ## type(void) \
|
|
|
|
{ \
|
|
|
|
icinga::Type::Ptr t = new TypeImpl<type>(); \
|
|
|
|
t->SetPrototype(prototype); \
|
|
|
|
type::TypeInstance = t; \
|
|
|
|
icinga::Type::Register(t); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
|
|
|
|
} } \
|
|
|
|
DEFINE_TYPE_INSTANCE(type)
|
|
|
|
|
2014-11-07 12:32:25 +01:00
|
|
|
#define DEFINE_TYPE_INSTANCE(type) \
|
2014-11-08 21:17:16 +01:00
|
|
|
Type::Ptr type::TypeInstance
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
#endif /* TYPE_H */
|