[icinga-checkins] icinga.org: icinga2/master: Move type variables into the 'Types' namespace

git at icinga.org git at icinga.org
Fri Aug 12 17:05:32 CEST 2016


Module: icinga2
Branch: master
Commit: afc1b9bdc5513cb8aefefacf7635fc74cb79900c
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=afc1b9bdc5513cb8aefefacf7635fc74cb79900c

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Fri Aug 12 16:53:44 2016 +0200

Move type variables into the 'Types' namespace

refs #12408

---

 lib/base/scriptframe.cpp        |    4 ++++
 lib/base/type.cpp               |   30 ++++++++++++++++++++++++++++--
 lib/base/type.hpp               |    1 +
 lib/config/configitem.cpp       |   13 +------------
 lib/remote/filterutility.cpp    |   19 +++++--------------
 lib/remote/typequeryhandler.cpp |   13 +------------
 6 files changed, 40 insertions(+), 40 deletions(-)

diff --git a/lib/base/scriptframe.cpp b/lib/base/scriptframe.cpp
index f2b1421..9e21cd4 100644
--- a/lib/base/scriptframe.cpp
+++ b/lib/base/scriptframe.cpp
@@ -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);
diff --git a/lib/base/type.cpp b/lib/base/type.cpp
index ab6b102..cad78a6 100644
--- a/lib/base/type.cpp
+++ b/lib/base/type.cpp
@@ -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();
diff --git a/lib/base/type.hpp b/lib/base/type.hpp
index eced9e7..cb2b50f 100644
--- a/lib/base/type.hpp
+++ b/lib/base/type.hpp
@@ -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;
diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp
index 5266464..26c9db1 100644
--- a/lib/config/configitem.cpp
+++ b/lib/config/configitem.cpp
@@ -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());
 	}
diff --git a/lib/remote/filterutility.cpp b/lib/remote/filterutility.cpp
index 6e4bb62..772bc7f 100644
--- a/lib/remote/filterutility.cpp
+++ b/lib/remote/filterutility.cpp
@@ -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();
diff --git a/lib/remote/typequeryhandler.cpp b/lib/remote/typequeryhandler.cpp
index 7af4651..b93d67f 100644
--- a/lib/remote/typequeryhandler.cpp
+++ b/lib/remote/typequeryhandler.cpp
@@ -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);
 		}
 	}



More information about the icinga-checkins mailing list