]> granicus.if.org Git - icinga2/blob - lib/base/type.hpp
Add missing meta type class for the Type class
[icinga2] / lib / base / type.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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
29 namespace icinga
30 {
31
32 enum FieldAttribute
33 {
34         FAConfig = 1,
35         FAState = 2,
36         FAInternal = 32
37 }; 
38
39 class Type;
40
41 struct Field
42 {
43         int ID;
44         const char *TypeName;
45         const char *Name;
46         int Attributes;
47
48         Field(int id, const char *type, const char *name, int attributes)
49                 : ID(id), TypeName(type), Name(name), Attributes(attributes)
50         { }
51 };
52
53 enum TypeAttribute
54 {
55         TAAbstract = 1
56 };
57
58 class I2_BASE_API Type : public Object
59 {
60 public:
61         DECLARE_PTR_TYPEDEFS(Type);
62
63         virtual String ToString(void) const;
64
65         virtual String GetName(void) const = 0;
66         virtual Type::Ptr GetBaseType(void) const = 0;
67         virtual int GetAttributes(void) const = 0;
68         virtual int GetFieldId(const String& name) const = 0;
69         virtual Field GetFieldInfo(int id) const = 0;
70         virtual int GetFieldCount(void) const = 0;
71
72         Object::Ptr Instantiate(void) const;
73
74         bool IsAssignableFrom(const Type::Ptr& other) const;
75
76         bool IsAbstract(void) const;
77
78         Object::Ptr GetPrototype(void) 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
84         virtual Value GetField(int id) const;
85
86 protected:
87         virtual ObjectFactory GetFactory(void) const = 0;
88
89 private:
90         Object::Ptr m_Prototype;
91 };
92
93 class I2_BASE_API TypeType : public Type
94 {
95 public:
96         DECLARE_PTR_TYPEDEFS(Type);
97
98         virtual String GetName(void) const;
99         virtual Type::Ptr GetBaseType(void) const;
100         virtual int GetAttributes(void) const;
101         virtual int GetFieldId(const String& name) const;
102         virtual Field GetFieldInfo(int id) const;
103         virtual int GetFieldCount(void) const;
104
105 protected:
106         virtual ObjectFactory GetFactory(void) const;
107 };
108
109 template<typename T>
110 class TypeImpl
111 {
112 };
113
114 #define REGISTER_TYPE(type) \
115         namespace { namespace UNIQUE_NAME(rt) { \
116                 void RegisterType ## type(void) \
117                 { \
118                         icinga::Type::Ptr t = new TypeImpl<type>(); \
119                         type::TypeInstance = t; \
120                         icinga::Type::Register(t); \
121                 } \
122                 \
123                 INITIALIZE_ONCE(RegisterType ## type); \
124         } } \
125         DEFINE_TYPE_INSTANCE(type)
126
127 #define DEFINE_TYPE_INSTANCE(type) \
128         Type::Ptr type::TypeInstance
129
130 }
131
132 #endif /* TYPE_H */