TAPs 0.7.7.3
TAPsCircularCounter.hpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsCircularCounter.hpp
00003 
00004 GraphNode class is a base class for a graph node.
00005 
00006 SUKITTI PUNAK   (04/17/2008)
00007 UPDATE          (04/17/2008)
00008 ******************************************************************************/
00009 #ifndef TAPs_CIRCULAR_COUNTER_HPP
00010 #define TAPs_CIRCULAR_COUNTER_HPP
00011 
00018 #include "../Core/TAPsLib.hpp"
00019 
00020 BEGIN_NAMESPACE_TAPs
00021 //=============================================================================
00022 
00023 template <typename T>
00024 class CircularCounter {
00025 //=============================================================================
00026 // Member Functions
00027 public:
00028     //-------------------------------------------------------------------------
00029     // Output Operator <<
00030     friend std::ostream & operator<< ( std::ostream &output, CircularCounter<T> const &ctr )
00031     {
00032         output  << "CircularCounter<" << typeid(T).name() << ">"
00033                 <<  " Highest Value (" << ctr.m_tHighest
00034                 << ") Current Value (" << ctr.m_tCurrent
00035                 << ") Lowest Value ("  << ctr.m_tLowest;
00036                 << ")\n";
00037         return output;
00038     }
00039     //-------------------------------------------------------------------------
00040     // Constructor and Destructor
00041 public:
00042 
00044     CircularCounter () : m_tCurrent(0)
00045     {
00046         if ( typeid(T) == typeid(unsigned char) ) {
00047             m_tLowest   = 0;
00048             m_tHighest  = static_cast<unsigned char>( UCHAR_MAX );
00049         }
00050         else if ( typeid(T) == typeid(char) ) {
00051             m_tLowest   = static_cast<char>( CHAR_MIN );
00052             m_tHighest  = static_cast<char>( CHAR_MAX );
00053         }
00054         else if ( typeid(T) == typeid(unsigned short) ) {
00055             m_tLowest   = 0;
00056             m_tHighest  = static_cast<unsigned short>( USHRT_MAX );
00057         }
00058         else if ( typeid(T) == typeid(short) ) {
00059             m_tLowest   = static_cast<short>( SHRT_MIN );
00060             m_tHighest  = static_cast<short>( SHRT_MAX );
00061         }
00062         else if ( typeid(T) == typeid(unsigned int) ) {
00063             m_tLowest   = 0;
00064             m_tHighest  = static_cast<unsigned int>( UINT_MAX );
00065         }
00066         else if ( typeid(T) == typeid(int) ) {
00067             m_tLowest   = static_cast<int>( INT_MIN );
00068             m_tHighest  = static_cast<int>( INT_MAX );
00069         }
00070         else if ( typeid(T) == typeid(unsigned long) ) {
00071             m_tLowest   = 0;
00072             m_tHighest  = static_cast<unsigned long>( ULONG_MAX );
00073         }
00074         else if ( typeid(T) == typeid(long) ) {
00075             m_tLowest   = static_cast<long>( LONG_MIN );
00076             m_tHighest  = static_cast<long>( LONG_MAX );
00077         }
00078     }
00079 
00083     CircularCounter ( T v1, T v2, T v3 )
00084     {
00085         if ( v1 >= v2 ) {
00086             if ( v1 >= v3 ) {
00087                 m_tHighest = v1;
00088                 if ( v2 >= v3 ) {
00089                     m_tCurrent = v2;
00090                     m_tLowest  = v3;
00091                 }
00092                 else {
00093                     m_tCurrent = v3;
00094                     m_tLowest  = v2;
00095                 }
00096             }
00097             else {
00098                 m_tHighest = v3;
00099                 m_tCurrent = v1;
00100                 m_tLowest  = v2;
00101             }
00102         }
00103         else {
00104             if ( v2 >= v3 ) {
00105                 m_tHighest = v2;
00106                 if ( v1 >= v3 ) {
00107                     m_tCurrent = v1;
00108                     m_tLowest  = v3;
00109                 }
00110                 else {
00111                     m_tCurrent = v3;
00112                     m_tLowe st = v1;
00113                 }
00114             }
00115             else {
00116                 m_tHighest = v3;
00117                 m_tCurrent = v2;
00118                 m_tLowe st = v1;
00119             }
00120         }
00121     }
00122 
00124     virtual ~CircularCounter () {};
00125 
00126     //-------------------------------------------------------------------------
00127     // Get/Set Fn(s)
00128     inline T    GetValueLowest  () const { return m_tLowest; }
00129     inline T    GetValueCurrent () const { return m_tCurrent; }
00130     inline T    GetValueHighest () const { return m_tHighest; }
00131     inline void SetValueLowest  ( T v ) 
00132     {
00133         if ( m_tLowest < m_tHighest )   m_tLowest  = v;
00134         if ( m_tLowest > m_tCurrent )   m_tCurrent = v;
00135     }
00136     inline void SetValueCurrent ( T v )
00137     {
00138         if      ( v > m_tHighest )  m_tCurrent = m_tHighest;
00139         else if ( v < m_tLowest )   m_tCurrent = m_tLowest;
00140         else                        m_tCurrent = v;
00141     }
00142     inline void SetValueHighest ( T v )
00143     {
00144         if ( m_tHighest > m_tLowest )   m_tHighest = v;
00145         if ( m_tHighest < m_tCurrent )  m_tCurrent = v;
00146     }
00147 
00148     //-------------------------------------------------------------------------
00149     // Operations
00150 
00151     inline void CountUp ()
00152     {
00153         ++m_tCurrent;
00154         CheckCountUp();
00155     }
00156     inline void CountUp ( T v )
00157     {
00158         m_tCurrent += v;
00159         CheckCountUp();
00160     }
00161     inline void CountDown ()
00162     {
00163         --m_tCurrent;
00164         CheckCountDown();
00165     }
00166     inline void CountDown ( T v )
00167     {
00168         m_tCurrent -= v;
00169         CheckCountDown();
00170     }
00171 
00172     inline T CountUpAndReturnTheCountValue ()
00173     {
00174         ++m_tCurrent;
00175         CheckCountUp();
00176         return m_tCurrent;
00177     }
00178     inline T CountUpAndReturnTheCountValue ( T v )
00179     {
00180         m_tCurrent += v;
00181         CheckCountUp();
00182         return m_tCurrent;
00183     }
00184     inline T CountDownAndReturnTheCountValue ()
00185     {
00186         --m_tCurrent;
00187         CheckCountDown();
00188         return m_tCurrent;
00189     }
00190     inline T CountDownAndReturnTheCountValue ( T v )
00191     {
00192         m_tCurrent -= v;
00193         CheckCountDown();
00194         return m_tCurrent;
00195     }
00196 
00197 //============================================================================
00198 protected:
00199 // Helper Function(s)
00200     inline void CheckCountUp ()
00201     { if ( m_tCurrent > m_tHighest )    m_tCurrent = m_tLowest; }
00202     inline void CheckCountDown ()
00203     { if ( m_tCurrent < m_tLowest )     m_tCurrent = m_tHighest; }
00204 
00205 // Data Members
00206     T   m_tLowest;
00207     T   m_tCurrent;
00208     T   m_tHighest;
00209 //============================================================================
00210 #if defined(__gl_h_) || defined(__GL_H__)
00211 public:
00212     // DrawByOpenGL UNFINISHED!!!
00213     virtual void DrawByOpenGL () {};
00214 #endif
00215 }; // END CLASS CircularCounter
00216 //=============================================================================
00217 //-----------------------------------------------------------------------------
00218 // Define CircularCounters
00219 typedef CircularCounter<unsigned char>  CircularCounteruc;
00220 typedef CircularCounter<char>           CircularCounterc;
00221 typedef CircularCounter<unsigned short> CircularCounterus;
00222 typedef CircularCounter<short>          CircularCounters;
00223 typedef CircularCounter<unsigned int>   CircularCounterui;
00224 typedef CircularCounter<int>            CircularCounteri;
00225 typedef CircularCounter<unsigned long>  CircularCounterul;
00226 typedef CircularCounter<long>           CircularCounterl;
00227 //=============================================================================
00228 END_NAMESPACE_TAPs
00229 //-----------------------------------------------------------------------------
00230 // Include definition if TAPs_USE_EXPORT is not defined
00231 //#if !defined( TAPs_USE_EXPORT )
00232     //#include "TAPsCircularCounter.cpp"
00233 //#endif
00234 //-----------------------------------------------------------------------------
00235 #endif
00236 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00237 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines