libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YBothDim.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:         YBothDim.h
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef YBothDim_h
00026 #define YBothDim_h
00027 
00028 #include "YTypes.h"
00029 #include "YUIException.h"
00030 
00031 
00032 /**
00033  * Template class for two-dimensional entities, such as
00034  * - width, height
00035  * - x_pos, y_pos
00036  * - hStretchable, vStretchable
00037  *
00038  * Precondition: type T needs to have a default constructor
00039  * (which all simple types like int, long, bool have).
00040  **/
00041 template<typename T> class YBothDim
00042 {
00043 public:
00044 
00045     // Data Members - intentionally public
00046     T vert;
00047     T hor;
00048 
00049     /**
00050      * Constructor with explicit initialization for both values
00051      **/
00052     YBothDim( T hor, T vert )
00053         : vert( vert )
00054         , hor( hor )
00055         {}
00056 
00057     /**
00058      * Default constructor (calls T default constructor for both values)
00059      **/
00060     YBothDim()
00061         {}
00062 
00063     /**
00064      * operator[] for alternative access via  myVar[ YD_HORIZ ]
00065      * Please note that this returns a non-const reference, so this can be used
00066      * as an lvalue (e.g., in assignments)
00067      **/
00068     T & operator[]( YUIDimension dim )
00069     {
00070         switch ( dim )
00071         {
00072             case YD_HORIZ:      return hor;
00073             case YD_VERT:       return vert;
00074             default:            YUI_THROW( YUIInvalidDimensionException() );
00075         }
00076 
00077         // never reached (but gcc will complain otherwise)
00078         return hor;
00079     }
00080 
00081     /**
00082      * Same as above for const objects
00083      **/
00084     const T & operator[]( YUIDimension dim ) const
00085     {
00086         switch ( dim )
00087         {
00088             case YD_HORIZ:      return hor;
00089             case YD_VERT:       return vert;
00090             default:            YUI_THROW( YUIInvalidDimensionException() );
00091         }
00092 
00093         // never reached (but gcc will complain otherwise)
00094         return hor;
00095     }
00096 };
00097 
00098 
00099 #endif // YBothDim_h
 All Classes Functions Variables Enumerations Friends