]> granicus.if.org Git - icinga2/commitdiff
Move type variables into the 'Types' namespace
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 12 Aug 2016 14:53:44 +0000 (16:53 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 12 Aug 2016 15:05:19 +0000 (17:05 +0200)
refs #12408

lib/base/scriptframe.cpp
lib/base/type.cpp
lib/base/type.hpp
lib/config/configitem.cpp
lib/remote/filterutility.cpp
lib/remote/typequeryhandler.cpp

index f2b14211de86f13c282da84aa422f6cfffa073af..9e21cd4d8229952d6db7c17f6f40d726d2ac9764 100644 (file)
@@ -34,6 +34,10 @@ void ScriptFrame::StaticInitialize(void)
        ScriptGlobal::Set("System", systemNS);
        AddImport(systemNS);
 
+       Dictionary::Ptr typesNS = new Dictionary();
+       ScriptGlobal::Set("Types", typesNS);
+       AddImport(typesNS);
+
        Dictionary::Ptr deprecatedNS = new Dictionary();
        ScriptGlobal::Set("Deprecated", deprecatedNS);
        AddImport(deprecatedNS);
index ab6b102321f1640dd13785d1692bea21196957ef..cad78a626a0ea8a4415d630760a32a6d5fdd3a6c 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "base/type.hpp"
 #include "base/scriptglobal.hpp"
+#include "base/objectlock.hpp"
+#include <boost/foreach.hpp>
 
 using namespace icinga;
 
@@ -43,12 +45,17 @@ void Type::Register(const Type::Ptr& type)
 {
        VERIFY(GetByName(type->GetName()) == NULL);
 
-       ScriptGlobal::Set(type->GetName(), type);
+       ScriptGlobal::Set("Types." + type->GetName(), type);
 }
 
 Type::Ptr Type::GetByName(const String& name)
 {
-       Value ptype = ScriptGlobal::Get(name, &Empty);
+       Dictionary::Ptr typesNS = ScriptGlobal::Get("Types", &Empty);
+
+       if (!typesNS)
+               return Type::Ptr();
+
+       Value ptype = typesNS->Get(name);
 
        if (!ptype.IsObjectType<Type>())
                return Type::Ptr();
@@ -56,6 +63,25 @@ Type::Ptr Type::GetByName(const String& name)
        return ptype;
 }
 
+std::vector<Type::Ptr> Type::GetAllTypes(void)
+{
+       std::vector<Type::Ptr> types;
+
+       Dictionary::Ptr typesNS = ScriptGlobal::Get("Types", &Empty);
+
+       if (typesNS) {
+               ObjectLock olock(typesNS);
+
+               BOOST_FOREACH(const Dictionary::Pair& kv, typesNS) {
+                       if (kv.second.IsObjectType<Type>())
+                               types.push_back(kv.second);
+       }
+
+       }
+
+       return types;
+}
+
 String Type::GetPluralName(void) const
 {
        String name = GetName();
index eced9e70fd21e55fc7dd437ac36dc8857a80d305..cb2b50f41beb65f81727c4ab3e39b6fe68ff407c 100644 (file)
@@ -97,6 +97,7 @@ public:
 
        static void Register(const Type::Ptr& type);
        static Type::Ptr GetByName(const String& name);
+       static std::vector<Type::Ptr> GetAllTypes(void);
 
        virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
        virtual Value GetField(int id) const override;
index 5266464cc0853e0883289101dc1e375ac0c5dffd..26c9db1c4382fa61b8fcfcd2350f9f7cc18e2c6a 100644 (file)
@@ -453,18 +453,7 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
 
        std::set<String> types;
 
-       std::vector<Type::Ptr> all_types;
-       Dictionary::Ptr globals = ScriptGlobal::GetGlobals();
-
-       {
-               ObjectLock olock(globals);
-               BOOST_FOREACH(const Dictionary::Pair& kv, globals) {
-                       if (kv.second.IsObjectType<Type>())
-                               all_types.push_back(kv.second);
-               }
-       }
-
-       BOOST_FOREACH(const Type::Ptr& type, all_types) {
+       BOOST_FOREACH(const Type::Ptr& type, Type::GetAllTypes()) {
                if (ConfigObject::TypeInstance->IsAssignableFrom(type))
                        types.insert(type->GetName());
        }
index 6e4bb625d720b0d9a6cfc19a6e10d69bb0052cde..772bc7f903dca47f1d36669d19002c621be72635 100644 (file)
@@ -34,21 +34,12 @@ Type::Ptr FilterUtility::TypeFromPluralName(const String& pluralName)
        String uname = pluralName;
        boost::algorithm::to_lower(uname);
 
-       {
-               Dictionary::Ptr globals = ScriptGlobal::GetGlobals();
-               ObjectLock olock(globals);
-               BOOST_FOREACH(const Dictionary::Pair& kv, globals) {
-                       if (!kv.second.IsObjectType<Type>())
-                               continue;
-
-                       Type::Ptr type = kv.second;
+       BOOST_FOREACH(const Type::Ptr&type, Type::GetAllTypes()) {
+               String pname = type->GetPluralName();
+               boost::algorithm::to_lower(pname);
 
-                       String pname = type->GetPluralName();
-                       boost::algorithm::to_lower(pname);
-
-                       if (uname == pname)
-                               return type;
-               }
+               if (uname == pname)
+                       return type;
        }
 
        return Type::Ptr();
index 7af46510e63d2cbd73413c06883e802e21c18486..b93d67f2b131546e368b3f5d33745033e6127974 100644 (file)
@@ -38,18 +38,7 @@ public:
        virtual void FindTargets(const String& type,
            const boost::function<void (const Value&)>& addTarget) const override
        {
-               std::vector<Type::Ptr> targets;
-
-               {
-                       Dictionary::Ptr globals = ScriptGlobal::GetGlobals();
-                       ObjectLock olock(globals);
-                       BOOST_FOREACH(const Dictionary::Pair& kv, globals) {
-                               if (kv.second.IsObjectType<Type>())
-                                       targets.push_back(kv.second);
-                       }
-               }
-
-               BOOST_FOREACH(const Type::Ptr& target, targets) {
+               BOOST_FOREACH(const Type::Ptr& target, Type::GetAllTypes()) {
                        addTarget(target);
                }
        }