1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://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 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/string.hpp"
25 #include "base/object.hpp"
26 #include "base/initialize.hpp"
27 #include <boost/function.hpp>
33 /* keep this in sync with tools/mkclass/classcompiler.hpp */
41 FANoUserModify = 1024,
52 const char *NavigationName;
53 const char *RefTypeName;
57 Field(int id, const char *type, const char *name, const char *navigationName, const char *reftype, int attributes, int arrayRank)
58 : ID(id), TypeName(type), Name(name), NavigationName(navigationName), RefTypeName(reftype), Attributes(attributes), ArrayRank(arrayRank)
70 virtual bool ValidateName(const String& type, const String& name) const = 0;
73 class I2_BASE_API Type : public Object
78 virtual String ToString(void) const override;
80 virtual String GetName(void) const = 0;
81 virtual Type::Ptr GetBaseType(void) const = 0;
82 virtual int GetAttributes(void) const = 0;
83 virtual int GetFieldId(const String& name) const = 0;
84 virtual Field GetFieldInfo(int id) const = 0;
85 virtual int GetFieldCount(void) const = 0;
87 String GetPluralName(void) const;
89 Object::Ptr Instantiate(void) const;
91 bool IsAssignableFrom(const Type::Ptr& other) const;
93 bool IsAbstract(void) const;
95 Object::Ptr GetPrototype(void) const;
96 void SetPrototype(const Object::Ptr& object);
98 static void Register(const Type::Ptr& type);
99 static Type::Ptr GetByName(const String& name);
101 virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
102 virtual Value GetField(int id) const override;
104 virtual std::vector<String> GetLoadDependencies(void) const;
106 typedef boost::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
107 virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
110 virtual ObjectFactory GetFactory(void) const = 0;
113 Object::Ptr m_Prototype;
116 class I2_BASE_API TypeType : public Type
119 DECLARE_PTR_TYPEDEFS(Type);
121 virtual String GetName(void) const override;
122 virtual Type::Ptr GetBaseType(void) const override;
123 virtual int GetAttributes(void) const override;
124 virtual int GetFieldId(const String& name) const override;
125 virtual Field GetFieldInfo(int id) const override;
126 virtual int GetFieldCount(void) const override;
128 static Object::Ptr GetPrototype(void);
131 virtual ObjectFactory GetFactory(void) const override;
139 #define REGISTER_TYPE(type) \
140 namespace { namespace UNIQUE_NAME(rt) { \
141 void RegisterType ## type(void) \
143 icinga::Type::Ptr t = new TypeImpl<type>(); \
144 type::TypeInstance = t; \
145 icinga::Type::Register(t); \
148 INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
150 DEFINE_TYPE_INSTANCE(type)
152 #define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
153 namespace { namespace UNIQUE_NAME(rt) { \
154 void RegisterType ## type(void) \
156 icinga::Type::Ptr t = new TypeImpl<type>(); \
157 t->SetPrototype(prototype); \
158 type::TypeInstance = t; \
159 icinga::Type::Register(t); \
162 INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
164 DEFINE_TYPE_INSTANCE(type)
166 #define DEFINE_TYPE_INSTANCE(type) \
167 Type::Ptr type::TypeInstance