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