13 template <
typename TArgs>
17 typedef typename TArgs::TId TId;
18 typedef typename TArgs::TModel TModel;
19 typedef typename TArgs::TView TView;
20 typedef typename TArgs::TViewFactory TViewFactory;
21 typedef typename TArgs::TRenderableFilter TRenderableFilter;
23 typedef std::map<TModel*, TView*> TModelToView;
27 : m_viewFactory(viewFactory)
28 , m_viewRenderer(renderableFilter)
34 for(
typename TModelToView::iterator iter = m_modelToView.begin(); iter != m_modelToView.end(); ++iter)
36 TView* pView = iter->second;
37 m_viewRenderer.RemoveView(*pView);
38 m_viewFactory.FreeView(pView);
40 m_modelToView.clear();
43 void Add(TModel& model)
45 TView* pView = m_viewFactory.CreateView(model);
46 m_viewRenderer.AddView(*pView);
47 m_modelToView[&model] = pView;
50 void Remove(TModel& model)
53 m_modelToView.find(&model) != m_modelToView.end(),
54 "Cannot remove, as it has not been added");
56 TView* pView = m_modelToView[&model];
58 m_modelToView.erase(&model);
59 m_viewRenderer.RemoveView(*pView);
60 m_viewFactory.FreeView(pView);
65 HasId(
const TId&
id) : m_id(
id) { }
66 bool operator() (
const std::pair<TModel*, TView*>& pair) {
return pair.first->GetId() == m_id; }
70 bool TryRemoveById(
const TId&
id, TModel*& out_removedItem)
72 typename std::map<TModel*, TView*>::iterator iter = std::find_if(
73 m_modelToView.begin(), m_modelToView.end(),
HasId(
id));
75 if(iter == m_modelToView.end())
77 out_removedItem = NULL;
81 const std::pair<TModel*, TView*>& pair = *iter;
82 TModel* pModel = pair.first;
84 out_removedItem = pModel;
88 TViewFactory& GetViewFactory()
const {
return m_viewFactory; }
89 TRenderableFilter& GetRenderer()
const {
return m_viewRenderer; }
92 TModelToView m_modelToView;
95 TViewFactory& m_viewFactory;
96 TRenderableFilter& m_viewRenderer;