2012-05-25 11:10:11 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-05-30 10:43:58 +02:00
|
|
|
#ifndef OBJECTSET_H
|
|
|
|
#define OBJECTSET_H
|
2012-05-25 11:10:11 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2012-06-05 15:05:15 +02:00
|
|
|
template<typename TValue>
|
2012-06-12 09:36:18 +02:00
|
|
|
class ObjectSet : public Object
|
2012-05-25 11:10:11 +02:00
|
|
|
{
|
|
|
|
public:
|
2012-06-05 15:05:15 +02:00
|
|
|
typedef shared_ptr<ObjectSet<TValue> > Ptr;
|
|
|
|
typedef weak_ptr<ObjectSet<TValue> > WeakPtr;
|
|
|
|
|
|
|
|
typedef typename set<TValue>::iterator Iterator;
|
|
|
|
|
|
|
|
ObjectSet(void)
|
|
|
|
: m_Parent(), m_Predicate()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
ObjectSet(const typename ObjectSet<TValue>::Ptr& parent, function<bool(const TValue&)> predicate)
|
|
|
|
: m_Parent(parent), m_Predicate(predicate)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void Start(void)
|
|
|
|
{
|
|
|
|
if (m_Parent) {
|
2012-06-16 03:42:54 +02:00
|
|
|
m_Parent->OnObjectAdded.connect(boost::bind(&ObjectSet::ObjectAddedOrCommittedHandler, this, _2));
|
|
|
|
m_Parent->OnObjectCommitted.connect(boost::bind(&ObjectSet::ObjectAddedOrCommittedHandler, this, _2));
|
|
|
|
m_Parent->OnObjectRemoved.connect(boost::bind(&ObjectSet::ObjectRemovedHandler, this, _2));
|
2012-06-05 15:05:15 +02:00
|
|
|
|
2012-07-16 22:00:50 +02:00
|
|
|
BOOST_FOREACH(const TValue& object, m_Parent) {
|
|
|
|
CheckObject(object);
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddObject(const TValue& object)
|
|
|
|
{
|
|
|
|
m_Objects.insert(object);
|
2012-06-16 13:06:06 +02:00
|
|
|
OnObjectAdded(GetSelf(), object);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveObject(const TValue& object)
|
|
|
|
{
|
|
|
|
ObjectSet::Iterator it = m_Objects.find(object);
|
|
|
|
|
|
|
|
if (it != m_Objects.end()) {
|
|
|
|
m_Objects.erase(it);
|
2012-06-16 13:06:06 +02:00
|
|
|
OnObjectRemoved(GetSelf(), object);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Contains(const TValue& object) const
|
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
return !(m_Objects.find(object) == m_Objects.end());
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckObject(const TValue& object)
|
|
|
|
{
|
|
|
|
if (m_Predicate && !m_Predicate(object)) {
|
|
|
|
RemoveObject(object);
|
|
|
|
} else {
|
|
|
|
if (!Contains(object)) {
|
|
|
|
AddObject(object);
|
|
|
|
} else {
|
2012-06-16 13:06:06 +02:00
|
|
|
OnObjectCommitted(GetSelf(), object);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
boost::signal<void (const typename ObjectSet<TValue>::Ptr&, const TValue&)> OnObjectAdded;
|
|
|
|
boost::signal<void (const typename ObjectSet<TValue>::Ptr&, const TValue&)> OnObjectCommitted;
|
|
|
|
boost::signal<void (const typename ObjectSet<TValue>::Ptr&, const TValue&)> OnObjectRemoved;
|
2012-06-05 15:05:15 +02:00
|
|
|
|
|
|
|
Iterator Begin(void)
|
|
|
|
{
|
|
|
|
return m_Objects.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator End(void)
|
|
|
|
{
|
|
|
|
return m_Objects.end();
|
|
|
|
}
|
2012-05-30 10:43:58 +02:00
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void ForeachObject(function<void (const typename Object::Ptr&, const TValue&)> callback)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2012-07-16 22:00:50 +02:00
|
|
|
BOOST_FOREACH(const TValue& object, m_Objects) {
|
|
|
|
callback(GetSelf(), object);
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-05 15:05:15 +02:00
|
|
|
private:
|
|
|
|
set<TValue> m_Objects;
|
2012-05-30 10:43:58 +02:00
|
|
|
|
2012-06-11 14:04:16 +02:00
|
|
|
typename ObjectSet<TValue>::Ptr m_Parent;
|
2012-06-05 15:05:15 +02:00
|
|
|
function<bool (const TValue&)> m_Predicate;
|
2012-05-30 10:43:58 +02:00
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void ObjectAddedOrCommittedHandler(const TValue& object)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-06-16 03:42:54 +02:00
|
|
|
CheckObject(object);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
2012-05-25 11:10:11 +02:00
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void ObjectRemovedHandler(const TValue& object)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-06-16 03:42:54 +02:00
|
|
|
RemoveObject(object);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
2012-05-25 11:10:11 +02:00
|
|
|
};
|
|
|
|
|
2012-07-16 22:00:50 +02:00
|
|
|
template<typename TValue>
|
|
|
|
typename ObjectSet<TValue>::Iterator range_begin(shared_ptr<ObjectSet<TValue> > x)
|
|
|
|
{
|
|
|
|
return x->Begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TValue>
|
|
|
|
typename ObjectSet<TValue>::Iterator range_end(shared_ptr<ObjectSet<TValue> > x)
|
|
|
|
{
|
|
|
|
return x->End();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename TValue>
|
|
|
|
struct range_mutable_iterator<shared_ptr<icinga::ObjectSet<TValue> > >
|
|
|
|
{
|
|
|
|
typedef typename icinga::ObjectSet<TValue>::Iterator type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename TValue>
|
|
|
|
struct range_const_iterator<shared_ptr<icinga::ObjectSet<TValue> > >
|
|
|
|
{
|
|
|
|
typedef typename icinga::ObjectSet<TValue>::Iterator type;
|
|
|
|
};
|
|
|
|
|
2012-05-25 11:10:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 15:05:15 +02:00
|
|
|
#endif /* OBJECTSET_H */
|