blob: 25c9751206f16eb8de40943a30edb7d49b20c8e5 [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// 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
18/**
19 * Support for various automatic counting objects.
20 * This header defines templates for various kinds of automatic counting
21 * and sequencing objects. Templates are used to allow manipulation of
22 * various numerical-like types.
23 * @file ucommon/counter.h
24 */
25
26#ifndef _UCOMMON_COUNTER_H_
27#define _UCOMMON_COUNTER_H_
28
29#ifndef _UCOMMON_CONFIG_H_
30#include <ucommon/platform.h>
31#endif
32
33NAMESPACE_UCOMMON
34
35/**
36 * Automatic integer counting class. This is an automatic counting object
37 * that is used to retrieve a new integer value between 0 and n each time
38 * the object is referenced. When reaching the last n value, the object
39 * restarts at 0, and so is used to retrieve a sequence of values in order.
40 * @author David Sugar <dyfet@gnutelephony.org>
41 */
42class __EXPORT counter
43{
44private:
45 unsigned value, cycle;
46
47public:
48 /**
49 * Initialize integer counter of unknown size.
50 */
51 counter();
52
53 /**
54 * Initialize integer counter for a range of values.
55 * @param limit before recycling to zero.
56 */
57 counter(unsigned limit);
58
59 /**
60 * Get the next counter value.
61 * @return next counter value.
62 */
63 unsigned get(void);
64
65 /**
66 * Get the range of values before recycling.
67 * @return counter limit.
68 */
69 inline unsigned range(void)
70 {return cycle;};
71
72 /**
73 * Reference next counter value through pointer operation.
74 * @return next counter value.
75 */
76 inline unsigned operator*()
77 {return get();};
78
79 /**
80 * Reference next counter value by casting to integer.
81 * @return next counter value.
82 */
83 inline operator unsigned()
84 {return get();};
85
86 /**
87 * Assign the value of the counter.
88 * @param value to assign.
89 */
90 void operator=(unsigned value);
91};
92
93/**
94 * Automatically return a sequence of untyped objects. This is an automatic
95 * counter based class which returns the next pointer in an array of pointers
96 * and restarts the list when reaching the end. This is used to support the
97 * sequence template.
98 * @author David Sugar <dyfet@gnutelephony.org>
99 */
100class __EXPORT SeqCounter : protected counter
101{
102private:
103 void *item;
104 size_t offset;
105
106protected:
107 SeqCounter(void *start, size_t size, unsigned count);
108
109 void *get(void);
110
111 void *get(unsigned idx);
112
113public:
114 /**
115 * Used to directly assign sequence position in template.
116 * @param inc_offset in sequence to reset sequencing to.
117 */
118 inline void operator=(unsigned inc_offset)
119 {counter::operator=(inc_offset);};
120};
121
122/**
123 * Automatically toggle a bool on each reference.
124 * @author David Sugar <dyfet@gnutelephony.org>
125 */
126class __EXPORT toggle
127{
128private:
129 bool value;
130
131public:
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
148/**
149 * A template to return a sequence of objects of a specified type.
150 * This is used to return a different member in a sequence of objects of
151 * a specified type during each reference to the sequencer.
152 * @author David Sugar <dyfet@gnutelephony.org>
153 */
154template <class T>
155class sequence : public SeqCounter
156{
157protected:
158 inline T *get(unsigned idx)
159 {return static_cast<T *>(SeqCounter::get(idx));};
160
161public:
162 /**
163 * Create a template auto-sequence from a list of typed pointers.
164 * @param array of typed values to sequence on reference.
165 * @param size of list of typed values.
166 */
167 inline sequence(T *array, unsigned size) :
168 SeqCounter(array, sizeof(T), size) {};
169
170 /**
171 * Return next typed member of the sequence.
172 * @return next typed member of sequence.
173 */
174 inline T* get(void)
175 {return static_cast<T *>(SeqCounter::get());};
176
177 /**
178 * Return next typed member of the sequence by pointer reference.
179 * @return next typed member of sequence.
180 */
181 inline T& operator*()
182 {return *get();};
183
184 /**
185 * Return next typed member of the sequence by casted reference.
186 * @return next typed member of sequence.
187 */
188 inline operator T&()
189 {return *get();};
190
191 /**
192 * Return a specific typed member from the sequence list.
193 * @param offset of member to return.
194 * @return typed value at the specified offset.
195 */
196 inline T& operator[](unsigned offset)
197 {return *get(offset);};
198};
199
200/**
201 * A convenience typecast for integer counters.
202 */
203typedef counter counter_t;
204
205/**
206 * A convenience typecast for auto-toggled bools.
207 */
208typedef toggle toggle_t;
209
210END_NAMESPACE
211
212#endif