Move type variables into the 'Types' namespace

refs #12408
This commit is contained in:
Gunnar Beutner 2016-08-12 16:53:44 +02:00
parent 4350403e1b
commit afc1b9bdc5
6 changed files with 40 additions and 40 deletions

View 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);

View 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();

View 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;

View 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());
}

View 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;
BOOST_FOREACH(const Type::Ptr&type, Type::GetAllTypes()) {
String pname = type->GetPluralName();
boost::algorithm::to_lower(pname);
Type::Ptr type = kv.second;
String pname = type->GetPluralName();
boost::algorithm::to_lower(pname);
if (uname == pname)
return type;
}
if (uname == pname)
return type;
}
return Type::Ptr();

View 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);
}
}