]> granicus.if.org Git - icinga2/blob - lib/base/type.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / type.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef TYPE_H
4 #define TYPE_H
5
6 #include "base/i2-base.hpp"
7 #include "base/string.hpp"
8 #include "base/object.hpp"
9 #include "base/initialize.hpp"
10 #include <vector>
11
12 namespace icinga
13 {
14
15 /* keep this in sync with tools/mkclass/classcompiler.hpp */
16 enum FieldAttribute
17 {
18         FAEphemeral = 1,
19         FAConfig = 2,
20         FAState = 4,
21         FARequired = 256,
22         FANavigation = 512,
23         FANoUserModify = 1024,
24         FANoUserView = 2048,
25         FADeprecated = 4096,
26 };
27
28 class Type;
29
30 struct Field
31 {
32         int ID;
33         const char *TypeName;
34         const char *Name;
35         const char *NavigationName;
36         const char *RefTypeName;
37         int Attributes;
38         int ArrayRank;
39
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)
42         { }
43 };
44
45 enum TypeAttribute
46 {
47         TAAbstract = 1
48 };
49
50 class ValidationUtils
51 {
52 public:
53         virtual bool ValidateName(const String& type, const String& name) const = 0;
54 };
55
56 class Type : public Object
57 {
58 public:
59         DECLARE_OBJECT(Type);
60
61         String ToString() const override;
62
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;
69
70         String GetPluralName() const;
71
72         Object::Ptr Instantiate(const std::vector<Value>& args) const;
73
74         bool IsAssignableFrom(const Type::Ptr& other) const;
75
76         bool IsAbstract() const;
77
78         Object::Ptr GetPrototype() const;
79         void SetPrototype(const Object::Ptr& object);
80
81         static void Register(const Type::Ptr& type);
82         static Type::Ptr GetByName(const String& name);
83         static std::vector<Type::Ptr> GetAllTypes();
84
85         void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
86         Value GetField(int id) const override;
87
88         virtual std::vector<String> GetLoadDependencies() const;
89         virtual int GetActivationPriority() const;
90
91         typedef std::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
92         virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
93
94 protected:
95         virtual ObjectFactory GetFactory() const = 0;
96
97 private:
98         Object::Ptr m_Prototype;
99 };
100
101 class TypeType final : public Type
102 {
103 public:
104         DECLARE_PTR_TYPEDEFS(Type);
105
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;
112
113         static Object::Ptr GetPrototype();
114
115 protected:
116         ObjectFactory GetFactory() const override;
117 };
118
119 template<typename T>
120 class TypeImpl
121 {
122 };
123
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); \
130         }, 10); \
131         DEFINE_TYPE_INSTANCE(type)
132
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); \
139         }, 10); \
140         DEFINE_TYPE_INSTANCE(type)
141
142 #define DEFINE_TYPE_INSTANCE(type) \
143         Type::Ptr type::TypeInstance
144
145 }
146
147 #endif /* TYPE_H */