/****************************************************************************** * 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 { template struct ObjectSetEventArgs : public EventArgs { TValue Target; }; template class ObjectSet : public Object { public: typedef shared_ptr > Ptr; typedef weak_ptr > WeakPtr; typedef typename set::iterator Iterator; ObjectSet(void) : m_Parent(), m_Predicate() { } ObjectSet(const typename ObjectSet::Ptr& parent, function predicate) : m_Parent(parent), m_Predicate(predicate) { } void Start(void) { if (m_Parent) { m_Parent->OnObjectAdded.connect(bind(&ObjectSet::ObjectAddedOrCommittedHandler, this, _1)); m_Parent->OnObjectCommitted.connect(bind(&ObjectSet::ObjectAddedOrCommittedHandler, this, _1)); m_Parent->OnObjectRemoved.connect(bind(&ObjectSet::ObjectRemovedHandler, this, _1)); for (ObjectSet::Iterator it = m_Parent->Begin(); it != m_Parent->End(); it++) CheckObject(*it); } } void AddObject(const TValue& object) { m_Objects.insert(object); ObjectSetEventArgs ea; ea.Source = shared_from_this(); ea.Target = object; OnObjectAdded(ea); } void RemoveObject(const TValue& 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.Target = object; OnObjectRemoved(ea); } } bool Contains(const TValue& object) const { ObjectSet::Iterator it = m_Objects.find(object); return !(it == m_Objects.end()); } void CheckObject(const TValue& object) { if (m_Predicate && !m_Predicate(object)) { RemoveObject(object); } else { if (!Contains(object)) { AddObject(object); } else { ObjectSetEventArgs ea; ea.Source = shared_from_this(); ea.Target = object; OnObjectCommitted(ea); } } } boost::signal&)> OnObjectAdded; boost::signal&)> OnObjectCommitted; boost::signal&)> OnObjectRemoved; Iterator Begin(void) { return m_Objects.begin(); } Iterator End(void) { return m_Objects.end(); } void ForeachObject(function&)> callback) { ObjectSetEventArgs ea; ea.Source = shared_from_this(); for (Iterator it = Begin(); it != End(); it++) { ea.Target(*it); callback(ea); } } private: set m_Objects; typename ObjectSet::Ptr m_Parent; function m_Predicate; int ObjectAddedOrCommittedHandler(const ObjectSetEventArgs& ea) { CheckObject(ea.Target); return 0; } int ObjectRemovedHandler(const ObjectSetEventArgs& ea) { RemoveObject(ea.Target); return 0; } }; } #endif /* OBJECTSET_H */