1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
26 template<typename TKey = string, typename TValue = Object::Ptr>
27 class ObjectMap : public Object
30 typedef shared_ptr<ObjectMap<TKey, TValue> > Ptr;
31 typedef weak_ptr<ObjectMap<TKey, TValue > > WeakPtr;
33 typedef typename multimap<TKey, TValue>::iterator Iterator;
34 typedef pair<Iterator, Iterator> Range;
36 ObjectMap(const typename ObjectSet<TValue>::Ptr& parent,
37 function<bool (const TValue&, TKey *key)> keygetter)
38 : m_Parent(parent), m_KeyGetter(keygetter)
46 m_Parent->OnObjectAdded += bind_weak(&ObjectMap::ObjectAddedHandler, shared_from_this());
47 m_Parent->OnObjectCommitted += bind_weak(&ObjectMap::ObjectCommittedHandler, shared_from_this());
48 m_Parent->OnObjectRemoved += bind_weak(&ObjectMap::ObjectRemovedHandler, shared_from_this());
50 for (typename ObjectSet<TValue>::Iterator it = m_Parent->Begin(); it != m_Parent->End(); it++)
54 Range GetRange(TKey key)
56 return m_Objects.equal_range(key);
59 void ForeachObject(TKey key, function<int (const ObjectSetEventArgs<TValue>&)> callback)
61 ObjectSetEventArgs<TValue> ea;
62 ea.Source = shared_from_this();
64 Range range = GetRange(key);
66 for (Iterator it = range.first; it != range.second; it++) {
73 multimap<TKey, TValue> m_Objects;
74 typename ObjectSet<TValue>::Ptr m_Parent;
75 function<bool (const TValue&, TKey *key)> m_KeyGetter;
77 void AddObject(const TValue& object)
80 if (!m_KeyGetter(object, &key))
83 m_Objects.insert(make_pair(key, object));
86 void RemoveObject(const TValue& object)
89 if (!m_KeyGetter(object, &key))
92 pair<Iterator, Iterator> range = GetRange(key);
94 for (Iterator i = range.first; i != range.second; i++) {
95 if (i->second == object) {
102 void CheckObject(const TValue& object)
104 RemoveObject(object);
108 int ObjectAddedHandler(const ObjectSetEventArgs<TValue>& ea)
110 AddObject(ea.Target);
115 int ObjectCommittedHandler(const ObjectSetEventArgs<TValue>& ea)
117 CheckObject(ea.Target);
122 int ObjectRemovedHandler(const ObjectSetEventArgs<TValue>& ea)
124 RemoveObject(ea.Target);
132 #endif /* OBJECTMAP_H */