]> granicus.if.org Git - icinga2/commitdiff
Fix the 'type' attribute for the Object class
authorGunnar Beutner <gunnar@beutner.name>
Thu, 5 Nov 2015 09:29:02 +0000 (10:29 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Thu, 5 Nov 2015 09:29:02 +0000 (10:29 +0100)
refs #10387

lib/base/CMakeLists.txt
lib/base/configobject.cpp
lib/base/configobject.ti
lib/base/object.cpp
lib/base/objecttype.cpp [new file with mode: 0644]
lib/base/objecttype.hpp [new file with mode: 0644]
lib/cli/consolecommand.cpp
lib/config/configitem.cpp

index ce2e3c928bb9b951e0da6badb1f68277e7ea11bf..4689c0677da37133d716f867443dc09569a5650f 100644 (file)
@@ -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
index deb746752ab036d7629b7bba42c6cc071705a479..ff23965f247ade0206aa58c4d341efe6644a041d 100644 (file)
@@ -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
index fdb01fd2707b91605925c20106a7876fb0132a99..c2e5a8323cfd20b0c56eb7a635eb56e767530009 100644 (file)
@@ -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;
index ca0cb540f36ad426ef3c752d1199a503a68dda21..4eeef78db92d35084f8fdc1b62e225becbaf7014 100644 (file)
@@ -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 (file)
index 0000000..65f486c
--- /dev/null
@@ -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<Object>;
+}
+
diff --git a/lib/base/objecttype.hpp b/lib/base/objecttype.hpp
new file mode 100644 (file)
index 0000000..20a915f
--- /dev/null
@@ -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 */
index 6fb0bd25ccd5fab9e69c266790d6fe873eb5e41a..32162b35d51c356341cb9714b003512d8e63e89f 100644 (file)
@@ -135,6 +135,8 @@ int ConsoleCommand::Run(const po::variables_map& vm, const std::vector<std::stri
        if (vm.count("sandbox"))
                scriptFrame.Sandboxed = true;
 
+       scriptFrame.Self = scriptFrame.Locals;
+
        if (!vm.count("eval"))
                std::cout << "Icinga 2 (version: " << Application::GetAppVersion() << ")\n";
 
index 535ae143a5997d849bd005d4889c52191f8c5009..621c7567546457eac7c2f282f9f5183ac138944d 100644 (file)
@@ -174,7 +174,6 @@ ConfigObject::Ptr ConfigItem::Commit(bool discard)
        ConfigObject::Ptr dobj = static_pointer_cast<ConfigObject>(type->Instantiate());
 
        dobj->SetDebugInfo(m_DebugInfo);
-       dobj->SetTypeNameV(m_Type);
        dobj->SetZoneName(m_Zone);
        dobj->SetPackage(m_Package);
        dobj->SetName(m_Name);