Clean up the DynamicType class.

Refs #5036
This commit is contained in:
Gunnar Beutner 2013-11-08 16:07:21 +01:00
parent 7c990eff6e
commit e546cd854d
42 changed files with 28 additions and 112 deletions

View File

@ -28,7 +28,6 @@
using namespace icinga;
REGISTER_NTYPE(CheckerComponent);
REGISTER_TYPE(CheckerComponent);
void CheckerComponent::Start(void)

View File

@ -34,7 +34,6 @@
using namespace icinga;
REGISTER_NTYPE(ClusterListener);
REGISTER_TYPE(ClusterListener);
/**

View File

@ -28,7 +28,6 @@
using namespace icinga;
REGISTER_NTYPE(Endpoint);
REGISTER_TYPE(Endpoint);
boost::signals2::signal<void (const Endpoint::Ptr&)> Endpoint::OnConnected;

View File

@ -30,7 +30,6 @@
using namespace icinga;
REGISTER_NTYPE(CheckResultReader);
REGISTER_TYPE(CheckResultReader);
/**

View File

@ -39,7 +39,6 @@
using namespace icinga;
REGISTER_NTYPE(CompatLogger);
REGISTER_TYPE(CompatLogger);
REGISTER_SCRIPTFUNCTION(ValidateRotationMethod, &CompatLogger::ValidateRotationMethod);

View File

@ -27,7 +27,6 @@
using namespace icinga;
REGISTER_NTYPE(ExternalCommandListener);
REGISTER_TYPE(ExternalCommandListener);
/**

View File

@ -41,7 +41,6 @@
using namespace icinga;
REGISTER_NTYPE(StatusDataWriter);
REGISTER_TYPE(StatusDataWriter);
/**

View File

@ -33,7 +33,6 @@
using namespace icinga;
REGISTER_NTYPE(IdoMysqlConnection);
REGISTER_TYPE(IdoMysqlConnection);
#define SCHEMA_VERSION "1.10.0"

View File

@ -33,7 +33,6 @@
using namespace icinga;
REGISTER_NTYPE(IdoPgsqlConnection);
REGISTER_TYPE(IdoPgsqlConnection);
#define SCHEMA_VERSION "1.10.0"

View File

@ -23,7 +23,6 @@
using namespace icinga;
REGISTER_NTYPE(Demo);
REGISTER_TYPE(Demo);
/**

View File

@ -34,7 +34,6 @@
using namespace icinga;
using namespace livestatus;
REGISTER_NTYPE(LivestatusListener);
REGISTER_TYPE(LivestatusListener);
REGISTER_SCRIPTFUNCTION(ValidateSocketType, &LivestatusListener::ValidateSocketType);

View File

@ -28,7 +28,6 @@
using namespace icinga;
REGISTER_NTYPE(NotificationComponent);
REGISTER_TYPE(NotificationComponent);
/**

View File

@ -42,7 +42,6 @@
using namespace icinga;
REGISTER_NTYPE(GraphiteWriter);
REGISTER_TYPE(GraphiteWriter);
void GraphiteWriter::Start(void)

View File

@ -30,7 +30,6 @@
using namespace icinga;
REGISTER_NTYPE(PerfdataWriter);
REGISTER_TYPE(PerfdataWriter);
void PerfdataWriter::Start(void)

View File

@ -43,7 +43,7 @@
using namespace icinga;
REGISTER_NTYPE(Application);
REGISTER_TYPE(Application);
Application *Application::m_Instance = NULL;
bool Application::m_ShuttingDown = false;

View File

@ -23,7 +23,6 @@
using namespace icinga;
REGISTER_NTYPE(ConsoleLogger);
REGISTER_TYPE(ConsoleLogger);
/**

View File

@ -37,7 +37,7 @@
using namespace icinga;
REGISTER_NTYPE(DynamicObject);
REGISTER_TYPE(DynamicObject);
boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnStarted;
boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnStopped;

View File

@ -24,8 +24,8 @@
using namespace icinga;
DynamicType::DynamicType(const String& name, const DynamicType::ObjectFactory& factory)
: m_Name(name), m_ObjectFactory(factory)
DynamicType::DynamicType(const String& name)
: m_Name(name)
{ }
DynamicType::Ptr DynamicType::GetByName(const String& name)
@ -34,9 +34,21 @@ DynamicType::Ptr DynamicType::GetByName(const String& name)
DynamicType::TypeMap::const_iterator tt = InternalGetTypeMap().find(name);
if (tt == InternalGetTypeMap().end())
if (tt == InternalGetTypeMap().end()) {
const Type *type = Type::GetByName(name);
if (!type || !GetType<DynamicObject>()->IsAssignableFrom(type)
|| type->IsAbstract())
return DynamicType::Ptr();
DynamicType::Ptr dtype = make_shared<DynamicType>(name);
InternalGetTypeMap()[type->GetName()] = dtype;
InternalGetTypeVector().push_back(dtype);
return dtype;
}
return tt->second;
}
@ -112,36 +124,17 @@ DynamicObject::Ptr DynamicType::GetObject(const String& name) const
return nt->second;
}
void DynamicType::RegisterType(const DynamicType::Ptr& type)
{
boost::mutex::scoped_lock lock(GetStaticMutex());
DynamicType::TypeMap::const_iterator tt = InternalGetTypeMap().find(type->GetName());
if (tt != InternalGetTypeMap().end())
BOOST_THROW_EXCEPTION(std::runtime_error("Cannot register class for type '" +
type->GetName() + "': Objects of this type already exist."));
InternalGetTypeMap()[type->GetName()] = type;
InternalGetTypeVector().push_back(type);
}
DynamicObject::Ptr DynamicType::CreateObject(const Dictionary::Ptr& serializedUpdate)
{
ASSERT(!OwnsLock());
ObjectFactory factory;
const Type *type = Type::GetByName(m_Name);
{
ObjectLock olock(this);
factory = m_ObjectFactory;
}
DynamicObject::Ptr object = factory();
Object::Ptr object = type->Instantiate();
Deserialize(object, serializedUpdate, FAConfig);
return object;
return dynamic_pointer_cast<DynamicObject>(object);
}
boost::mutex& DynamicType::GetStaticMutex(void)

View File

@ -36,16 +36,12 @@ class I2_BASE_API DynamicType : public Object
public:
DECLARE_PTR_TYPEDEFS(DynamicType);
typedef boost::function<DynamicObject::Ptr (void)> ObjectFactory;
DynamicType(const String& name, const ObjectFactory& factory);
DynamicType(const String& name);
String GetName(void) const;
static DynamicType::Ptr GetByName(const String& name);
static void RegisterType(const DynamicType::Ptr& type);
DynamicObject::Ptr CreateObject(const Dictionary::Ptr& serializedUpdate);
DynamicObject::Ptr GetObject(const String& name) const;
@ -72,7 +68,6 @@ public:
private:
String m_Name;
ObjectFactory m_ObjectFactory;
typedef std::map<String, DynamicObject::Ptr, string_iless> ObjectMap;
typedef std::vector<DynamicObject::Ptr> ObjectVector;
@ -90,46 +85,6 @@ private:
static std::vector<DynamicObject::Ptr> GetObjects(const String& type);
};
/**
* A registry for DynamicType objects.
*
* @ingroup base
*/
class DynamicTypeRegistry : public Registry<DynamicTypeRegistry, DynamicType::Ptr>
{ };
/**
* Helper class for registering DynamicObject implementation classes.
*
* @ingroup base
*/
class RegisterTypeHelper
{
public:
RegisterTypeHelper(const String& name, const DynamicType::ObjectFactory& factory)
{
DynamicType::Ptr type = make_shared<DynamicType>(name, factory);
DynamicType::RegisterType(type);
}
};
/**
* Factory function for DynamicObject-based classes.
*
* @ingroup base
*/
template<typename T>
shared_ptr<T> DynamicObjectFactory(void)
{
return make_shared<T>();
}
#define REGISTER_TYPE_ALIAS(type, alias) \
I2_EXPORT icinga::RegisterTypeHelper g_RegisterDT_ ## type(alias, DynamicObjectFactory<type>);
#define REGISTER_TYPE(type) \
REGISTER_TYPE_ALIAS(type, #type)
}
#endif /* DYNAMICTYPE_H */

View File

@ -23,7 +23,6 @@
using namespace icinga;
REGISTER_NTYPE(FileLogger);
REGISTER_TYPE(FileLogger);
/**

View File

@ -29,7 +29,7 @@
using namespace icinga;
REGISTER_NTYPE(Logger);
REGISTER_TYPE(Logger);
std::set<Logger::Ptr> Logger::m_Loggers;
boost::mutex Logger::m_Mutex;

View File

@ -26,7 +26,6 @@
using namespace icinga;
REGISTER_NTYPE(Script);
REGISTER_TYPE(Script);
void Script::Start(void)

View File

@ -26,7 +26,7 @@
using namespace icinga;
REGISTER_NTYPE(StreamLogger);
REGISTER_TYPE(StreamLogger);
boost::mutex StreamLogger::m_Mutex;

View File

@ -23,7 +23,6 @@
#ifndef _WIN32
using namespace icinga;
REGISTER_NTYPE(SyslogLogger);
REGISTER_TYPE(SyslogLogger);
/**

View File

@ -95,7 +95,7 @@ struct FactoryHelper
}
};
#define REGISTER_NTYPE(type) \
#define REGISTER_TYPE(type) \
namespace { \
void RegisterType(void) \
{ \

View File

@ -31,7 +31,7 @@
using namespace icinga;
REGISTER_NTYPE(DbConnection);
REGISTER_TYPE(DbConnection);
Timer::Ptr DbConnection::m_ProgramStatusTimer;

View File

@ -23,7 +23,6 @@
using namespace icinga;
REGISTER_NTYPE(Hello);
REGISTER_TYPE(Hello);
/**

View File

@ -22,7 +22,6 @@
using namespace icinga;
REGISTER_NTYPE(CheckCommand);
REGISTER_TYPE(CheckCommand);
Dictionary::Ptr CheckCommand::Execute(const Service::Ptr& service)

View File

@ -21,7 +21,7 @@
using namespace icinga;
REGISTER_NTYPE(Command);
REGISTER_TYPE(Command);
bool Command::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
{

View File

@ -22,7 +22,6 @@
using namespace icinga;
REGISTER_NTYPE(Domain);
REGISTER_TYPE(Domain);
int Domain::GetPrivileges(const String& instance) const

View File

@ -22,7 +22,6 @@
using namespace icinga;
REGISTER_NTYPE(EventCommand);
REGISTER_TYPE(EventCommand);
void EventCommand::Execute(const Service::Ptr& service)

View File

@ -38,7 +38,6 @@
using namespace icinga;
REGISTER_NTYPE(Host);
REGISTER_TYPE(Host);
void Host::Start(void)

View File

@ -27,7 +27,6 @@
using namespace icinga;
REGISTER_NTYPE(HostGroup);
REGISTER_TYPE(HostGroup);
std::set<Host::Ptr> HostGroup::GetMembers(void) const

View File

@ -32,7 +32,6 @@ using namespace icinga;
static Timer::Ptr l_RetentionTimer;
REGISTER_NTYPE(IcingaApplication);
REGISTER_TYPE(IcingaApplication);
INITIALIZE_ONCE(IcingaApplication, &IcingaApplication::StaticInitialize);

View File

@ -32,7 +32,6 @@
using namespace icinga;
REGISTER_NTYPE(Notification);
REGISTER_TYPE(Notification);
boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;

View File

@ -22,7 +22,6 @@
using namespace icinga;
REGISTER_NTYPE(NotificationCommand);
REGISTER_TYPE(NotificationCommand);
Dictionary::Ptr NotificationCommand::Execute(const Notification::Ptr& notification,

View File

@ -25,7 +25,7 @@
using namespace icinga;
REGISTER_NTYPE(PerfdataValue);
REGISTER_TYPE(PerfdataValue);
PerfdataValue::PerfdataValue(void)
{ }

View File

@ -33,7 +33,6 @@
using namespace icinga;
REGISTER_NTYPE(Service);
REGISTER_TYPE(Service);
boost::signals2::signal<void (const Service::Ptr&, const String&, const String&, AcknowledgementType, double, const String&)> Service::OnAcknowledgementSet;

View File

@ -28,7 +28,6 @@
using namespace icinga;
REGISTER_NTYPE(ServiceGroup);
REGISTER_TYPE(ServiceGroup);
std::set<Service::Ptr> ServiceGroup::GetMembers(void) const

View File

@ -29,7 +29,6 @@
using namespace icinga;
REGISTER_NTYPE(TimePeriod);
REGISTER_TYPE(TimePeriod);
REGISTER_SCRIPTFUNCTION(EmptyTimePeriod, &TimePeriod::EmptyTimePeriodUpdate);
REGISTER_SCRIPTFUNCTION(EvenMinutesTimePeriod, &TimePeriod::EvenMinutesTimePeriodUpdate);

View File

@ -25,7 +25,6 @@
using namespace icinga;
REGISTER_NTYPE(User);
REGISTER_TYPE(User);
void User::OnConfigLoaded(void)

View File

@ -27,7 +27,6 @@
using namespace icinga;
REGISTER_NTYPE(UserGroup);
REGISTER_TYPE(UserGroup);
std::set<User::Ptr> UserGroup::GetMembers(void) const