Merge branch 'feature/dtype-5036' into next

Fixes #5036
This commit is contained in:
Gunnar Beutner 2013-11-08 21:59:17 +01:00
commit a95a0985b7
54 changed files with 127 additions and 154 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,8 +34,20 @@ DynamicType::Ptr DynamicType::GetByName(const String& name)
DynamicType::TypeMap::const_iterator tt = InternalGetTypeMap().find(name);
if (tt == InternalGetTypeMap().end())
return DynamicType::Ptr();
if (tt == InternalGetTypeMap().end()) {
const Type *type = Type::GetByName(name);
if (!type || !Type::GetByName("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

@ -34,8 +34,10 @@ inline bool InitializeOnceHelper(InitializeFunc func)
return true;
}
#define INITIALIZE_ONCE(name, func) \
I2_EXPORT bool l_InitializeOnce ## name(InitializeOnceHelper(func))
#define INITIALIZE_ONCE(func) \
namespace { \
I2_EXPORT bool l_InitializeOnce(InitializeOnceHelper(func)); \
}
}

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

@ -54,9 +54,10 @@ Object::Ptr Type::Instantiate(void) const
bool Type::IsAssignableFrom(const Type *other) const
{
for (const Type *t = other; t; t = t->GetBaseType())
for (const Type *t = other; t; t = t->GetBaseType()) {
if (t == this)
return true;
}
return false;
}

View File

@ -74,12 +74,6 @@ class TypeImpl
{
};
template<typename T>
Type *GetType(void)
{
return TypeImpl<T>::GetInstance();
}
template<typename T>
shared_ptr<T> ObjectFactory(void)
{
@ -95,16 +89,16 @@ struct FactoryHelper
}
};
#define REGISTER_NTYPE(type) \
#define REGISTER_TYPE(type) \
namespace { \
void RegisterType(void) \
{ \
icinga::Type *t = icinga::GetType<type>(); \
icinga::Type *t = new TypeImpl<type>(); \
t->SetFactory(FactoryHelper<type>().GetFactory()); \
icinga::Type::Register(GetType<type>()); \
icinga::Type::Register(t); \
} \
\
INITIALIZE_ONCE(type, RegisterType); \
INITIALIZE_ONCE(RegisterType); \
}
}

View File

@ -198,6 +198,19 @@ ValueType Value::GetType(void) const
return static_cast<ValueType>(m_Value.which());
}
bool Value::operator==(bool rhs)
{
if (!IsScalar())
return false;
return static_cast<double>(*this) == rhs;
}
bool Value::operator!=(bool rhs)
{
return !(*this == rhs);
}
bool Value::operator==(int rhs)
{
if (!IsScalar())

View File

@ -73,6 +73,9 @@ public:
operator double(void) const;
operator String(void) const;
bool operator==(bool rhs);
bool operator!=(bool rhs);
bool operator==(int rhs);
bool operator!=(int rhs);

View File

@ -31,11 +31,11 @@
using namespace icinga;
REGISTER_NTYPE(DbConnection);
REGISTER_TYPE(DbConnection);
Timer::Ptr DbConnection::m_ProgramStatusTimer;
INITIALIZE_ONCE(DbConnection, &DbConnection::StaticInitialize);
INITIALIZE_ONCE(&DbConnection::StaticInitialize);
void DbConnection::Start(void)
{

View File

@ -32,7 +32,7 @@ using namespace icinga;
boost::signals2::signal<void (const DbQuery&)> DbObject::OnQuery;
INITIALIZE_ONCE(DbObject, &DbObject::StaticInitialize);
INITIALIZE_ONCE(&DbObject::StaticInitialize);
DbObject::DbObject(const shared_ptr<DbType>& type, const String& name1, const String& name2)
: m_Name1(name1), m_Name2(name2), m_Type(type), m_LastConfigUpdate(0), m_LastStatusUpdate(0)

View File

@ -38,7 +38,7 @@ using namespace icinga;
REGISTER_DBTYPE(Service, "service", DbObjectTypeService, "service_object_id", ServiceDbObject);
INITIALIZE_ONCE(ServiceDbObject, &ServiceDbObject::StaticInitialize);
INITIALIZE_ONCE(&ServiceDbObject::StaticInitialize);
void ServiceDbObject::StaticInitialize(void)
{

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,9 +32,8 @@ using namespace icinga;
static Timer::Ptr l_RetentionTimer;
REGISTER_NTYPE(IcingaApplication);
REGISTER_TYPE(IcingaApplication);
INITIALIZE_ONCE(IcingaApplication, &IcingaApplication::StaticInitialize);
INITIALIZE_ONCE(&IcingaApplication::StaticInitialize);
void IcingaApplication::StaticInitialize(void)
{

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

@ -1,3 +1,22 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 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. *
******************************************************************************/
#include "icinga/perfdatavalue.h"
#include "base/convert.h"
#include <boost/algorithm/string/case_conv.hpp>
@ -6,7 +25,7 @@
using namespace icinga;
REGISTER_NTYPE(PerfdataValue);
REGISTER_TYPE(PerfdataValue);
PerfdataValue::PerfdataValue(void)
{ }

View File

@ -1,3 +1,22 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 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 PERFDATAVALUE_H
#define PERFDATAVALUE_H

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

View File

@ -47,6 +47,10 @@ add_boost_test(base
base_object/construct
base_object/getself
base_object/weak
base_serialize/scalar
base_serialize/array
base_serialize/dictionary
base_serialize/object
base_shellescape/escape_basic
base_shellescape/escape_quoted
base_stacktrace/stacktrace

View File

@ -43,11 +43,8 @@ BOOST_AUTO_TEST_CASE(todouble)
BOOST_AUTO_TEST_CASE(tostring)
{
BOOST_CHECK(Convert::ToString(7) == "7");
BOOST_CHECK(Convert::ToString(7.3) == "7.3");
BOOST_CHECK(Convert::ToString(7.5) == "7.5");
BOOST_CHECK(Convert::ToString("hello") == "hello");
Object::Ptr object = make_shared<Object>();
BOOST_CHECK(Convert::ToString(object) == "Object of type 'icinga::Object'");
}
BOOST_AUTO_TEST_CASE(tobool)

View File

@ -32,15 +32,15 @@ BOOST_AUTO_TEST_SUITE(base_type)
BOOST_AUTO_TEST_CASE(gettype)
{
const Type *t = GetType<Application>();
const Type *t = Type::GetByName("Application");
BOOST_CHECK(t);
}
BOOST_AUTO_TEST_CASE(assign)
{
const Type *t1 = GetType<Application>();
const Type *t2 = GetType<DynamicObject>();
const Type *t1 = Type::GetByName("Application");
const Type *t2 = Type::GetByName("DynamicObject");
BOOST_CHECK(t1->IsAssignableFrom(t1));
BOOST_CHECK(t2->IsAssignableFrom(t1));

View File

@ -88,7 +88,7 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
/* TypeImpl */
std::cout << "template<>" << std::endl
<< "class TypeImpl<" << klass.Name << ">"
<< " : public Type, public Singleton<TypeImpl<" << klass.Name << "> >" << std::endl
<< " : public Type" << std::endl
<< "{" << std::endl
<< "public:" << std::endl;
@ -111,7 +111,7 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
std::cout << "\t\t" << "return ";
if (!klass.Parent.empty())
std::cout << "Singleton<TypeImpl<" << klass.Parent << "> >::GetInstance()";
std::cout << "Type::GetByName(\"" << klass.Parent << "\")";
else
std::cout << "NULL";
@ -168,25 +168,31 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
std::cout << "\t\t" << "int real_id = id - " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount();" << std::endl
<< "\t\t" << "if (real_id < 0) { return " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldInfo(id); }" << std::endl;
std::cout << "\t\t" << "switch (";
if (klass.Fields.size() > 0) {
std::cout << "\t\t" << "switch (";
if (!klass.Parent.empty())
std::cout << "real_id";
else
std::cout << "id";
if (!klass.Parent.empty())
std::cout << "real_id";
else
std::cout << "id";
std::cout << ") {" << std::endl;
std::cout << ") {" << std::endl;
num = 0;
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
std::cout << "\t\t\t" << "case " << num << ":" << std::endl
<< "\t\t\t\t" << "return Field(" << num << ", \"" << it->Name << "\", " << it->Attributes << ");" << std::endl;
num++;
num = 0;
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
std::cout << "\t\t\t" << "case " << num << ":" << std::endl
<< "\t\t\t\t" << "return Field(" << num << ", \"" << it->Name << "\", " << it->Attributes << ");" << std::endl;
num++;
}
std::cout << "\t\t\t" << "default:" << std::endl
<< "\t\t";
}
std::cout << "\t\t\t" << "default:" << std::endl
<< "\t\t\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl
<< "\t\t" << "}" << std::endl;
std::cout << "\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl;
if (klass.Fields.size() > 0)
std::cout << "\t\t" << "}" << std::endl;
std::cout << "\t" << "}" << std::endl << std::endl;
@ -220,7 +226,7 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
/* GetReflectionType */
std::cout << "\t" << "virtual const Type *GetReflectionType(void) const" << std::endl
<< "\t" << "{" << std::endl
<< "\t\t" << "return TypeImpl<" << klass.Name << ">::GetInstance();" << std::endl
<< "\t\t" << "return Type::GetByName(\"" << klass.Name << "\");" << std::endl
<< "\t" << "}" << std::endl << std::endl;
if (!klass.Fields.empty()) {
@ -408,7 +414,6 @@ void ClassCompiler::CompileStream(const std::string& path, std::istream *stream)
std::cout << "#include \"base/object.h\"" << std::endl
<< "#include \"base/type.h\"" << std::endl
<< "#include \"base/singleton.h\"" << std::endl
<< "#include \"base/debug.h\"" << std::endl
<< "#include \"base/value.h\"" << std::endl
<< "#include \"base/array.h\"" << std::endl