From 701961b73bf867a3bcfe1225189c4a5bd26360eb Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 5 Nov 2015 10:29:02 +0100 Subject: [PATCH] Fix the 'type' attribute for the Object class refs #10387 --- lib/base/CMakeLists.txt | 2 +- lib/base/configobject.cpp | 2 +- lib/base/configobject.ti | 1 - lib/base/object.cpp | 6 +-- lib/base/objecttype.cpp | 78 ++++++++++++++++++++++++++++++++++++++ lib/base/objecttype.hpp | 48 +++++++++++++++++++++++ lib/cli/consolecommand.cpp | 2 + lib/config/configitem.cpp | 1 - 8 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 lib/base/objecttype.cpp create mode 100644 lib/base/objecttype.hpp diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index ce2e3c928..4689c0677 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -30,7 +30,7 @@ set(base_SOURCES exception.cpp fifo.cpp filelogger.cpp filelogger.thpp initialize.cpp json.cpp json-script.cpp loader.cpp logger.cpp logger.thpp math-script.cpp netstring.cpp networkstream.cpp number.cpp number-script.cpp object.cpp - object-script.cpp primitivetype.cpp process.cpp ringbuffer.cpp scriptframe.cpp + object-script.cpp objecttype.cpp primitivetype.cpp process.cpp ringbuffer.cpp scriptframe.cpp function.cpp function-script.cpp functionwrapper.cpp scriptglobal.cpp scriptutils.cpp serializer.cpp socket.cpp socketevents.cpp stacktrace.cpp statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp diff --git a/lib/base/configobject.cpp b/lib/base/configobject.cpp index deb746752..ff23965f2 100644 --- a/lib/base/configobject.cpp +++ b/lib/base/configobject.cpp @@ -53,7 +53,7 @@ ConfigObject::ConfigObject(void) ConfigType::Ptr ConfigObject::GetType(void) const { - return ConfigType::GetByName(GetTypeNameV()); + return ConfigType::GetByName(GetReflectionType()->GetName()); } bool ConfigObject::IsActive(void) const diff --git a/lib/base/configobject.ti b/lib/base/configobject.ti index fdb01fd27..c2e5a8323 100644 --- a/lib/base/configobject.ti +++ b/lib/base/configobject.ti @@ -77,7 +77,6 @@ abstract class ConfigObject : ConfigObjectBase return m_ShortName; }}} }; - [config, get_protected, no_user_modify] String type (TypeNameV); [config] name(Zone) zone (ZoneName); [config, no_user_modify] String package; [config, get_protected, no_user_modify] Array::Ptr templates; diff --git a/lib/base/object.cpp b/lib/base/object.cpp index ca0cb540f..4eeef78db 100644 --- a/lib/base/object.cpp +++ b/lib/base/object.cpp @@ -25,7 +25,7 @@ using namespace icinga; -REGISTER_PRIMITIVE_TYPE(Object, None, Object::GetPrototype()); +DEFINE_TYPE_INSTANCE(Object); /** * Default constructor for the Object class. @@ -79,7 +79,7 @@ void Object::InflateMutex(void) void Object::SetField(int id, const Value&, bool, const Value&) { if (id == 0) - BOOST_THROW_EXCEPTION(std::runtime_error("Prototype field cannot be set.")); + BOOST_THROW_EXCEPTION(std::runtime_error("Type field cannot be set.")); else BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID.")); } @@ -87,7 +87,7 @@ void Object::SetField(int id, const Value&, bool, const Value&) Value Object::GetField(int id) const { if (id == 0) - return Empty; + return GetReflectionType()->GetName(); else BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID.")); } diff --git a/lib/base/objecttype.cpp b/lib/base/objecttype.cpp new file mode 100644 index 000000000..65f486c12 --- /dev/null +++ b/lib/base/objecttype.cpp @@ -0,0 +1,78 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2015 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 "base/objecttype.hpp" +#include "base/initialize.hpp" + +using namespace icinga; + +static void RegisterObjectType(void) +{ + Type::Ptr type = new ObjectType(); + type->SetPrototype(Object::GetPrototype()); + Type::Register(type); + Object::TypeInstance = type; +} + +INITIALIZE_ONCE(&RegisterObjectType); + +ObjectType::ObjectType(void) +{ } + +String ObjectType::GetName(void) const +{ + return "Object"; +} + +Type::Ptr ObjectType::GetBaseType(void) const +{ + return Type::Ptr(); +} + +int ObjectType::GetAttributes(void) const +{ + return 0; +} + +int ObjectType::GetFieldId(const String& name) const +{ + if (name == "type") + return 0; + else + return -1; +} + +Field ObjectType::GetFieldInfo(int id) const +{ + if (id == 0) + return Field(1, "String", "type", NULL, NULL, 0, 0); + else + BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID.")); +} + +int ObjectType::GetFieldCount(void) const +{ + return 1; +} + +ObjectFactory ObjectType::GetFactory(void) const +{ + return DefaultObjectFactory; +} + diff --git a/lib/base/objecttype.hpp b/lib/base/objecttype.hpp new file mode 100644 index 000000000..20a915f95 --- /dev/null +++ b/lib/base/objecttype.hpp @@ -0,0 +1,48 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2015 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 OBJECTTYPE_H +#define OBJECTTYPE_H + +#include "base/i2-base.hpp" +#include "base/type.hpp" +#include "base/initialize.hpp" + +namespace icinga +{ + +class I2_BASE_API ObjectType : public Type +{ +public: + ObjectType(void); + + virtual String GetName(void) const override; + virtual Type::Ptr GetBaseType(void) const override; + virtual int GetAttributes(void) const override; + virtual int GetFieldId(const String& name) const override; + virtual Field GetFieldInfo(int id) const override; + virtual int GetFieldCount(void) const override; + +protected: + virtual ObjectFactory GetFactory(void) const override; +}; + +} + +#endif /* OBJECTTYPE_H */ diff --git a/lib/cli/consolecommand.cpp b/lib/cli/consolecommand.cpp index 6fb0bd25c..32162b35d 100644 --- a/lib/cli/consolecommand.cpp +++ b/lib/cli/consolecommand.cpp @@ -135,6 +135,8 @@ int ConsoleCommand::Run(const po::variables_map& vm, const std::vector(type->Instantiate()); dobj->SetDebugInfo(m_DebugInfo); - dobj->SetTypeNameV(m_Type); dobj->SetZoneName(m_Zone); dobj->SetPackage(m_Package); dobj->SetName(m_Name);