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 "db_ido/i2-db_ido.hpp"
24 #include "base/object.hpp"
25 #include "base/registry.hpp"
26 #include "base/singleton.hpp"
35 * A database object type.
39 class I2_DB_IDO_API DbType : public Object
42 DECLARE_PTR_TYPEDEFS(DbType);
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;
48 DbType(const String& table, long tid, const String& idcolumn, const ObjectFactory& factory);
50 std::vector<String> GetNames(void) const;
51 String GetTable(void) const;
52 long GetTypeID(void) const;
53 String GetIDColumn(void) const;
55 static void RegisterType(const String& name, const DbType::Ptr& type);
57 static DbType::Ptr GetByName(const String& name);
58 static DbType::Ptr GetByID(long tid);
60 shared_ptr<DbObject> GetOrCreateObjectByName(const String& name1, const String& name2);
62 static std::set<DbType::Ptr> GetAllTypes(void);
65 std::vector<String> m_Names;
69 ObjectFactory m_ObjectFactory;
71 static boost::mutex& GetStaticMutex(void);
72 static TypeMap& GetTypes(void);
78 * A registry for DbType objects.
82 class I2_DB_IDO_API DbTypeRegistry : public Registry<DbTypeRegistry, DbType::Ptr>
85 static inline DbTypeRegistry *GetInstance(void)
87 return Singleton<DbTypeRegistry>::GetInstance();
92 * Helper class for registering DynamicObject implementation classes.
96 class RegisterDbTypeHelper
99 RegisterDbTypeHelper(const String& name, const String& table, long tid, const String& idcolumn, const DbType::ObjectFactory& factory)
103 dbtype = DbType::GetByID(tid);
106 dbtype = make_shared<DbType>(table, tid, idcolumn, factory);
108 DbType::RegisterType(name, dbtype);
113 * Factory function for DbObject-based classes.
118 shared_ptr<T> DbObjectFactory(const DbType::Ptr& type, const String& name1, const String& name2)
120 return make_shared<T>(type, name1, name2);
123 #define REGISTER_DBTYPE(name, table, tid, idcolumn, type) \
124 I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, idcolumn, DbObjectFactory<type>);
128 #endif /* DBTYPE_H */