1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
6 #include "base/i2-base.hpp"
7 #include "base/string.hpp"
8 #include "base/object.hpp"
9 #include "base/initialize.hpp"
15 /* keep this in sync with tools/mkclass/classcompiler.hpp */
23 FANoUserModify = 1024,
35 const char *NavigationName;
36 const char *RefTypeName;
40 Field(int id, const char *type, const char *name, const char *navigationName, const char *reftype, int attributes, int arrayRank)
41 : ID(id), TypeName(type), Name(name), NavigationName(navigationName), RefTypeName(reftype), Attributes(attributes), ArrayRank(arrayRank)
53 virtual bool ValidateName(const String& type, const String& name) const = 0;
56 class Type : public Object
61 String ToString() const override;
63 virtual String GetName() const = 0;
64 virtual Type::Ptr GetBaseType() const = 0;
65 virtual int GetAttributes() const = 0;
66 virtual int GetFieldId(const String& name) const = 0;
67 virtual Field GetFieldInfo(int id) const = 0;
68 virtual int GetFieldCount() const = 0;
70 String GetPluralName() const;
72 Object::Ptr Instantiate(const std::vector<Value>& args) const;
74 bool IsAssignableFrom(const Type::Ptr& other) const;
76 bool IsAbstract() const;
78 Object::Ptr GetPrototype() const;
79 void SetPrototype(const Object::Ptr& object);
81 static void Register(const Type::Ptr& type);
82 static Type::Ptr GetByName(const String& name);
83 static std::vector<Type::Ptr> GetAllTypes();
85 void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
86 Value GetField(int id) const override;
88 virtual std::vector<String> GetLoadDependencies() const;
89 virtual int GetActivationPriority() const;
91 typedef std::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
92 virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
95 virtual ObjectFactory GetFactory() const = 0;
98 Object::Ptr m_Prototype;
101 class TypeType final : public Type
104 DECLARE_PTR_TYPEDEFS(Type);
106 String GetName() const override;
107 Type::Ptr GetBaseType() const override;
108 int GetAttributes() const override;
109 int GetFieldId(const String& name) const override;
110 Field GetFieldInfo(int id) const override;
111 int GetFieldCount() const override;
113 static Object::Ptr GetPrototype();
116 ObjectFactory GetFactory() const override;
124 /* Ensure that the priority is lower than the basic namespace initialization in scriptframe.cpp. */
125 #define REGISTER_TYPE(type) \
126 INITIALIZE_ONCE_WITH_PRIORITY([]() { \
127 icinga::Type::Ptr t = new TypeImpl<type>(); \
128 type::TypeInstance = t; \
129 icinga::Type::Register(t); \
131 DEFINE_TYPE_INSTANCE(type)
133 #define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
134 INITIALIZE_ONCE_WITH_PRIORITY([]() { \
135 icinga::Type::Ptr t = new TypeImpl<type>(); \
136 t->SetPrototype(prototype); \
137 type::TypeInstance = t; \
138 icinga::Type::Register(t); \
140 DEFINE_TYPE_INSTANCE(type)
142 #define DEFINE_TYPE_INSTANCE(type) \
143 Type::Ptr type::TypeInstance