]> granicus.if.org Git - icinga2/blob - lib/base/type.hpp
Rename C++ header files.
[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/qstring.hpp"
25 #include "base/object.hpp"
26 #include "base/initialize.hpp"
27 #include <boost/function.hpp>
28
29 namespace icinga
30 {
31
32 struct Field
33 {
34         int ID;
35         const char *Name;
36         int Attributes;
37
38         Field(int id, const char *name, int attributes)
39                 : ID(id), Name(name), Attributes(attributes)
40         { }
41 };
42
43 enum TypeAttribute
44 {
45         TAAbstract = 1,
46         TASafe = 2
47 };
48
49 class I2_BASE_API Type
50 {
51 public:
52         typedef boost::function<Object::Ptr (void)> Factory;
53
54         virtual String GetName(void) const = 0;
55         virtual const Type *GetBaseType(void) const = 0;
56         virtual int GetAttributes(void) const = 0;
57         virtual int GetFieldId(const String& name) const = 0;
58         virtual Field GetFieldInfo(int id) const = 0;
59         virtual int GetFieldCount(void) const = 0;
60
61         Object::Ptr Instantiate(void) const;
62
63         bool IsAssignableFrom(const Type *other) const;
64
65         bool IsAbstract(void) const;
66         bool IsSafe(void) const;
67
68         static void Register(const Type *type);
69         static const Type *GetByName(const String& name);
70
71         void SetFactory(const Factory& factory);
72
73 private:
74         typedef std::map<String, const Type *> TypeMap;
75
76         static TypeMap& GetTypes(void);
77
78         Factory m_Factory;
79 };
80
81 template<typename T>
82 class TypeImpl
83 {
84 };
85
86 template<typename T>
87 shared_ptr<T> ObjectFactory(void)
88 {
89         return make_shared<T>();
90 }
91
92 template<typename T>
93 struct FactoryHelper
94 {
95         Type::Factory GetFactory(void)
96         {
97                 return ObjectFactory<T>;
98         }
99 };
100
101 #define REGISTER_TYPE(type) \
102         namespace { \
103                 void RegisterType(void) \
104                 { \
105                         icinga::Type *t = new TypeImpl<type>(); \
106                         t->SetFactory(FactoryHelper<type>().GetFactory()); \
107                         icinga::Type::Register(t); \
108                 } \
109                 \
110                 INITIALIZE_ONCE(RegisterType); \
111         }
112
113 }
114
115 #endif /* TYPE_H */