2012-04-04 10:04:38 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* SetHive
|
|
|
|
*
|
|
|
|
* Sets the hive this collection belongs to.
|
|
|
|
*
|
|
|
|
* @param hive The hive.
|
|
|
|
*/
|
2012-04-04 10:04:38 +02:00
|
|
|
void ConfigCollection::SetHive(const ConfigHive::WeakPtr& hive)
|
|
|
|
{
|
|
|
|
m_Hive = hive;
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* GetHive
|
|
|
|
*
|
|
|
|
* Retrieves the hive this collection belongs to.
|
|
|
|
*
|
|
|
|
* @returns The hive.
|
|
|
|
*/
|
2012-04-04 10:04:38 +02:00
|
|
|
ConfigHive::WeakPtr ConfigCollection::GetHive(void) const
|
|
|
|
{
|
|
|
|
return m_Hive;
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* AddObject
|
|
|
|
*
|
|
|
|
* Adds a new object to this collection.
|
|
|
|
*
|
|
|
|
* @param object The new object.
|
|
|
|
*/
|
2012-04-04 10:04:38 +02:00
|
|
|
void ConfigCollection::AddObject(const ConfigObject::Ptr& object)
|
|
|
|
{
|
2012-04-20 13:49:04 +02:00
|
|
|
RemoveObject(object);
|
|
|
|
|
2012-04-04 10:04:38 +02:00
|
|
|
Objects[object->GetName()] = object;
|
2012-05-09 10:15:51 +02:00
|
|
|
object->Commit();
|
2012-04-04 10:04:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* RemoveObject
|
|
|
|
*
|
|
|
|
* Removes an object from this collection
|
|
|
|
*
|
|
|
|
* @param object The object that is to be removed.
|
|
|
|
*/
|
2012-04-04 10:04:38 +02:00
|
|
|
void ConfigCollection::RemoveObject(const ConfigObject::Ptr& object)
|
|
|
|
{
|
|
|
|
ObjectIterator oi = Objects.find(object->GetName());
|
|
|
|
|
|
|
|
if (oi != Objects.end()) {
|
|
|
|
Objects.erase(oi);
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
EventArgs ea;
|
2012-04-18 15:22:25 +02:00
|
|
|
ea.Source = object;
|
2012-04-04 10:04:38 +02:00
|
|
|
OnObjectRemoved(ea);
|
|
|
|
|
|
|
|
ConfigHive::Ptr hive = m_Hive.lock();
|
|
|
|
if (hive)
|
|
|
|
hive->OnObjectRemoved(ea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* GetObject
|
|
|
|
*
|
|
|
|
* Retrieves an object by name.
|
|
|
|
*
|
|
|
|
* @param name The name of the object.
|
|
|
|
* @returns The object or a null pointer if the specified object
|
|
|
|
* could not be found.
|
|
|
|
*/
|
|
|
|
ConfigObject::Ptr ConfigCollection::GetObject(const string& name) const
|
2012-04-04 10:04:38 +02:00
|
|
|
{
|
2012-04-24 14:02:15 +02:00
|
|
|
ObjectConstIterator oi = Objects.find(name);
|
2012-04-04 10:04:38 +02:00
|
|
|
|
|
|
|
if (oi == Objects.end())
|
|
|
|
return ConfigObject::Ptr();
|
|
|
|
|
|
|
|
return oi->second;
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* ForEachObject
|
|
|
|
*
|
|
|
|
* Invokes the specified callback for each object contained in this collection.
|
|
|
|
*
|
|
|
|
* @param callback The callback.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
void ConfigCollection::ForEachObject(function<int (const EventArgs&)> callback)
|
2012-04-04 10:04:38 +02:00
|
|
|
{
|
2012-04-20 13:49:04 +02:00
|
|
|
EventArgs ea;
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-04-04 10:04:38 +02:00
|
|
|
for (ObjectIterator oi = Objects.begin(); oi != Objects.end(); oi++) {
|
2012-04-18 15:22:25 +02:00
|
|
|
ea.Source = oi->second;
|
2012-04-04 10:04:38 +02:00
|
|
|
callback(ea);
|
|
|
|
}
|
|
|
|
}
|