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