base_types2.hh

Go to the documentation of this file.
00001 /*
00019  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00020  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00021  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00022  *****************************************************************************/
00023 
00024 #ifndef BASE_TYPES2_HH
00025 #define BASE_TYPES2_HH 1
00026 
00027 #include "base_shared_ptr.hh"
00028 
00029 namespace base {
00030 
00031 //==============================================================================
00032 //==============================================================================
00033 //                     Virtual methods allowed in this section
00034 //==============================================================================
00035 //==============================================================================
00036 
00048 template<typename T>
00049 class AbstractIterator
00050 {
00051 public:
00052                     AbstractIterator( void ) { }
00053     virtual         ~AbstractIterator() { }
00054 
00055     virtual         operator bool() = 0;
00056     virtual T       Next( void ) = 0;
00057 };
00058 
00059 //==============================================================================
00060 //==============================================================================
00061 //                    Virtual methods forbidden in this section
00062 //==============================================================================
00063 //==============================================================================
00064 
00065 // Basic types must be fast, prevent them having virtual functions.
00066 #define virtual VIRTUAL_METHODS_IN_BASIC_TYPES_IS_TOO_SLOW
00067 
00073 template<typename T,typename CONTAINER>
00074 class STLIterator : public base::AbstractIterator<T>
00075 {
00076 public:
00077     explicit    STLIterator( CONTAINER& cont ) : mCont(cont), mIter(cont.begin()) { }
00078                 operator bool() { return mIter != mCont.end(); }
00079     T           Next( void ) { return *mIter++; }
00080 private:
00081     CONTAINER&                      mCont;
00082     typename CONTAINER::iterator    mIter;
00083 };
00084 
00089 template<typename T=int>
00090 class Multivar
00091 {
00092 public:
00096     Multivar( void ) { }
00097     ~Multivar() { }
00098 
00102     void
00103     Clear( void )
00104     {
00105         mValues.clear();
00106     }
00107 
00111     void
00112     Remove( T value )
00113     {
00114         mValues.remove( value );
00115     }
00116 
00120     void
00121     Set( T value )
00122     {
00123         mValues.clear();
00124         mValues.push_back( value );
00125     }
00126 
00130     void
00131     Add( T value )
00132     {
00133         // Add value unless present.
00134         if ( ! Query(value) )
00135             mValues.push_back( value );
00136     }
00137 
00141     void
00142     Toggle( T value )
00143     {
00144         if ( Query(value) )
00145             Remove( value );
00146         else
00147             Add( value );
00148     }
00149 
00153     bool
00154     Query( T value ) const
00155     {
00156         return std::find( mValues.begin(), mValues.end(), value ) != mValues.end();
00157     }
00158 
00162     bool
00163     IfEmpty( void ) const
00164     {
00165         return mValues.empty();
00166     }
00167 
00168 private:
00169     std::list<T> mValues;  // multiple values of same type
00170 };
00171 
00179 class Percent
00180 {
00181 public:
00182             Percent( void ) : mPercent(0) { }
00183             Percent( uint percent ) { Set( percent ); }  // not explicit
00184 
00185     // Assigning an out-of-range percent is an error.
00186     void    operator=( uint percent ) { Set( percent ); }
00187             operator uint() const { ASSERT( mPercent <= 100 ); return mPercent; }
00188 
00189     // These clamp.
00190     void    operator+=( int n ) { SetClamp( int(mPercent) + int(n) ); }
00191     void    operator-=( int n ) { SetClamp( int(mPercent) - int(n) ); }
00192 
00193 private:
00194     // Set() is strict: it will fail if percent is out-of-range.
00195     void    Set( uint percent )
00196             {
00197                 ASSERT( percent <= 100 );
00198                 mPercent = percent;
00199             }
00200 
00201     // SetClamp() is lenient: it will clamp out-of-range values.
00202     void    SetClamp( int percent )
00203             {
00204                 if ( percent < 0 )
00205                     mPercent = 0;
00206                 else if ( percent > 100 )
00207                     mPercent = 100;
00208                 else
00209                     mPercent = uint(percent);
00210             }
00211 private:
00212     uint    mPercent;
00213 };
00214 
00221 class StringBuf
00222 {
00223 public:
00224                     StringBuf( void ) { }
00225                     ~StringBuf() { }
00226     void            Set( const char* buf )           { mBuf.assign( buf ); }
00227     void            Set( const char* buf, int len )  { mBuf.assign( buf, len ); }
00228     void            Set( const uchar* buf, int len ) { mBuf.assign( (char*)buf, len ); }
00229     void            Erase( void )               { mBuf.erase(); }
00230     bool            IfEmpty( void ) const       { return mBuf.size() == 0; }
00231     int             Size( void ) const          { return mBuf.size(); }
00232     const string*   GetString( void ) const     { return &mBuf; }
00233     const char*     GetChars( void ) const      { return mBuf.c_str(); }
00234     const uchar*    GetUchars( void ) const     { return (uchar*)mBuf.c_str(); }
00235 
00236 public:
00237     string      mBuf;   
00238 };
00239 
00240 #undef virtual
00241 
00242 } // namespace base
00243 
00244 #endif // BASE_TYPES2_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:07 2007