File: INCLUDE\DETECUSE\Range.h

    1 /*
    2 $Header: /vol/baal-vol3/projekt98/quellen/XCTL_32/INCLUDE/DETECUSE/Range.h,v 1.2 2004/04/15 21:41:53 reinecke Exp $
    3 
    4 Projekt   : XCTL
    5 Subsystem : alle
    6 Autor     : Jan Picard <picard@informatik.hu-berlin.de> 2001-2002
    7           Institut fuer Informatik,
    8           Humboldt-Universitaet Berlin
    9 Inhalt    : Eine Klasse, die ein Intervall mit Ober- und Untergrenzen
   10           realisiert
   11 */
   12 
   13 #ifndef __RANGE_H
   14 #define __RANGE_H
   15 
   16 #include <windows.h>
   17 
   18 //--||--\\--||--//--||--\\--||--//--||--\\--||--//--||--\\--||--//--||--\\--||--
   19 
   20 //! Implementation of Martin Fowlers "Range Pattern"
   21 template <class TRangeType>
   22 class TRange
   23 {
   24         //! The range limits belong the range! So its possible
   25         //! to force a value into a given range by setting it to
   26         //! to the lower or upper limit.
   27         //! The TRangeType class must provide operator<
   28 
   29 public:
   30         TRange(TRangeType min, TRangeType max)
   31         {
   32                 if ( min < max)
   33                 {
   34                         _min= min;
   35                         _max= max;
   36                 }
   37 
   38                 else
   39                 {
   40                         _min= max;
   41                         _max= min;
   42                 }
   43         }
   44 
   45         TRangeType ForceIntoRange(TRangeType value) const
   46         {
   47                 if ( value < _min)
   48                         return _min;
   49                 else
   50                         if ( _max < value)
   51                                 return _max;
   52                         else
   53                                 return value;
   54         }
   55 
   56         void SetMin(TRangeType value)
   57         {
   58                 if (_max < value)
   59                         _min= _max;
   60                 else
   61                         _min= value;
   62         }
   63 
   64         void SetMax(TRangeType value)
   65         {
   66                 if (value < _min)
   67                         _max= _min;
   68                 else
   69                         _max= value;
   70         }
   71 
   72         TRangeType GetMin() const
   73         {
   74                 return _min;
   75         }
   76 
   77         TRangeType GetMax() const
   78         {
   79                 return _max;
   80         }
   81 
   82         BOOL includes(TRangeType value) const
   83         {
   84                 if ( (value < _min) || (_max < value) )
   85                         return FALSE;
   86                 else
   87                         return TRUE;
   88         }
   89 
   90 private:
   91         TRangeType _min;
   92         TRangeType _max;
   93 };
   94 
   95 #endif // __RANGE_H
   96 
   97 
   98