/******************************************************************************
* Icinga 2 *
- * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
+ * Copyright (C) 2012-2015 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 *
#define TYPE_H
#include "base/i2-base.hpp"
-#include "base/qstring.hpp"
+#include "base/string.hpp"
#include "base/object.hpp"
#include "base/initialize.hpp"
-#include "base/utility.hpp"
-#include <boost/function.hpp>
+#include <vector>
namespace icinga
{
+/* keep this in sync with tools/mkclass/classcompiler.hpp */
+enum FieldAttribute
+{
+ FAEphemeral = 1,
+ FAConfig = 2,
+ FAState = 4,
+ FAInternal = 64,
+ FARequired = 512
+};
+
+class Type;
+
struct Field
{
int ID;
+ const char *TypeName;
const char *Name;
+ const char *RefTypeName;
int Attributes;
- Field(int id, const char *name, int attributes)
- : ID(id), Name(name), Attributes(attributes)
+ Field(int id, const char *type, const char *name, const char *reftype, int attributes)
+ : ID(id), TypeName(type), Name(name), RefTypeName(reftype), Attributes(attributes)
{ }
};
enum TypeAttribute
{
- TAAbstract = 1,
- TASafe = 2
+ TAAbstract = 1
};
-class I2_BASE_API Type
+class ValidationUtils
{
public:
- typedef boost::function<Object::Ptr (void)> Factory;
+ virtual bool ValidateName(const String& type, const String& name) const = 0;
+};
+
+class I2_BASE_API Type : public Object
+{
+public:
+ DECLARE_PTR_TYPEDEFS(Type);
+
+ virtual String ToString(void) const;
virtual String GetName(void) const = 0;
- virtual const Type *GetBaseType(void) const = 0;
+ virtual Type::Ptr GetBaseType(void) const = 0;
virtual int GetAttributes(void) const = 0;
virtual int GetFieldId(const String& name) const = 0;
virtual Field GetFieldInfo(int id) const = 0;
Object::Ptr Instantiate(void) const;
- bool IsAssignableFrom(const Type *other) const;
+ bool IsAssignableFrom(const Type::Ptr& other) const;
bool IsAbstract(void) const;
- bool IsSafe(void) const;
- static void Register(const Type *type);
- static const Type *GetByName(const String& name);
+ Object::Ptr GetPrototype(void) const;
+ void SetPrototype(const Object::Ptr& object);
- void SetFactory(const Factory& factory);
+ static void Register(const Type::Ptr& type);
+ static Type::Ptr GetByName(const String& name);
-private:
- typedef std::map<String, const Type *> TypeMap;
+ virtual void SetField(int id, const Value& value);
+ virtual Value GetField(int id) const;
- static TypeMap& GetTypes(void);
+ virtual std::vector<String> GetLoadDependencies(void) const;
- Factory m_Factory;
-};
+protected:
+ virtual ObjectFactory GetFactory(void) const = 0;
-template<typename T>
-class TypeImpl
-{
+private:
+ Object::Ptr m_Prototype;
};
-template<typename T>
-shared_ptr<T> ObjectFactory(void)
+class I2_BASE_API TypeType : public Type
{
- return make_shared<T>();
-}
+public:
+ DECLARE_PTR_TYPEDEFS(Type);
+
+ virtual String GetName(void) const;
+ virtual Type::Ptr GetBaseType(void) const;
+ virtual int GetAttributes(void) const;
+ virtual int GetFieldId(const String& name) const;
+ virtual Field GetFieldInfo(int id) const;
+ virtual int GetFieldCount(void) const;
+
+protected:
+ virtual ObjectFactory GetFactory(void) const;
+};
template<typename T>
-struct FactoryHelper
+class TypeImpl
{
- Type::Factory GetFactory(void)
- {
- return ObjectFactory<T>;
- }
};
#define REGISTER_TYPE(type) \
namespace { namespace UNIQUE_NAME(rt) { \
- void RegisterType(void) \
+ void RegisterType ## type(void) \
{ \
- icinga::Type *t = new TypeImpl<type>(); \
- t->SetFactory(FactoryHelper<type>().GetFactory()); \
+ icinga::Type::Ptr t = new TypeImpl<type>(); \
+ type::TypeInstance = t; \
icinga::Type::Register(t); \
} \
\
- INITIALIZE_ONCE(RegisterType); \
- } }
+ INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
+ } } \
+ DEFINE_TYPE_INSTANCE(type)
+
+#define DEFINE_TYPE_INSTANCE(type) \
+ Type::Ptr type::TypeInstance
}