libyui  3.3.1
YChildrenManager.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YChildrenManager.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YChildrenManager_h
26 #define YChildrenManager_h
27 
28 #include <list>
29 #include <algorithm>
30 #include "YUIException.h"
31 
32 
33 /**
34  * Abstract base template class for children management, such as child
35  * widgets.
36  **/
37 template<class T> class YChildrenManager
38 {
39 public:
40 
41  /**
42  * Constructor.
43  *
44  * 'containerParent' is the class whose children are managed.
45  **/
46  YChildrenManager( T * containerParent )
47  : _container( containerParent )
48  {}
49 
50  /**
51  * Destructor.
52  **/
53  virtual ~YChildrenManager() {}
54 
55 
56  typedef std::list<T *> ChildrenList;
57 
58  /**
59  * Check if there are any children.
60  **/
61  bool hasChildren() const { return ! empty(); }
62 
63  /**
64  * Check if the children list is empty, i.e. if there are no children.
65  **/
66  bool empty() const { return _children.empty(); }
67 
68  /**
69  * Returns the number of children.
70  **/
71  int count() const { return _children.size(); }
72 
73  /**
74  * Return an iterator that points to the first child.
75  **/
76  typename ChildrenList::iterator begin()
77  { return _children.begin(); }
78 
79  /**
80  * Return an iterator that points after the last child.
81  **/
82  typename ChildrenList::iterator end()
83  { return _children.end(); }
84 
85  /**
86  * Return an iterator that points to the first child.
87  **/
88  typename ChildrenList::const_iterator begin() const
89  { return _children.begin(); }
90 
91  /**
92  * Return an iterator that points after the last child.
93  **/
94  typename ChildrenList::const_iterator end() const
95  { return _children.end(); }
96 
97  /**
98  * Return a reverse iterator that points to the last child.
99  **/
100  typename ChildrenList::const_reverse_iterator rbegin() const
101  { return _children.rbegin(); }
102 
103  /**
104  * Return a reverse iterator that points before the first child.
105  **/
106  typename ChildrenList::const_reverse_iterator rend() const
107  { return _children.rend(); }
108 
109  /**
110  * Returns the first child or 0 if there is none.
111  * Useful mostly for children managers that handle only one child.
112  **/
114  { return _children.empty() ? (T *) 0 : _children.front(); }
115 
116  /**
117  * Returns the last child or 0 if there is none.
118  **/
119  T * lastChild()
120  { return _children.empty() ? (T *) 0 : _children.back(); }
121 
122  /**
123  * Add a new child.
124  *
125  * This may throw exceptions if more children are added than the class
126  * whose children are handled (the associated widget) can handle.
127  **/
128  virtual void add( T * child )
129  { _children.push_back( child ); }
130 
131  /**
132  * Remove a child. This only removes the child from the children manager's
133  * list; it does not delete it.
134  **/
135  virtual void remove( T * child )
136  { _children.remove( child ); }
137 
138  /**
139  * Remove all children. This only removes the children from the children
140  * manager's list; it does not delete them.
141  **/
142  virtual void clear()
143  { _children.clear(); }
144 
145  /**
146  * Check if the children list contains the specified child.
147  * Returns 'true' if the children list contains the child,
148  * 'false' otherwise.
149  **/
150  bool contains( T * child ) const
151  {
152  return ( find( _children.begin(), _children.end(), child )
153  != _children.end() );
154  }
155 
156  /**
157  * Returns the associated container, i.e. the object whose children are
158  * handled here.
159  **/
160  T * container() const { return _container; }
161 
162 protected:
163 
164  T * _container;
165  ChildrenList _children;
166 };
167 
168 
169 /**
170  * Children manager that can handle one single child (rejecting any more).
171  * Useful for YAlignment, YFrame etc.
172  **/
173 template<class T> class YSingleChildManager: public YChildrenManager<T>
174 {
175 public:
176 
177  YSingleChildManager( T * containerParent )
178  : YChildrenManager<T>( containerParent )
179  {}
180 
181  /**
182  * Add a new child.
183  *
184  * Reimplemented from YChildrenManager.
185  *
186  * This will throw a YUITooManyChildrenException if there already is a
187  * child.
188  **/
189  virtual void add( T * child )
190  {
191  if ( this->empty() )
192  this->_children.push_back( child );
193  else
194  YUI_THROW( YUITooManyChildrenException<T>( this->container() ) );
195  }
196 
197  /**
198  * Replace the previous child (if any) with a new one.
199  **/
200  void replace( T * newChild )
201  {
202  this->_children.clear();
203  this->_children.push_back( newChild );
204  }
205 };
206 
207 
208 /**
209  * Children manager that rejects all children.
210  *
211  * Useful for widget classes that can't handle children such as YPushButton,
212  * YSelectionBox etc.
213  **/
214 template<class T> class YChildrenRejector: public YChildrenManager<T>
215 {
216 public:
217  /**
218  * Constructor.
219  **/
220  YChildrenRejector( T * containerParent )
221  : YChildrenManager<T>( containerParent )
222  {}
223 
224  /**
225  * Add a new child.
226  *
227  * Reimplemented from YChildrenManager.
228  *
229  * Since this class is designed to reject children, this always throws a
230  * YUITooManyChildrenException.
231  **/
232  virtual void add( T * child )
233  { YUI_THROW( YUITooManyChildrenException<T>( this->container() ) ); }
234 };
235 
236 
237 #endif // YChildrenManager_h
Exception class for "too many children": Attempt to add a child to a widget class that can&#39;t handle c...
Definition: YUIException.h:663
bool hasChildren() const
Check if there are any children.
ChildrenList::iterator begin()
Return an iterator that points to the first child.
YChildrenRejector(T *containerParent)
Constructor.
T * lastChild()
Returns the last child or 0 if there is none.
ChildrenList::const_reverse_iterator rbegin() const
Return a reverse iterator that points to the last child.
bool empty() const
Check if the children list is empty, i.e.
ChildrenList::const_iterator begin() const
Return an iterator that points to the first child.
virtual void add(T *child)
Add a new child.
virtual ~YChildrenManager()
Destructor.
T * container() const
Returns the associated container, i.e.
ChildrenList::const_iterator end() const
Return an iterator that points after the last child.
ChildrenList::const_reverse_iterator rend() const
Return a reverse iterator that points before the first child.
Abstract base template class for children management, such as child widgets.
YChildrenManager(T *containerParent)
Constructor.
virtual void add(T *child)
Add a new child.
virtual void add(T *child)
Add a new child.
bool contains(T *child) const
Check if the children list contains the specified child.
Children manager that can handle one single child (rejecting any more).
Children manager that rejects all children.
void replace(T *newChild)
Replace the previous child (if any) with a new one.
ChildrenList::iterator end()
Return an iterator that points after the last child.
T * firstChild()
Returns the first child or 0 if there is none.
virtual void clear()
Remove all children.
int count() const
Returns the number of children.