libyui
 
Loading...
Searching...
No Matches
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 <shundhammer@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
37template<class T> class YChildrenManager
38{
39public:
40
46 YChildrenManager( T * containerParent )
47 : _container( containerParent )
48 {}
49
53 virtual ~YChildrenManager() {}
54
55
56 typedef std::list<T *> ChildrenList;
57
61 bool hasChildren() const { return ! empty(); }
62
66 bool empty() const { return _children.empty(); }
67
71 int count() const { return _children.size(); }
72
76 typename ChildrenList::iterator begin()
77 { return _children.begin(); }
78
82 typename ChildrenList::iterator end()
83 { return _children.end(); }
84
88 typename ChildrenList::const_iterator begin() const
89 { return _children.begin(); }
90
94 typename ChildrenList::const_iterator end() const
95 { return _children.end(); }
96
100 typename ChildrenList::const_reverse_iterator rbegin() const
101 { return _children.rbegin(); }
102
106 typename ChildrenList::const_reverse_iterator rend() const
107 { return _children.rend(); }
108
114 { return _children.empty() ? (T *) 0 : _children.front(); }
115
120 { return _children.empty() ? (T *) 0 : _children.back(); }
121
128 virtual void add( T * child )
129 { _children.push_back( child ); }
130
135 virtual void remove( T * child )
136 { _children.remove( child ); }
137
142 virtual void clear()
143 { _children.clear(); }
144
150 bool contains( T * child ) const
151 {
152 return ( find( _children.begin(), _children.end(), child )
153 != _children.end() );
154 }
155
160 T * container() const { return _container; }
161
162protected:
163
164 T * _container;
165 ChildrenList _children;
166};
167
168
173template<class T> class YSingleChildManager: public YChildrenManager<T>
174{
175public:
176
177 YSingleChildManager( T * containerParent )
178 : YChildrenManager<T>( containerParent )
179 {}
180
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
200 void replace( T * newChild )
201 {
202 this->_children.clear();
203 this->_children.push_back( newChild );
204 }
205};
206
207
214template<class T> class YChildrenRejector: public YChildrenManager<T>
215{
216public:
220 YChildrenRejector( T * containerParent )
221 : YChildrenManager<T>( containerParent )
222 {}
223
232 virtual void add( T * child )
233 { YUI_THROW( YUITooManyChildrenException<T>( this->container() ) ); }
234};
235
236
237#endif // YChildrenManager_h
virtual void clear()
Definition YChildrenManager.h:142
T * lastChild()
Definition YChildrenManager.h:119
ChildrenList::const_reverse_iterator rend() const
Definition YChildrenManager.h:106
virtual void add(T *child)
Definition YChildrenManager.h:128
bool hasChildren() const
Definition YChildrenManager.h:61
T * container() const
Definition YChildrenManager.h:160
ChildrenList::const_iterator end() const
Definition YChildrenManager.h:94
ChildrenList::iterator end()
Definition YChildrenManager.h:82
ChildrenList::const_iterator begin() const
Definition YChildrenManager.h:88
int count() const
Definition YChildrenManager.h:71
ChildrenList::const_reverse_iterator rbegin() const
Definition YChildrenManager.h:100
virtual void remove(T *child)
Definition YChildrenManager.h:135
T * firstChild()
Definition YChildrenManager.h:113
bool empty() const
Definition YChildrenManager.h:66
bool contains(T *child) const
Definition YChildrenManager.h:150
ChildrenList::iterator begin()
Definition YChildrenManager.h:76
virtual ~YChildrenManager()
Definition YChildrenManager.h:53
YChildrenManager(T *containerParent)
Definition YChildrenManager.h:46
YChildrenRejector(T *containerParent)
Definition YChildrenManager.h:220
virtual void add(T *child)
Definition YChildrenManager.h:232
virtual void add(T *child)
Definition YChildrenManager.h:189
void replace(T *newChild)
Definition YChildrenManager.h:200
Definition YUIException.h:664