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);
#include "base/type.hpp"
#include "base/scriptglobal.hpp"
+#include "base/objectlock.hpp"
+#include <boost/foreach.hpp>
using namespace icinga;
{
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();
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();
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;
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());
}
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();
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);
}
}