5 #include "IModelRepository.h"
6 #include "IModelRepositoryObserver.h"
16 template <
typename TKey,
typename TValue>
21 typedef std::map<TKey, TValue> TStore;
24 typedef typename ThisType::KeyType KeyType;
25 typedef typename ThisType::ItemType ItemType;
26 typedef typename ThisType::ObserverType ObserverType;
27 typedef typename ThisType::IteratorType IteratorType;
31 return static_cast<int>(m_store.size());
34 bool Contains(
const KeyType& key)
const
36 return m_store.find(key) != m_store.end();
39 TValue Get(
const KeyType& key)
const
41 Eegeo_ASSERT(Contains(key));
43 return m_store.at(key);
46 void Add(
const KeyType& key,
const ItemType& value)
48 Eegeo_ASSERT(!Contains(key));
50 m_store.insert(std::make_pair(key, value));
51 NotifyItemAdded(value);
54 void Remove(
const KeyType& key)
56 const ItemType& item = Get(key);
58 NotifyItemRemoved(item);
66 void AllKeys(std::vector<KeyType>& out_allKeys)
const
69 out_allKeys.reserve(m_store.size());
71 for (
typename TStore::const_iterator iter = m_store.begin(); iter != m_store.end(); ++iter)
73 out_allKeys.push_back(iter->first);
78 void AllItems(std::vector<ItemType>& out_allItems)
const
81 out_allItems.reserve(m_store.size());
83 for (
typename TStore::const_iterator iter = m_store.begin(); iter != m_store.end(); ++iter)
85 out_allItems.push_back(iter->second);
89 IteratorType Begin()
const
91 return m_store.begin();
94 IteratorType End()
const
99 void AddObserver(ObserverType& observer)
101 m_observers.push_back(&observer);
104 void RemoveObserver(ObserverType& observer)
106 m_observers.erase(std::remove(m_observers.begin(), m_observers.end(), &observer));
109 typedef std::vector<ObserverType*> TObservers;
111 void NotifyItemAdded(
const ItemType& item)
const
113 for(
typename TObservers::const_iterator iter = m_observers.begin(); iter != m_observers.end(); ++iter)
115 (*iter)->OnItemAdded(item);
119 void NotifyItemRemoved(
const ItemType& item)
const
121 for(
typename TObservers::const_iterator iter = m_observers.begin(); iter != m_observers.end(); ++iter)
123 (*iter)->OnItemRemoved(item);
128 TObservers m_observers;