]> granicus.if.org Git - icinga2/blob - lib/base/type.hpp
Update copyright headers for 2016
[icinga2] / lib / base / type.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
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 <boost/function.hpp>
28 #include <vector>
29
30 namespace icinga
31 {
32
33 /* keep this in sync with tools/mkclass/classcompiler.hpp */
34 enum FieldAttribute
35 {
36         FAEphemeral = 1,
37         FAConfig = 2,
38         FAState = 4,
39         FARequired = 256,
40         FANavigation = 512,
41         FANoUserModify = 1024,
42         FANoUserView = 2048
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 I2_BASE_API Type : public Object
74 {
75 public:
76         DECLARE_OBJECT(Type);
77
78         virtual String ToString(void) const override;
79
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;
86
87         String GetPluralName(void) const;
88
89         Object::Ptr Instantiate(void) const;
90
91         bool IsAssignableFrom(const Type::Ptr& other) const;
92
93         bool IsAbstract(void) const;
94
95         Object::Ptr GetPrototype(void) 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
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;
103
104         virtual std::vector<String> GetLoadDependencies(void) const;
105         
106         typedef boost::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
107         virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
108
109 protected:
110         virtual ObjectFactory GetFactory(void) const = 0;
111
112 private:
113         Object::Ptr m_Prototype;
114 };
115
116 class I2_BASE_API TypeType : public Type
117 {
118 public:
119         DECLARE_PTR_TYPEDEFS(Type);
120
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;
127         
128         static Object::Ptr GetPrototype(void);
129
130 protected:
131         virtual ObjectFactory GetFactory(void) const override;
132 };
133
134 template<typename T>
135 class TypeImpl
136 {
137 };
138
139 #define REGISTER_TYPE(type) \
140         namespace { namespace UNIQUE_NAME(rt) { \
141                 void RegisterType ## type(void) \
142                 { \
143                         icinga::Type::Ptr t = new TypeImpl<type>(); \
144                         type::TypeInstance = t; \
145                         icinga::Type::Register(t); \
146                 } \
147                 \
148                 INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
149         } } \
150         DEFINE_TYPE_INSTANCE(type)
151
152 #define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
153         namespace { namespace UNIQUE_NAME(rt) { \
154                 void RegisterType ## type(void) \
155                 { \
156                         icinga::Type::Ptr t = new TypeImpl<type>(); \
157                         t->SetPrototype(prototype); \
158                         type::TypeInstance = t; \
159                         icinga::Type::Register(t); \
160                 } \
161                 \
162                 INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
163         } } \
164         DEFINE_TYPE_INSTANCE(type)
165
166 #define DEFINE_TYPE_INSTANCE(type) \
167         Type::Ptr type::TypeInstance
168
169 }
170
171 #endif /* TYPE_H */