2013-07-05 09:37:04 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef DBTYPE_H
|
|
|
|
#define DBTYPE_H
|
|
|
|
|
|
|
|
#include "base/object.h"
|
|
|
|
#include "base/registry.h"
|
2013-08-01 13:20:30 +02:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2013-08-01 13:20:30 +02:00
|
|
|
class DbObject;
|
|
|
|
|
2013-07-05 09:37:04 +02:00
|
|
|
/**
|
|
|
|
* A database object type.
|
|
|
|
*
|
|
|
|
* @ingroup ido
|
|
|
|
*/
|
2013-07-22 12:08:49 +02:00
|
|
|
class DbType : public Object
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DECLARE_PTR_TYPEDEFS(DbType);
|
|
|
|
|
2013-08-01 13:20:30 +02:00
|
|
|
typedef boost::function<boost::shared_ptr<DbObject> (const boost::shared_ptr<DbType>&, const String&, const String&)> ObjectFactory;
|
2013-07-05 09:37:04 +02:00
|
|
|
typedef std::map<String, DbType::Ptr, string_iless> TypeMap;
|
2013-08-01 13:20:30 +02:00
|
|
|
typedef std::map<std::pair<String, String>, boost::shared_ptr<DbObject>, pair_string_iless> ObjectMap;
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2013-08-01 14:08:02 +02:00
|
|
|
DbType(const String& name, const String& table, long tid, const String& idcolumn, const ObjectFactory& factory);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
String GetName(void) const;
|
|
|
|
String GetTable(void) const;
|
2013-07-17 14:10:28 +02:00
|
|
|
long GetTypeID(void) const;
|
2013-08-01 14:08:02 +02:00
|
|
|
String GetIDColumn(void) const;
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
static void RegisterType(const DbType::Ptr& type);
|
|
|
|
|
|
|
|
static DbType::Ptr GetByName(const String& name);
|
2013-07-17 14:10:28 +02:00
|
|
|
static DbType::Ptr GetByID(long tid);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2013-08-01 13:20:30 +02:00
|
|
|
boost::shared_ptr<DbObject> GetOrCreateObjectByName(const String& name1, const String& name2);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
String m_Name;
|
|
|
|
String m_Table;
|
2013-07-17 14:10:28 +02:00
|
|
|
long m_TypeID;
|
2013-08-01 14:08:02 +02:00
|
|
|
String m_IDColumn;
|
2013-07-05 09:37:04 +02:00
|
|
|
ObjectFactory m_ObjectFactory;
|
|
|
|
|
|
|
|
static boost::mutex m_StaticMutex;
|
|
|
|
static TypeMap& GetTypes(void);
|
|
|
|
|
|
|
|
ObjectMap& GetObjects(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A registry for DbType objects.
|
|
|
|
*
|
|
|
|
* @ingroup ido
|
|
|
|
*/
|
|
|
|
class DbTypeRegistry : public Registry<DbType::Ptr>
|
|
|
|
{ };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class for registering DynamicObject implementation classes.
|
|
|
|
*
|
|
|
|
* @ingroup ido
|
|
|
|
*/
|
|
|
|
class RegisterDbTypeHelper
|
|
|
|
{
|
|
|
|
public:
|
2013-08-01 14:08:02 +02:00
|
|
|
RegisterDbTypeHelper(const String& name, const String& table, long tid, const String& idcolumn, const DbType::ObjectFactory& factory)
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2013-08-01 14:08:02 +02:00
|
|
|
DbType::Ptr dbtype = boost::make_shared<DbType>(name, table, tid, idcolumn, factory);
|
2013-07-05 09:37:04 +02:00
|
|
|
DbType::RegisterType(dbtype);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory function for DbObject-based classes.
|
|
|
|
*
|
|
|
|
* @ingroup ido
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2013-08-01 13:20:30 +02:00
|
|
|
shared_ptr<T> DbObjectFactory(const DbType::Ptr& type, const String& name1, const String& name2)
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2013-08-01 13:20:30 +02:00
|
|
|
return boost::make_shared<T>(type, name1, name2);
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
2013-08-01 14:08:02 +02:00
|
|
|
#define REGISTER_DBTYPE(name, table, tid, idcolumn, type) \
|
|
|
|
I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, idcolumn, DbObjectFactory<type>);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DBTYPE_H */
|