1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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"
32 /* keep this in sync with tools/mkclass/classcompiler.hpp */
40 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 Type : public Object
78 String ToString() const override;
80 virtual String GetName() const = 0;
81 virtual Type::Ptr GetBaseType() const = 0;
82 virtual int GetAttributes() const = 0;
83 virtual int GetFieldId(const String& name) const = 0;
84 virtual Field GetFieldInfo(int id) const = 0;
85 virtual int GetFieldCount() const = 0;
87 String GetPluralName() const;
89 Object::Ptr Instantiate(const std::vector<Value>& args) const;
91 bool IsAssignableFrom(const Type::Ptr& other) const;
93 bool IsAbstract() const;
95 Object::Ptr GetPrototype() const;
96 void SetPrototype(const Object::Ptr& object);
98 static void Register(const Type::Ptr& type);
99 static Type::Ptr GetByName(const String& name);
100 static std::vector<Type::Ptr> GetAllTypes();
102 void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
103 Value GetField(int id) const override;
105 virtual std::vector<String> GetLoadDependencies() const;
106 virtual int GetActivationPriority() const;
108 typedef std::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
109 virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
112 virtual ObjectFactory GetFactory() const = 0;
115 Object::Ptr m_Prototype;
118 class TypeType final : public Type
121 DECLARE_PTR_TYPEDEFS(Type);
123 String GetName() const override;
124 Type::Ptr GetBaseType() const override;
125 int GetAttributes() const override;
126 int GetFieldId(const String& name) const override;
127 Field GetFieldInfo(int id) const override;
128 int GetFieldCount() const override;
130 static Object::Ptr GetPrototype();
133 ObjectFactory GetFactory() const override;
141 /* Ensure that the priority is lower than the basic namespace initialization in scriptframe.cpp. */
142 #define REGISTER_TYPE(type) \
143 INITIALIZE_ONCE_WITH_PRIORITY([]() { \
144 icinga::Type::Ptr t = new TypeImpl<type>(); \
145 type::TypeInstance = t; \
146 icinga::Type::Register(t); \
148 DEFINE_TYPE_INSTANCE(type)
150 #define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
151 INITIALIZE_ONCE_WITH_PRIORITY([]() { \
152 icinga::Type::Ptr t = new TypeImpl<type>(); \
153 t->SetPrototype(prototype); \
154 type::TypeInstance = t; \
155 icinga::Type::Register(t); \
157 DEFINE_TYPE_INSTANCE(type)
159 #define DEFINE_TYPE_INSTANCE(type) \
160 Type::Ptr type::TypeInstance