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