]> granicus.if.org Git - icinga2/blob - lib/db_ido/dbtype.hpp
Fix Zone::IsGlobal()
[icinga2] / lib / db_ido / dbtype.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 DBTYPE_H
21 #define DBTYPE_H
22
23 #include "db_ido/i2-db_ido.hpp"
24 #include "base/object.hpp"
25 #include "base/registry.hpp"
26 #include "base/singleton.hpp"
27 #include <set>
28
29 namespace icinga
30 {
31
32 class DbObject;
33
34 /**
35  * A database object type.
36  *
37  * @ingroup ido
38  */
39 class I2_DB_IDO_API DbType : public Object
40 {
41 public:
42         DECLARE_PTR_TYPEDEFS(DbType);
43
44         typedef boost::function<shared_ptr<DbObject> (const shared_ptr<DbType>&, const String&, const String&)> ObjectFactory;
45         typedef std::map<String, DbType::Ptr> TypeMap;
46         typedef std::map<std::pair<String, String>, shared_ptr<DbObject> > ObjectMap;
47
48         DbType(const String& table, long tid, const String& idcolumn, const ObjectFactory& factory);
49
50         std::vector<String> GetNames(void) const;
51         String GetTable(void) const;
52         long GetTypeID(void) const;
53         String GetIDColumn(void) const;
54
55         static void RegisterType(const String& name, const DbType::Ptr& type);
56
57         static DbType::Ptr GetByName(const String& name);
58         static DbType::Ptr GetByID(long tid);
59
60         shared_ptr<DbObject> GetOrCreateObjectByName(const String& name1, const String& name2);
61
62         static std::set<DbType::Ptr> GetAllTypes(void);
63
64 private:
65         std::vector<String> m_Names;
66         String m_Table;
67         long m_TypeID;
68         String m_IDColumn;
69         ObjectFactory m_ObjectFactory;
70
71         static boost::mutex& GetStaticMutex(void);
72         static TypeMap& GetTypes(void);
73
74         ObjectMap m_Objects;
75 };
76
77 /**
78  * A registry for DbType objects.
79  *
80  * @ingroup ido
81  */
82 class I2_DB_IDO_API DbTypeRegistry : public Registry<DbTypeRegistry, DbType::Ptr>
83 {
84 public:
85         static inline DbTypeRegistry *GetInstance(void)
86         {
87                 return Singleton<DbTypeRegistry>::GetInstance();
88         }
89 };
90
91 /**
92  * Helper class for registering DynamicObject implementation classes.
93  *
94  * @ingroup ido
95  */
96 class RegisterDbTypeHelper
97 {
98 public:
99         RegisterDbTypeHelper(const String& name, const String& table, long tid, const String& idcolumn, const DbType::ObjectFactory& factory)
100         {
101                 DbType::Ptr dbtype;
102
103                 dbtype = DbType::GetByID(tid);
104
105                 if (!dbtype)
106                         dbtype = make_shared<DbType>(table, tid, idcolumn, factory);
107
108                 DbType::RegisterType(name, dbtype);
109         }
110 };
111
112 /**
113  * Factory function for DbObject-based classes.
114  *
115  * @ingroup ido
116  */
117 template<typename T>
118 shared_ptr<T> DbObjectFactory(const DbType::Ptr& type, const String& name1, const String& name2)
119 {
120         return make_shared<T>(type, name1, name2);
121 }
122
123 #define REGISTER_DBTYPE(name, table, tid, idcolumn, type) \
124         I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, idcolumn, DbObjectFactory<type>);
125
126 }
127
128 #endif /* DBTYPE_H */