]> granicus.if.org Git - icinga2/blob - lib/base/type.hpp
Make order for deferred initializers deterministic
[icinga2] / lib / base / type.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://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         FAInternal = 64,
40 }; 
41
42 class Type;
43
44 struct Field
45 {
46         int ID;
47         const char *TypeName;
48         const char *Name;
49         int Attributes;
50
51         Field(int id, const char *type, const char *name, int attributes)
52                 : ID(id), TypeName(type), Name(name), Attributes(attributes)
53         { }
54 };
55
56 enum TypeAttribute
57 {
58         TAAbstract = 1
59 };
60
61 class I2_BASE_API Type : public Object
62 {
63 public:
64         DECLARE_PTR_TYPEDEFS(Type);
65
66         virtual String ToString(void) const;
67
68         virtual String GetName(void) const = 0;
69         virtual Type::Ptr GetBaseType(void) const = 0;
70         virtual int GetAttributes(void) const = 0;
71         virtual int GetFieldId(const String& name) const = 0;
72         virtual Field GetFieldInfo(int id) const = 0;
73         virtual int GetFieldCount(void) const = 0;
74
75         Object::Ptr Instantiate(void) const;
76
77         bool IsAssignableFrom(const Type::Ptr& other) const;
78
79         bool IsAbstract(void) const;
80
81         Object::Ptr GetPrototype(void) const;
82         void SetPrototype(const Object::Ptr& object);
83
84         static void Register(const Type::Ptr& type);
85         static Type::Ptr GetByName(const String& name);
86
87         virtual void SetField(int id, const Value& value);
88         virtual Value GetField(int id) const;
89
90         virtual std::vector<String> GetLoadDependencies(void) const;
91
92 protected:
93         virtual ObjectFactory GetFactory(void) const = 0;
94
95 private:
96         Object::Ptr m_Prototype;
97 };
98
99 class I2_BASE_API TypeType : public Type
100 {
101 public:
102         DECLARE_PTR_TYPEDEFS(Type);
103
104         virtual String GetName(void) const;
105         virtual Type::Ptr GetBaseType(void) const;
106         virtual int GetAttributes(void) const;
107         virtual int GetFieldId(const String& name) const;
108         virtual Field GetFieldInfo(int id) const;
109         virtual int GetFieldCount(void) const;
110
111 protected:
112         virtual ObjectFactory GetFactory(void) const;
113 };
114
115 template<typename T>
116 class TypeImpl
117 {
118 };
119
120 #define REGISTER_TYPE(type) \
121         namespace { namespace UNIQUE_NAME(rt) { \
122                 void RegisterType ## type(void) \
123                 { \
124                         icinga::Type::Ptr t = new TypeImpl<type>(); \
125                         type::TypeInstance = t; \
126                         icinga::Type::Register(t); \
127                 } \
128                 \
129                 INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
130         } } \
131         DEFINE_TYPE_INSTANCE(type)
132
133 #define DEFINE_TYPE_INSTANCE(type) \
134         Type::Ptr type::TypeInstance
135
136 }
137
138 #endif /* TYPE_H */