UCommon
|
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00017 00026 #ifndef _UCOMMON_COUNTER_H_ 00027 #define _UCOMMON_COUNTER_H_ 00028 00029 #ifndef _UCOMMON_CONFIG_H_ 00030 #include <ucommon/platform.h> 00031 #endif 00032 00033 NAMESPACE_UCOMMON 00034 00042 class __EXPORT counter 00043 { 00044 private: 00045 unsigned value, cycle; 00046 00047 public: 00051 counter(); 00052 00057 counter(unsigned limit); 00058 00063 unsigned get(void); 00064 00069 inline unsigned range(void) 00070 {return cycle;}; 00071 00076 inline unsigned operator*() 00077 {return get();}; 00078 00083 inline operator unsigned() 00084 {return get();}; 00085 00090 void operator=(unsigned value); 00091 }; 00092 00100 class __EXPORT SeqCounter : protected counter 00101 { 00102 private: 00103 void *item; 00104 size_t offset; 00105 00106 protected: 00107 SeqCounter(void *start, size_t size, unsigned count); 00108 00109 void *get(void); 00110 00111 void *get(unsigned idx); 00112 00113 public: 00118 inline void operator=(unsigned inc_offset) 00119 {counter::operator=(inc_offset);}; 00120 }; 00121 00126 class __EXPORT toggle 00127 { 00128 private: 00129 bool value; 00130 00131 public: 00132 inline toggle() 00133 {value = false;}; 00134 00135 bool get(void); 00136 00137 inline bool operator*() 00138 {return get();}; 00139 00140 inline void operator=(bool v) 00141 {value = v;}; 00142 00143 inline operator bool() 00144 {return get();}; 00145 00146 }; 00147 00154 template <class T> 00155 class sequence : public SeqCounter 00156 { 00157 protected: 00158 inline T *get(unsigned idx) 00159 {return static_cast<T *>(SeqCounter::get(idx));}; 00160 00161 public: 00167 inline sequence(T *array, unsigned size) : 00168 SeqCounter(array, sizeof(T), size) {}; 00169 00174 inline T* get(void) 00175 {return static_cast<T *>(SeqCounter::get());}; 00176 00181 inline T& operator*() 00182 {return *get();}; 00183 00188 inline operator T&() 00189 {return *get();}; 00190 00196 inline T& operator[](unsigned offset) 00197 {return *get(offset);}; 00198 }; 00199 00203 typedef counter counter_t; 00204 00208 typedef toggle toggle_t; 00209 00210 END_NAMESPACE 00211 00212 #endif