mirror of
https://github.com/Icinga/icinga2.git
synced 2025-04-08 17:05:25 +02:00
Added dyn test class.
This commit is contained in:
parent
6c580aeb4a
commit
8144ca398b
@ -84,11 +84,11 @@
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\mmatch;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)\third-party\mmatch;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<IncludePath>$(SolutionDir)\mmatch;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)\third-party\mmatch;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
|
@ -45,11 +45,11 @@
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\cJSON;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\third-party\cJSON;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\cJSON;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\third-party\cJSON;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
|
@ -14,12 +14,14 @@
|
||||
<ClInclude Include="dynamicdictionary.h" />
|
||||
<ClInclude Include="dynamicobject.h" />
|
||||
<ClInclude Include="i2-dyn.h" />
|
||||
<ClInclude Include="objectspace.h" />
|
||||
<ClInclude Include="objectmap.h" />
|
||||
<ClInclude Include="objectset.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="dynamicdictionary.cpp" />
|
||||
<ClCompile Include="dynamicobject.cpp" />
|
||||
<ClCompile Include="objectspace.cpp" />
|
||||
<ClCompile Include="objectmap.cpp" />
|
||||
<ClCompile Include="objectset.cpp" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{B26AFFA6-2BDF-42E6-A224-2591FFD9BFB7}</ProjectGuid>
|
||||
@ -62,7 +64,7 @@
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>_WINDLL;I2_DYN_BUILD;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
@ -78,7 +80,7 @@
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>_WINDLL;I2_DYN_BUILD;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
|
@ -38,7 +38,7 @@ struct DynamicDictionaryValue
|
||||
DynamicDictionaryOperator Operator;
|
||||
};
|
||||
|
||||
class DynamicDictionary : public Object
|
||||
class I2_DYN_API DynamicDictionary : public Object
|
||||
{
|
||||
public:
|
||||
typedef shared_ptr<DynamicDictionary> Ptr;
|
||||
|
@ -21,70 +21,29 @@
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
DynamicDictionary::Ptr DynamicObject::GetProperties(void) const
|
||||
DynamicObject::DynamicObject(void)
|
||||
: m_Config(make_shared<Dictionary>()), m_Tags(make_shared<Dictionary>())
|
||||
{
|
||||
return m_Properties;
|
||||
}
|
||||
|
||||
void DynamicObject::SetProperties(DynamicDictionary::Ptr properties)
|
||||
{
|
||||
m_Properties = properties;
|
||||
Dictionary::Ptr resolvedProperties = properties->ToFlatDictionary();
|
||||
Reload(resolvedProperties);
|
||||
Dictionary::Ptr DynamicObject::GetConfig(void) const
|
||||
{
|
||||
return m_Config;
|
||||
}
|
||||
|
||||
string DynamicObject::GetName(void) const
|
||||
Dictionary::Ptr DynamicObject::GetTags(void) const
|
||||
{
|
||||
return m_Name;
|
||||
}
|
||||
|
||||
void DynamicObject::SetName(string name)
|
||||
{
|
||||
m_Name = name;
|
||||
}
|
||||
|
||||
string DynamicObject::GetType(void) const
|
||||
{
|
||||
return m_Type;
|
||||
}
|
||||
|
||||
void DynamicObject::SetType(string type)
|
||||
{
|
||||
m_Type = type;
|
||||
}
|
||||
|
||||
bool DynamicObject::IsLocal(void) const
|
||||
{
|
||||
return m_Local;
|
||||
}
|
||||
|
||||
void DynamicObject::SetLocal(bool value)
|
||||
{
|
||||
m_Local = value;
|
||||
}
|
||||
|
||||
bool DynamicObject::IsAbstract(void) const
|
||||
{
|
||||
return m_Abstract;
|
||||
}
|
||||
|
||||
void DynamicObject::SetAbstract(bool value)
|
||||
{
|
||||
m_Abstract = value;
|
||||
return m_Tags;
|
||||
}
|
||||
|
||||
void DynamicObject::Commit(void)
|
||||
{
|
||||
// TODO: link properties to parent objects
|
||||
|
||||
Dictionary::Ptr resolvedProperties = m_Properties->ToFlatDictionary();
|
||||
Reload(resolvedProperties);
|
||||
DynamicObject::Ptr self = static_pointer_cast<DynamicObject>(shared_from_this());
|
||||
ObjectSet::GetAllObjects()->CheckObject(self);
|
||||
}
|
||||
|
||||
void DynamicObject::Reload(Dictionary::Ptr resolvedProperties)
|
||||
void DynamicObject::Unregister(void)
|
||||
{
|
||||
resolvedProperties->GetProperty("__name", &m_Name);
|
||||
resolvedProperties->GetProperty("__type", &m_Type);
|
||||
resolvedProperties->GetProperty("__local", &m_Local);
|
||||
resolvedProperties->GetProperty("__abstract", &m_Abstract);
|
||||
DynamicObject::Ptr self = static_pointer_cast<DynamicObject>(shared_from_this());
|
||||
ObjectSet::GetAllObjects()->RemoveObject(self);
|
||||
}
|
||||
|
@ -23,47 +23,23 @@
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
class DynamicObject : public Object
|
||||
class I2_DYN_API DynamicObject : public Object
|
||||
{
|
||||
public:
|
||||
typedef shared_ptr<DynamicObject> Ptr;
|
||||
typedef weak_ptr<DynamicObject> WeakPtr;
|
||||
|
||||
void AddParentObject(DynamicObject::Ptr parent);
|
||||
void RemoveParentObject(DynamicObject::Ptr parent);
|
||||
DynamicObject(void);
|
||||
|
||||
void AddChildObject(DynamicObject::WeakPtr parent);
|
||||
void RemoveChildObject(DynamicObject::WeakPtr parent);
|
||||
|
||||
DynamicDictionary::Ptr GetProperties(void) const;
|
||||
void SetProperties(DynamicDictionary::Ptr properties);
|
||||
|
||||
Dictionary::Ptr GetResolvedProperties(void) const;
|
||||
|
||||
string GetName(void) const;
|
||||
string GetType(void) const;
|
||||
bool IsLocal(void) const;
|
||||
bool IsAbstract(void) const;
|
||||
Dictionary::Ptr GetConfig(void) const;
|
||||
Dictionary::Ptr GetTags(void) const;
|
||||
|
||||
void Commit(void);
|
||||
|
||||
protected:
|
||||
virtual void Reload(Dictionary::Ptr resolvedProperties);
|
||||
void Unregister(void);
|
||||
|
||||
private:
|
||||
set<DynamicObject::Ptr> m_Parents;
|
||||
set<DynamicObject::WeakPtr> m_Children;
|
||||
DynamicDictionary::Ptr m_Properties;
|
||||
|
||||
string m_Type;
|
||||
string m_Name;
|
||||
bool m_Local;
|
||||
bool m_Abstract;
|
||||
|
||||
void SetName(string name);
|
||||
void SetType(string type);
|
||||
void SetLocal(bool local);
|
||||
void SetAbstract(bool abstract);
|
||||
Dictionary::Ptr m_Config;
|
||||
Dictionary::Ptr m_Tags;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -29,8 +29,15 @@
|
||||
|
||||
#include <i2-base.h>
|
||||
|
||||
#ifdef I2_DYN_BUILD
|
||||
# define I2_DYN_API I2_EXPORT
|
||||
#else /* I2_DYN_BUILD */
|
||||
# define I2_DYN_API I2_IMPORT
|
||||
#endif /* I2_DYN_BUILD */
|
||||
|
||||
#include "dynamicdictionary.h"
|
||||
#include "dynamicobject.h"
|
||||
#include "objectspace.h"
|
||||
#include "objectset.h"
|
||||
#include "objectmap.h"
|
||||
|
||||
#endif /* I2DYN_H */
|
||||
|
93
dyn/objectmap.cpp
Normal file
93
dyn/objectmap.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012 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 "i2-dyn.h"
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
ObjectMap::ObjectMap(const ObjectSet::Ptr& parent, ObjectKeyGetter keygetter)
|
||||
: m_Parent(parent), m_KeyGetter(keygetter)
|
||||
{
|
||||
assert(m_Parent);
|
||||
assert(m_KeyGetter);
|
||||
}
|
||||
|
||||
void ObjectMap::Start(void)
|
||||
{
|
||||
m_Parent->OnObjectAdded += bind_weak(&ObjectMap::ObjectAddedHandler, shared_from_this());
|
||||
m_Parent->OnObjectCommitted += bind_weak(&ObjectMap::ObjectCommittedHandler, shared_from_this());
|
||||
m_Parent->OnObjectRemoved += bind_weak(&ObjectMap::ObjectRemovedHandler, shared_from_this());
|
||||
|
||||
for (ObjectSet::Iterator it = m_Parent->Begin(); it != m_Parent->End(); it++)
|
||||
AddObject(*it);
|
||||
}
|
||||
|
||||
void ObjectMap::AddObject(const Object::Ptr& object)
|
||||
{
|
||||
string key;
|
||||
if (!m_KeyGetter(object, &key))
|
||||
return;
|
||||
|
||||
m_Objects.insert(pair<string, Object::Ptr>(key, object));
|
||||
}
|
||||
|
||||
void ObjectMap::RemoveObject(const Object::Ptr& object)
|
||||
{
|
||||
string key;
|
||||
if (!m_KeyGetter(object, &key))
|
||||
return;
|
||||
|
||||
pair<Iterator, Iterator> range = GetRange(key);
|
||||
|
||||
for (Iterator i = range.first; i != range.second; i++) {
|
||||
if (i->second == object) {
|
||||
m_Objects.erase(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectMap::CheckObject(const Object::Ptr& object)
|
||||
{
|
||||
RemoveObject(object);
|
||||
AddObject(object);
|
||||
}
|
||||
|
||||
ObjectMap::Range ObjectMap::GetRange(string key)
|
||||
{
|
||||
return m_Objects.equal_range(key);
|
||||
}
|
||||
|
||||
int ObjectMap::ObjectAddedHandler(const ObjectSetEventArgs& ea)
|
||||
{
|
||||
AddObject(ea.Object);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ObjectMap::ObjectCommittedHandler(const ObjectSetEventArgs& ea)
|
||||
{
|
||||
CheckObject(ea.Object);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ObjectMap::ObjectRemovedHandler(const ObjectSetEventArgs& ea)
|
||||
{
|
||||
RemoveObject(ea.Object);
|
||||
return 0;
|
||||
}
|
@ -17,33 +17,43 @@
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef OBJECTSPACE_H
|
||||
#define OBJECTSPACE_H
|
||||
#ifndef OBJECTMAP_H
|
||||
#define OBJECTMAP_H
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
typedef function<DynamicObject::Ptr()> DynamicObjectFactory;
|
||||
typedef function<bool (const Object::Ptr&, string *key)> ObjectKeyGetter;
|
||||
|
||||
class ObjectSpace : public Object
|
||||
class I2_DYN_API ObjectMap : public Object
|
||||
{
|
||||
public:
|
||||
void RegisterClass(string name, DynamicObjectFactory factory);
|
||||
void UnregisterClass(string name);
|
||||
typedef shared_ptr<ObjectMap> Ptr;
|
||||
typedef weak_ptr<ObjectMap> WeakPtr;
|
||||
|
||||
Dictionary::Ptr SerializeObject(DynamicObject::Ptr object);
|
||||
DynamicObject::Ptr UnserializeObject(Dictionary::Ptr serializedObject);
|
||||
typedef multimap<string, Object::Ptr>::iterator Iterator;
|
||||
typedef pair<Iterator, Iterator> Range;
|
||||
|
||||
vector<DynamicObject::Ptr> FindObjects(function<bool (DynamicObject::Ptr)> predicate);
|
||||
ObjectMap(const ObjectSet::Ptr& parent, ObjectKeyGetter keygetter);
|
||||
|
||||
void Start(void);
|
||||
|
||||
Range GetRange(string key);
|
||||
|
||||
private:
|
||||
map<string, DynamicObjectFactory> m_Classes;
|
||||
set<DynamicObject::Ptr> m_Objects;
|
||||
multimap<string, Object::Ptr> m_Objects;
|
||||
ObjectSet::Ptr m_Parent;
|
||||
ObjectKeyGetter m_KeyGetter;
|
||||
|
||||
void RegisterObject(DynamicObject::Ptr object);
|
||||
void UnregisterObject(DynamicObject::Ptr object);
|
||||
void AddObject(const Object::Ptr& object);
|
||||
void RemoveObject(const Object::Ptr& object);
|
||||
void CheckObject(const Object::Ptr& object);
|
||||
|
||||
int ObjectAddedHandler(const ObjectSetEventArgs& ea);
|
||||
int ObjectCommittedHandler(const ObjectSetEventArgs& ea);
|
||||
int ObjectRemovedHandler(const ObjectSetEventArgs& ea);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* OBJECTSPACE_H */
|
||||
#endif OBJECTMAP_H
|
124
dyn/objectset.cpp
Normal file
124
dyn/objectset.cpp
Normal file
@ -0,0 +1,124 @@
|
||||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012 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 "i2-dyn.h"
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
ObjectSet::ObjectSet(void)
|
||||
: m_Parent(), m_Filter()
|
||||
{
|
||||
}
|
||||
|
||||
ObjectSet::ObjectSet(const ObjectSet::Ptr& parent, ObjectPredicate filter)
|
||||
: m_Parent(parent), m_Filter(filter)
|
||||
{
|
||||
}
|
||||
|
||||
void ObjectSet::Start(void)
|
||||
{
|
||||
if (m_Parent) {
|
||||
m_Parent->OnObjectCommitted += bind_weak(&ObjectSet::ObjectCommittedHandler, shared_from_this());
|
||||
m_Parent->OnObjectRemoved += bind_weak(&ObjectSet::ObjectRemovedHandler, shared_from_this());
|
||||
|
||||
for (ObjectSet::Iterator it = m_Parent->Begin(); it != m_Parent->End(); it++)
|
||||
CheckObject(*it);
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectSet::AddObject(const Object::Ptr& object)
|
||||
{
|
||||
m_Objects.insert(object);
|
||||
|
||||
ObjectSetEventArgs ea;
|
||||
ea.Source = shared_from_this();
|
||||
ea.Object = object;
|
||||
OnObjectAdded(ea);
|
||||
}
|
||||
|
||||
void ObjectSet::RemoveObject(const Object::Ptr& object)
|
||||
{
|
||||
ObjectSet::Iterator it = m_Objects.find(object);
|
||||
|
||||
if (it != m_Objects.end()) {
|
||||
m_Objects.erase(it);
|
||||
|
||||
ObjectSetEventArgs ea;
|
||||
ea.Source = shared_from_this();
|
||||
ea.Object = object;
|
||||
OnObjectRemoved(ea);
|
||||
}
|
||||
}
|
||||
|
||||
bool ObjectSet::Contains(const Object::Ptr& object) const
|
||||
{
|
||||
ObjectSet::Iterator it = m_Objects.find(object);
|
||||
|
||||
return !(it == m_Objects.end());
|
||||
}
|
||||
|
||||
void ObjectSet::CheckObject(const Object::Ptr& object)
|
||||
{
|
||||
if (m_Filter && !m_Filter(object))
|
||||
RemoveObject(object);
|
||||
else {
|
||||
if (!Contains(object))
|
||||
AddObject(object);
|
||||
else {
|
||||
ObjectSetEventArgs ea;
|
||||
ea.Source = shared_from_this();
|
||||
ea.Object = object;
|
||||
OnObjectCommitted(ea);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ObjectSet::ObjectCommittedHandler(const ObjectSetEventArgs& ea)
|
||||
{
|
||||
CheckObject(ea.Object);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ObjectSet::ObjectRemovedHandler(const ObjectSetEventArgs& ea)
|
||||
{
|
||||
RemoveObject(ea.Object);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ObjectSet::Iterator ObjectSet::Begin(void)
|
||||
{
|
||||
return m_Objects.begin();
|
||||
}
|
||||
|
||||
ObjectSet::Iterator ObjectSet::End(void)
|
||||
{
|
||||
return m_Objects.end();
|
||||
}
|
||||
|
||||
ObjectSet::Ptr ObjectSet::GetAllObjects(void)
|
||||
{
|
||||
static ObjectSet::Ptr allObjects;
|
||||
|
||||
if (!allObjects) {
|
||||
allObjects = make_shared<ObjectSet>();
|
||||
allObjects->Start();
|
||||
}
|
||||
|
||||
return allObjects;
|
||||
}
|
73
dyn/objectset.h
Normal file
73
dyn/objectset.h
Normal file
@ -0,0 +1,73 @@
|
||||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012 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 OBJECTSET_H
|
||||
#define OBJECTSET_H
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
struct ObjectSetEventArgs : public EventArgs
|
||||
{
|
||||
Object::Ptr Object;
|
||||
};
|
||||
|
||||
typedef function<bool (const Object::Ptr&)> ObjectPredicate;
|
||||
|
||||
class I2_DYN_API ObjectSet : public Object
|
||||
{
|
||||
public:
|
||||
typedef shared_ptr<ObjectSet> Ptr;
|
||||
typedef weak_ptr<ObjectSet> WeakPtr;
|
||||
|
||||
typedef set<Object::Ptr>::iterator Iterator;
|
||||
|
||||
ObjectSet(void);
|
||||
ObjectSet(const ObjectSet::Ptr& parent, ObjectPredicate filter);
|
||||
|
||||
void Start(void);
|
||||
|
||||
void AddObject(const Object::Ptr& object);
|
||||
void RemoveObject(const Object::Ptr& object);
|
||||
bool Contains(const Object::Ptr& object) const;
|
||||
|
||||
void CheckObject(const Object::Ptr& object);
|
||||
|
||||
Observable<ObjectSetEventArgs> OnObjectAdded;
|
||||
Observable<ObjectSetEventArgs> OnObjectCommitted;
|
||||
Observable<ObjectSetEventArgs> OnObjectRemoved;
|
||||
|
||||
Iterator Begin(void);
|
||||
Iterator End(void);
|
||||
|
||||
static ObjectSet::Ptr GetAllObjects(void);
|
||||
|
||||
private:
|
||||
set<Object::Ptr> m_Objects;
|
||||
|
||||
ObjectSet::Ptr m_Parent;
|
||||
ObjectPredicate m_Filter;
|
||||
|
||||
int ObjectCommittedHandler(const ObjectSetEventArgs& ea);
|
||||
int ObjectRemovedHandler(const ObjectSetEventArgs& ea);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* OBJECTSET_H */
|
@ -1,91 +0,0 @@
|
||||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012 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 "i2-dyn.h"
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
void ObjectSpace::RegisterClass(string name, DynamicObjectFactory factory)
|
||||
{
|
||||
m_Classes[name] = factory;
|
||||
}
|
||||
|
||||
void ObjectSpace::UnregisterClass(string name)
|
||||
{
|
||||
map<string, DynamicObjectFactory>::iterator ci = m_Classes.find(name);
|
||||
|
||||
if (ci != m_Classes.end())
|
||||
m_Classes.erase(ci);
|
||||
}
|
||||
|
||||
void ObjectSpace::RegisterObject(DynamicObject::Ptr object)
|
||||
{
|
||||
m_Objects.insert(object);
|
||||
}
|
||||
|
||||
void ObjectSpace::UnregisterObject(DynamicObject::Ptr object)
|
||||
{
|
||||
set<DynamicObject::Ptr>::iterator di = m_Objects.find(object);
|
||||
|
||||
if (di != m_Objects.end())
|
||||
m_Objects.erase(di);
|
||||
}
|
||||
|
||||
Dictionary::Ptr ObjectSpace::SerializeObject(DynamicObject::Ptr object)
|
||||
{
|
||||
DynamicDictionary::Ptr properties = object->GetProperties();
|
||||
if (!properties)
|
||||
throw InvalidArgumentException();
|
||||
|
||||
Dictionary::Ptr data = make_shared<Dictionary>();
|
||||
data->SetProperty("type", object->GetType());
|
||||
|
||||
Dictionary::Ptr serializedProperties = properties->Serialize();
|
||||
data->SetProperty("properties", serializedProperties);
|
||||
return data;
|
||||
}
|
||||
|
||||
DynamicObject::Ptr ObjectSpace::UnserializeObject(Dictionary::Ptr data)
|
||||
{
|
||||
string type;
|
||||
if (!data->GetProperty("type", &type))
|
||||
throw InvalidArgumentException();
|
||||
|
||||
Dictionary::Ptr serializedProperties;
|
||||
if (!data->GetProperty("properties", &serializedProperties))
|
||||
throw InvalidArgumentException();
|
||||
DynamicDictionary::Ptr properties = make_shared<DynamicDictionary>(serializedProperties);
|
||||
|
||||
map<string, DynamicObjectFactory>::iterator di = m_Classes.find(type);
|
||||
DynamicObject::Ptr object;
|
||||
if (di != m_Classes.end())
|
||||
object = di->second();
|
||||
else
|
||||
object = make_shared<DynamicObject>();
|
||||
|
||||
object->SetProperties(properties);
|
||||
object->Commit();
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
vector<DynamicObject::Ptr> FindObjects(function<bool (DynamicObject::Ptr)> predicate)
|
||||
{
|
||||
return vector<DynamicObject::Ptr>();
|
||||
}
|
37
dyntest/dyntest.cpp
Normal file
37
dyntest/dyntest.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <i2-dyn.h>
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
bool foogetter(const Object::Ptr& object, string *key)
|
||||
{
|
||||
DynamicObject::Ptr dobj = dynamic_pointer_cast<DynamicObject>(object);
|
||||
return dobj->GetConfig()->GetProperty("foo", key);
|
||||
}
|
||||
|
||||
bool foo(const Object::Ptr& object)
|
||||
{
|
||||
DynamicObject::Ptr dobj = dynamic_pointer_cast<DynamicObject>(object);
|
||||
|
||||
string value;
|
||||
return dobj->GetConfig()->GetProperty("foo", &value);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
DynamicObject::Ptr dobj = make_shared<DynamicObject>();
|
||||
dobj->GetConfig()->SetProperty("foo", "bar");
|
||||
dobj->Commit();
|
||||
}
|
||||
|
||||
ObjectSet::Ptr filtered = make_shared<ObjectSet>(ObjectSet::GetAllObjects(), &foo);
|
||||
filtered->Start();
|
||||
|
||||
ObjectMap::Ptr m = make_shared<ObjectMap>(ObjectSet::GetAllObjects(), &foogetter);
|
||||
m->Start();
|
||||
|
||||
ObjectMap::Range range = m->GetRange("bar");
|
||||
cout << distance(range.first, range.second) << " elements" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
87
dyntest/dyntest.vcxproj
Normal file
87
dyntest/dyntest.vcxproj
Normal file
@ -0,0 +1,87 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{E6FA740D-0939-4711-AFBC-3D9E913510A1}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>dyntest</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\dyn;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\dyn;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>base.lib;dyn.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>base.lib;dyn.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="dyntest.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
35
icinga.sln
35
icinga.sln
@ -1,6 +1,6 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 11.00
|
||||
# Visual C++ Express 2010
|
||||
# Visual Studio 2010
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "base", "base\base.vcxproj", "{9C92DA90-FD53-43A9-A244-90F2E8AF9677}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345} = {19CBCE06-3F5C-479A-BD75-E2AB6215D345}
|
||||
@ -12,8 +12,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsonrpc", "jsonrpc\jsonrpc.
|
||||
{9C92DA90-FD53-43A9-A244-90F2E8AF9677} = {9C92DA90-FD53-43A9-A244-90F2E8AF9677}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cJSON", "cJSON\cJSON.vcxproj", "{66BED474-C33F-48F9-90BA-BBCFEDC006B8}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "icinga", "icinga\icinga.vcxproj", "{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{9C92DA90-FD53-43A9-A244-90F2E8AF9677} = {9C92DA90-FD53-43A9-A244-90F2E8AF9677}
|
||||
@ -49,13 +47,20 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "discovery", "components\dis
|
||||
{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8} = {C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mmatch", "mmatch\mmatch.vcxproj", "{19CBCE06-3F5C-479A-BD75-E2AB6215D345}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dyn", "dyn\dyn.vcxproj", "{B26AFFA6-2BDF-42E6-A224-2591FFD9BFB7}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{9C92DA90-FD53-43A9-A244-90F2E8AF9677} = {9C92DA90-FD53-43A9-A244-90F2E8AF9677}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cJSON", "third-party\cJSON\cJSON.vcxproj", "{66BED474-C33F-48F9-90BA-BBCFEDC006B8}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mmatch", "third-party\mmatch\mmatch.vcxproj", "{19CBCE06-3F5C-479A-BD75-E2AB6215D345}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dyntest", "dyntest\dyntest.vcxproj", "{E6FA740D-0939-4711-AFBC-3D9E913510A1}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{B26AFFA6-2BDF-42E6-A224-2591FFD9BFB7} = {B26AFFA6-2BDF-42E6-A224-2591FFD9BFB7}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
@ -70,10 +75,6 @@ Global
|
||||
{8DD52FAC-ECEE-48C2-B266-E7C47ED485F8}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{8DD52FAC-ECEE-48C2-B266-E7C47ED485F8}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{8DD52FAC-ECEE-48C2-B266-E7C47ED485F8}.Release|Win32.Build.0 = Release|Win32
|
||||
{66BED474-C33F-48F9-90BA-BBCFEDC006B8}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{66BED474-C33F-48F9-90BA-BBCFEDC006B8}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{66BED474-C33F-48F9-90BA-BBCFEDC006B8}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{66BED474-C33F-48F9-90BA-BBCFEDC006B8}.Release|Win32.Build.0 = Release|Win32
|
||||
{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}.Release|Win32.ActiveCfg = Release|Win32
|
||||
@ -98,14 +99,22 @@ Global
|
||||
{EAD41628-BB96-4F99-9070-8A9676801295}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{EAD41628-BB96-4F99-9070-8A9676801295}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{EAD41628-BB96-4F99-9070-8A9676801295}.Release|Win32.Build.0 = Release|Win32
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345}.Release|Win32.Build.0 = Release|Win32
|
||||
{B26AFFA6-2BDF-42E6-A224-2591FFD9BFB7}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{B26AFFA6-2BDF-42E6-A224-2591FFD9BFB7}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{B26AFFA6-2BDF-42E6-A224-2591FFD9BFB7}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{B26AFFA6-2BDF-42E6-A224-2591FFD9BFB7}.Release|Win32.Build.0 = Release|Win32
|
||||
{66BED474-C33F-48F9-90BA-BBCFEDC006B8}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{66BED474-C33F-48F9-90BA-BBCFEDC006B8}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{66BED474-C33F-48F9-90BA-BBCFEDC006B8}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{66BED474-C33F-48F9-90BA-BBCFEDC006B8}.Release|Win32.Build.0 = Release|Win32
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{19CBCE06-3F5C-479A-BD75-E2AB6215D345}.Release|Win32.Build.0 = Release|Win32
|
||||
{E6FA740D-0939-4711-AFBC-3D9E913510A1}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{E6FA740D-0939-4711-AFBC-3D9E913510A1}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{E6FA740D-0939-4711-AFBC-3D9E913510A1}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{E6FA740D-0939-4711-AFBC-3D9E913510A1}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -55,11 +55,11 @@
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\cJSON;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\third-party\cJSON;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\cJSON;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\third-party\cJSON;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
|
Loading…
x
Reference in New Issue
Block a user