]> granicus.if.org Git - icinga2/blob - lib/base/type.hpp
Merge pull request #6718 from Icinga/bugfix/ssl-shutdown
[icinga2] / lib / base / type.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef TYPE_H
21 #define TYPE_H
22
23 #include "base/i2-base.hpp"
24 #include "base/string.hpp"
25 #include "base/object.hpp"
26 #include "base/initialize.hpp"
27 #include <vector>
28
29 namespace icinga
30 {
31
32 /* keep this in sync with tools/mkclass/classcompiler.hpp */
33 enum FieldAttribute
34 {
35         FAEphemeral = 1,
36         FAConfig = 2,
37         FAState = 4,
38         FARequired = 256,
39         FANavigation = 512,
40         FANoUserModify = 1024,
41         FANoUserView = 2048,
42         FADeprecated = 4096,
43 };
44
45 class Type;
46
47 struct Field
48 {
49         int ID;
50         const char *TypeName;
51         const char *Name;
52         const char *NavigationName;
53         const char *RefTypeName;
54         int Attributes;
55         int ArrayRank;
56
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)
59         { }
60 };
61
62 enum TypeAttribute
63 {
64         TAAbstract = 1
65 };
66
67 class ValidationUtils
68 {
69 public:
70         virtual bool ValidateName(const String& type, const String& name) const = 0;
71 };
72
73 class Type : public Object
74 {
75 public:
76         DECLARE_OBJECT(Type);
77
78         String ToString() const override;
79
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;
86
87         String GetPluralName() const;
88
89         Object::Ptr Instantiate(const std::vector<Value>& args) const;
90
91         bool IsAssignableFrom(const Type::Ptr& other) const;
92
93         bool IsAbstract() const;
94
95         Object::Ptr GetPrototype() const;
96         void SetPrototype(const Object::Ptr& object);
97
98         static void Register(const Type::Ptr& type);
99         static Type::Ptr GetByName(const String& name);
100         static std::vector<Type::Ptr> GetAllTypes();
101
102         void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
103         Value GetField(int id) const override;
104
105         virtual std::vector<String> GetLoadDependencies() const;
106         virtual int GetActivationPriority() const;
107
108         typedef std::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
109         virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
110
111 protected:
112         virtual ObjectFactory GetFactory() const = 0;
113
114 private:
115         Object::Ptr m_Prototype;
116 };
117
118 class TypeType final : public Type
119 {
120 public:
121         DECLARE_PTR_TYPEDEFS(Type);
122
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;
129
130         static Object::Ptr GetPrototype();
131
132 protected:
133         ObjectFactory GetFactory() const override;
134 };
135
136 template<typename T>
137 class TypeImpl
138 {
139 };
140
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); \
147         }, 10); \
148         DEFINE_TYPE_INSTANCE(type)
149
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); \
156         }, 10); \
157         DEFINE_TYPE_INSTANCE(type)
158
159 #define DEFINE_TYPE_INSTANCE(type) \
160         Type::Ptr type::TypeInstance
161
162 }
163
164 #endif /* TYPE_H */