ucommon
counter.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
26 #ifndef _UCOMMON_COUNTER_H_
27 #define _UCOMMON_COUNTER_H_
28 
29 #ifndef _UCOMMON_CONFIG_H_
30 #include <ucommon/platform.h>
31 #endif
32 
33 NAMESPACE_UCOMMON
34 
42 class __EXPORT counter
43 {
44 private:
45  unsigned value, cycle;
46 
47 public:
51  counter();
52 
57  counter(unsigned limit);
58 
63  unsigned get(void);
64 
69  inline unsigned range(void)
70  {return cycle;};
71 
76  inline unsigned operator*()
77  {return get();};
78 
83  inline operator unsigned()
84  {return get();};
85 
90  void operator=(unsigned value);
91 };
92 
100 class __EXPORT SeqCounter : protected counter
101 {
102 private:
103  void *item;
104  size_t offset;
105 
106 protected:
107  SeqCounter(void *start, size_t size, unsigned count);
108 
109  void *get(void);
110 
111  void *get(unsigned idx);
112 
113 public:
118  inline void operator=(unsigned inc_offset)
119  {counter::operator=(inc_offset);};
120 };
121 
126 class __EXPORT toggle
127 {
128 private:
129  bool value;
130 
131 public:
132  inline toggle()
133  {value = false;};
134 
135  bool get(void);
136 
137  inline bool operator*()
138  {return get();};
139 
140  inline void operator=(bool v)
141  {value = v;};
142 
143  inline operator bool()
144  {return get();};
145 
146 };
147 
154 template <class T>
155 class sequence : public SeqCounter
156 {
157 protected:
158  inline T *get(unsigned idx)
159  {return static_cast<T *>(SeqCounter::get(idx));};
160 
161 public:
167  inline sequence(T *array, unsigned size) :
168  SeqCounter(array, sizeof(T), size) {};
169 
174  inline T* get(void)
175  {return static_cast<T *>(SeqCounter::get());};
176 
181  inline T& operator*()
182  {return *get();};
183 
188  inline operator T&()
189  {return *get();};
190 
196  inline T& operator[](unsigned offset)
197  {return *get(offset);};
198 };
199 
204 
208 typedef toggle toggle_t;
209 
210 END_NAMESPACE
211 
212 #endif
sequence(T *array, unsigned size)
Create a template auto-sequence from a list of typed pointers.
Definition: counter.h:167
Automatic integer counting class.
Definition: counter.h:42
toggle toggle_t
A convenience typecast for auto-toggled bools.
Definition: counter.h:208
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
Definition: thread.h:1814
T & operator[](unsigned offset)
Return a specific typed member from the sequence list.
Definition: counter.h:196
Various miscellaneous platform specific headers and defines.
void operator=(unsigned value)
Assign the value of the counter.
unsigned operator*()
Reference next counter value through pointer operation.
Definition: counter.h:76
A template to return a sequence of objects of a specified type.
Definition: counter.h:155
void operator=(unsigned inc_offset)
Used to directly assign sequence position in template.
Definition: counter.h:118
unsigned range(void)
Get the range of values before recycling.
Definition: counter.h:69
counter counter_t
A convenience typecast for integer counters.
Definition: counter.h:203
Automatically toggle a bool on each reference.
Definition: counter.h:126
Automatically return a sequence of untyped objects.
Definition: counter.h:100
T & operator*()
Return next typed member of the sequence by pointer reference.
Definition: counter.h:181
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
Definition: generics.h:568