libyui  3.0.10
 All Classes Functions Variables Enumerations Friends
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::const_iterator begin() const
77  { return _children.begin(); }
78 
79  /**
80  * Return an iterator that points after the last child.
81  **/
82  typename ChildrenList::const_iterator end() const
83  { return _children.end(); }
84 
85  /**
86  * Return a reverse iterator that points to the last child.
87  **/
88  typename ChildrenList::const_reverse_iterator rbegin() const
89  { return _children.rbegin(); }
90 
91  /**
92  * Return a reverse iterator that points before the first child.
93  **/
94  typename ChildrenList::const_reverse_iterator rend() const
95  { return _children.rend(); }
96 
97  /**
98  * Returns the first child or 0 if there is none.
99  * Useful mostly for children managers that handle only one child.
100  **/
102  { return _children.empty() ? (T *) 0 : _children.front(); }
103 
104  /**
105  * Returns the last child or 0 if there is none.
106  **/
107  T * lastChild()
108  { return _children.empty() ? (T *) 0 : _children.back(); }
109 
110  /**
111  * Add a new child.
112  *
113  * This may throw exceptions if more children are added than the class
114  * whose children are handled (the associated widget) can handle.
115  **/
116  virtual void add( T * child )
117  { _children.push_back( child ); }
118 
119  /**
120  * Remove a child. This only removes the child from the children manager's
121  * list; it does not delete it.
122  **/
123  virtual void remove( T * child )
124  { _children.remove( child ); }
125 
126  /**
127  * Remove all children. This only removes the children from the children
128  * manager's list; it does not delete them.
129  **/
130  virtual void clear()
131  { _children.clear(); }
132 
133  /**
134  * Check if the children list contains the specified child.
135  * Returns 'true' if the children list contains the child,
136  * 'false' otherwise.
137  **/
138  bool contains( T * child ) const
139  {
140  return ( find( _children.begin(), _children.end(), child )
141  != _children.end() );
142  }
143 
144  /**
145  * Returns the associated container, i.e. the object whose children are
146  * handled here.
147  **/
148  T * container() const { return _container; }
149 
150 protected:
151 
152  T * _container;
153  ChildrenList _children;
154 };
155 
156 
157 /**
158  * Children manager that can handle one single child (rejecting any more).
159  * Useful for YAlignment, YFrame etc.
160  **/
161 template<class T> class YSingleChildManager: public YChildrenManager<T>
162 {
163 public:
164 
165  YSingleChildManager( T * containerParent )
166  : YChildrenManager<T>( containerParent )
167  {}
168 
169  /**
170  * Add a new child.
171  *
172  * Reimplemented from YChildrenManager.
173  *
174  * This will throw a YUITooManyChildrenException if there already is a
175  * child.
176  **/
177  virtual void add( T * child )
178  {
179  if ( this->empty() )
180  this->_children.push_back( child );
181  else
182  YUI_THROW( YUITooManyChildrenException<T>( this->container() ) );
183  }
184 
185  /**
186  * Replace the previous child (if any) with a new one.
187  **/
188  void replace( T * newChild )
189  {
190  this->_children.clear();
191  this->_children.push_back( newChild );
192  }
193 };
194 
195 
196 /**
197  * Children manager that rejects all children.
198  *
199  * Useful for widget classes that can't handle children such as YPushButton,
200  * YSelectionBox etc.
201  **/
202 template<class T> class YChildrenRejector: public YChildrenManager<T>
203 {
204 public:
205  /**
206  * Constructor.
207  **/
208  YChildrenRejector( T * containerParent )
209  : YChildrenManager<T>( containerParent )
210  {}
211 
212  /**
213  * Add a new child.
214  *
215  * Reimplemented from YChildrenManager.
216  *
217  * Since this class is designed to reject children, this always throws a
218  * YUITooManyChildrenException.
219  **/
220  virtual void add( T * child )
221  { YUI_THROW( YUITooManyChildrenException<T>( this->container() ) ); }
222 };
223 
224 
225 #endif // YChildrenManager_h