bakery  2.6
View_Composite.h
Go to the documentation of this file.
1 /*
2  * Copyright 2000 Murray Cumming
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #ifndef BAKERY_VIEW_COMPOSITE_H
20 #define BAKERY_VIEW_COMPOSITE_H
21 
22 #include "bakery/View/View.h"
23 #include <vector>
24 #include <algorithm> //For std::find
25 
26 namespace Bakery
27 {
28 
32 template< class T_Document >
33 class View_Composite : public View<T_Document>
34 {
35 public:
37  {
38  }
39 
40  virtual ~View_Composite()
41  {
42  }
43 
45 
46  virtual void add_view(type_view* pView)
47  {
48  //Ensure that the view has the same document:
49  //This should be unnecessary.
50  if(pView)
51  {
53 
54  //Add it to the list of child views:
55  m_vecViews.push_back(pView);
56  }
57  }
58 
59  virtual void remove_view(type_view* pView)
60  {
61  typename type_vecViews::iterator iter = std::find(m_vecViews.begin(), m_vecViews.end(), pView);
62  if(iter != m_vecViews.end())
63  m_vecViews.erase(iter);
64  }
65 
66  virtual void set_document(T_Document* pDocument)
67  {
68  //Call base class:
70 
71  //Change the document in the child views.
72  for(typename type_vecViews::iterator iter = m_vecViews.begin(); iter != m_vecViews.end(); iter++)
73  {
74  type_view* pView = *iter;
75  if(pView)
76  pView->set_document(pDocument);
77  }
78  }
79 
80  virtual void load_from_document()
81  {
82  //Delegate to the child views:
83  for(typename type_vecViews::iterator iter = m_vecViews.begin(); iter != m_vecViews.end(); iter++)
84  {
85  type_view* pView = *iter;
86  if(pView)
87  pView->load_from_document();
88  }
89  }
90 
91  virtual void save_to_document()
92  {
93  //Delegate to the child views:
94  for(typename type_vecViews::iterator iter = m_vecViews.begin(); iter != m_vecViews.end(); iter++)
95  {
96  type_view* pView = *iter;
97  if(pView)
98  pView->save_to_document();
99  }
100  }
101 
102 protected:
103  typedef std::vector<type_view*> type_vecViews;
105 };
106 
107 } //namespace
108 
109 #endif