1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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>
38 Field(int id, const char *name, int attributes)
39 : ID(id), Name(name), Attributes(attributes)
49 class I2_BASE_API Type
52 typedef boost::function<Object::Ptr (void)> Factory;
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;
61 Object::Ptr Instantiate(void) const;
63 bool IsAssignableFrom(const Type *other) const;
65 bool IsAbstract(void) const;
66 bool IsSafe(void) const;
68 static void Register(const Type *type);
69 static const Type *GetByName(const String& name);
71 void SetFactory(const Factory& factory);
74 typedef std::map<String, const Type *> TypeMap;
76 static TypeMap& GetTypes(void);
87 shared_ptr<T> ObjectFactory(void)
89 return make_shared<T>();
95 Type::Factory GetFactory(void)
97 return ObjectFactory<T>;
101 #define REGISTER_TYPE(type) \
103 void RegisterType(void) \
105 icinga::Type *t = new TypeImpl<type>(); \
106 t->SetFactory(FactoryHelper<type>().GetFactory()); \
107 icinga::Type::Register(t); \
110 INITIALIZE_ONCE(RegisterType); \