libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YChildrenManager.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YChildrenManager.h
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef YChildrenManager_h
00026 #define YChildrenManager_h
00027 
00028 #include <list>
00029 #include <algorithm>
00030 #include "YUIException.h"
00031 
00032 
00033 /**
00034  * Abstract base template class for children management, such as child
00035  * widgets.
00036  **/
00037 template<class T> class YChildrenManager
00038 {
00039 public:
00040 
00041     /**
00042      * Constructor.
00043      *
00044      * 'containerParent' is the class whose children are managed.
00045      **/
00046     YChildrenManager( T * containerParent )
00047         : _container( containerParent )
00048         {}
00049 
00050     /**
00051      * Destructor.
00052      **/
00053     virtual ~YChildrenManager() {}
00054 
00055 
00056     typedef std::list<T *> ChildrenList;
00057 
00058     /**
00059      * Check if there are any children.
00060      **/
00061     bool hasChildren() const { return ! empty(); }
00062 
00063     /**
00064      * Check if the children list is empty, i.e. if there are no children.
00065      **/
00066     bool empty() const { return _children.empty(); }
00067 
00068     /**
00069      * Returns the number of children.
00070      **/
00071     int count() const { return _children.size(); }
00072 
00073     /**
00074      * Return an iterator that points to the first child.
00075      **/
00076     typename ChildrenList::const_iterator begin() const
00077         { return _children.begin(); }
00078 
00079     /**
00080      * Return an iterator that points after the last child.
00081      **/
00082     typename ChildrenList::const_iterator end() const
00083         { return _children.end(); }
00084 
00085     /**
00086      * Return a reverse iterator that points to the last child.
00087      **/
00088     typename ChildrenList::const_reverse_iterator rbegin() const
00089         { return _children.rbegin(); }
00090 
00091     /**
00092      * Return a reverse iterator that points before the first child.
00093      **/
00094     typename ChildrenList::const_reverse_iterator rend() const
00095         { return _children.rend(); }
00096 
00097     /**
00098      * Returns the first child or 0 if there is none.
00099      * Useful mostly for children managers that handle only one child.
00100      **/
00101     T * firstChild()
00102         { return _children.empty() ? (T *) 0 : _children.front(); }
00103 
00104     /**
00105      * Returns the last child or 0 if there is none.
00106      **/
00107     T * lastChild()
00108         { return _children.empty() ? (T *) 0 : _children.back(); }
00109 
00110     /**
00111      * Add a new child.
00112      *
00113      * This may throw exceptions if more children are added than the class
00114      * whose children are handled (the associated widget) can handle.
00115      **/
00116     virtual void add( T * child )
00117         { _children.push_back( child ); }
00118 
00119     /**
00120      * Remove a child. This only removes the child from the children manager's
00121      * list; it does not delete it.
00122      **/
00123     virtual void remove( T * child )
00124         { _children.remove( child ); }
00125 
00126     /**
00127      * Remove all children. This only removes the children from the children
00128      * manager's list; it does not delete them.
00129      **/
00130     virtual void clear()
00131         { _children.clear(); }
00132 
00133     /**
00134      * Check if the children list contains the specified child.
00135      * Returns 'true' if the children list contains the child,
00136      * 'false' otherwise.
00137      **/
00138     bool contains( T * child ) const
00139     {
00140         return ( find( _children.begin(), _children.end(), child )
00141                  != _children.end() );
00142     }
00143 
00144     /**
00145      * Returns the associated container, i.e. the object whose children are
00146      * handled here.
00147      **/
00148     T * container() const { return _container; }
00149 
00150 protected:
00151 
00152     T *                 _container;
00153     ChildrenList        _children;
00154 };
00155 
00156 
00157 /**
00158  * Children manager that can handle one single child (rejecting any more).
00159  * Useful for YAlignment, YFrame etc.
00160  **/
00161 template<class T> class YSingleChildManager: public YChildrenManager<T>
00162 {
00163 public:
00164 
00165     YSingleChildManager( T * containerParent )
00166         : YChildrenManager<T>( containerParent )
00167         {}
00168 
00169     /**
00170      * Add a new child.
00171      *
00172      * Reimplemented from YChildrenManager.
00173      *
00174      * This will throw a YUITooManyChildrenException if there already is a
00175      * child.
00176      **/
00177     virtual void add( T * child )
00178     {
00179         if ( this->empty() )
00180             this->_children.push_back( child );
00181         else
00182             YUI_THROW( YUITooManyChildrenException<T>( this->container() ) );
00183     }
00184 
00185     /**
00186      * Replace the previous child (if any) with a new one.
00187      **/
00188     void replace( T * newChild )
00189     {
00190         this->_children.clear();
00191         this->_children.push_back( newChild );
00192     }
00193 };
00194 
00195 
00196 /**
00197  * Children manager that rejects all children.
00198  *
00199  * Useful for widget classes that can't handle children such as YPushButton,
00200  * YSelectionBox etc.
00201  **/
00202 template<class T> class YChildrenRejector: public YChildrenManager<T>
00203 {
00204 public:
00205     /**
00206      * Constructor.
00207      **/
00208     YChildrenRejector( T * containerParent )
00209         : YChildrenManager<T>( containerParent )
00210         {}
00211 
00212     /**
00213      * Add a new child.
00214      *
00215      * Reimplemented from YChildrenManager.
00216      *
00217      * Since this class is designed to reject children, this always throws a
00218      * YUITooManyChildrenException.
00219      **/
00220     virtual void add( T * child )
00221         { YUI_THROW( YUITooManyChildrenException<T>( this->container() ) ); }
00222 };
00223 
00224 
00225 #endif // YChildrenManager_h
 All Classes Functions Variables Enumerations Friends