libyui

/data/gitorious.org/libyui/libyui-master/src/YChildrenManager.h

00001 /**************************************************************************
00002 Copyright (C) 2000 - 2010 Novell, Inc.
00003 All Rights Reserved.
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License along
00016 with this program; if not, write to the Free Software Foundation, Inc.,
00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018 
00019 **************************************************************************/
00020 
00021 
00022 /*---------------------------------------------------------------------\
00023 |                                                                      |
00024 |                      __   __    ____ _____ ____                      |
00025 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00026 |                       \ V / _` \___ \ | |   __) |                    |
00027 |                        | | (_| |___) || |  / __/                     |
00028 |                        |_|\__,_|____/ |_| |_____|                    |
00029 |                                                                      |
00030 |                               core system                            |
00031 |                                                        (C) SuSE GmbH |
00032 \----------------------------------------------------------------------/
00033 
00034   File:         YChildrenManager.h
00035 
00036   Author:       Stefan Hundhammer <sh@suse.de>
00037 
00038 /-*/
00039 
00040 #ifndef YChildrenManager_h
00041 #define YChildrenManager_h
00042 
00043 #include <list>
00044 #include <algorithm>
00045 #include <YUIException.h>
00046 
00047 
00052 template<class T> class YChildrenManager
00053 {
00054 public:
00055 
00061     YChildrenManager( T * containerParent )
00062         : _container( containerParent )
00063         {}
00064 
00068     virtual ~YChildrenManager() {}
00069 
00070     
00071     typedef std::list<T *> ChildrenList;
00072     
00076     bool hasChildren() const { return ! empty(); }
00077 
00081     bool empty() const { return _children.empty(); }
00082 
00086     int count() const { return _children.size(); }
00087 
00091     typename ChildrenList::const_iterator begin() const
00092         { return _children.begin(); }
00093 
00097     typename ChildrenList::const_iterator end() const
00098         { return _children.end(); }
00099 
00103     typename ChildrenList::const_reverse_iterator rbegin() const
00104         { return _children.rbegin(); }
00105     
00109     typename ChildrenList::const_reverse_iterator rend() const
00110         { return _children.rend(); }
00111 
00116     T * firstChild()
00117         { return _children.empty() ? (T *) 0 : _children.front(); }
00118 
00122     T * lastChild()
00123         { return _children.empty() ? (T *) 0 : _children.back(); }
00124 
00131     virtual void add( T * child )
00132         { _children.push_back( child ); }
00133 
00138     virtual void remove( T * child )
00139         { _children.remove( child ); }
00140 
00145     virtual void clear()
00146         { _children.clear(); }
00147 
00153     bool contains( T * child ) const
00154     {
00155         return ( find( _children.begin(), _children.end(), child )
00156                  != _children.end() );
00157     }
00158 
00163     T * container() const { return _container; }
00164 
00165 protected:
00166 
00167     T *                 _container;
00168     ChildrenList        _children;
00169 };
00170 
00171 
00176 template<class T> class YSingleChildManager: public YChildrenManager<T>
00177 {
00178 public:
00179     
00180     YSingleChildManager( T * containerParent )
00181         : YChildrenManager<T>( containerParent )
00182         {}
00183 
00192     virtual void add( T * child )
00193     {
00194         if ( this->empty() )
00195             this->_children.push_back( child );
00196         else
00197             YUI_THROW( YUITooManyChildrenException<T>( this->container() ) );
00198     }
00199 
00203     void replace( T * newChild )
00204     {
00205         this->_children.clear();
00206         this->_children.push_back( newChild );
00207     }
00208 };
00209 
00210 
00217 template<class T> class YChildrenRejector: public YChildrenManager<T>
00218 {
00219 public:
00223     YChildrenRejector( T * containerParent )
00224         : YChildrenManager<T>( containerParent )
00225         {}
00226 
00235     virtual void add( T * child )
00236         { YUI_THROW( YUITooManyChildrenException<T>( this->container() ) ); }
00237 };
00238 
00239 
00240 #endif // YChildrenManager_h
 All Classes Functions Variables Enumerations Friends