FlagSet.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2019 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* https://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* */
40 /* Handy type-safe template class for flag sets. */
41 /* */
42 /******************************************************************************/
43 
44 #ifndef FlagSet_Included
45 #define FlagSet_Included
46 #include "BitSet.hpp"
47 
48 // a useful type-safe bit flag class where the allowed settings are based
49 // on enum values.
50 
51 // this is defined as a template so we can specify what enums are stored here.
52 template<typename TEnum, int TMaxFlags = 8>
53 class FlagSet
54 {
55  public:
56  // a reference to a flag set item. This is a proxy that allows
57  // assignment semantics
59  {
60  friend class FlagSet<TEnum, TMaxFlags>;
61 
62  public:
63  inline ~FlagSetReference() { }
64 
65  // assign bool to the a flag position.
67  {
68  refSet->set(refPos, val);
69  return *this;
70  }
71 
72  // allows a flag value to be retrieved.
73  operator bool() const
74  {
75  return refSet->test(refPos);
76  }
77 
78  private:
79  // constructor for a reference proxy
81  : refSet(&theSet), refPos(pos) { }
82 
83  // pointer to the set we're proxying
85  // the element we test
86  TEnum refPos;
87  };
88 
89  FlagSet() { }
90  // some constructors for creating constant sets for
91  // testing setting
92  FlagSet(const TEnum f1)
93  {
94  set(f1);
95  }
96 
97  FlagSet(const TEnum f1, const TEnum f2)
98  {
99  set(f1);
100  set(f2);
101  }
102 
103  FlagSet(const TEnum f1, const TEnum f2, const TEnum f3)
104  {
105  set(f1);
106  set(f2);
107  set(f3);
108  }
109 
110  FlagSet(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4)
111  {
112  set(f1);
113  set(f2);
114  set(f3);
115  set(f4);
116  }
117 
118  FlagSet(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4, const TEnum f5)
119  {
120  set(f1);
121  set(f2);
122  set(f3);
123  set(f4);
124  set(f5);
125  }
126 
127  FlagSet(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4, const TEnum f5, const TEnum f6)
128  {
129  set(f1);
130  set(f2);
131  set(f3);
132  set(f4);
133  set(f5);
134  set(f6);
135  }
136 
137  // set a flag value to true
138  inline void set(const TEnum flag, bool val = true)
139  {
140  flags.set(flag, val);
141  }
142 
143 
144  // set all flag values to true
145  inline void set()
146  {
147  flags.set();
148  }
149 
150 
151  // set multiple flags values on
152  inline void set(const TEnum f1, const TEnum f2)
153  {
154  flags.set(f1);
155  flags.set(f2);
156  }
157 
158 
159  // set multiple flag values on
160  inline void set(const TEnum f1, const TEnum f2, const TEnum f3)
161  {
162  flags.set(f1);
163  flags.set(f2);
164  flags.set(f3);
165  }
166 
167 
168  // set multiple flag values on
169  inline void set(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4)
170  {
171  flags.set(f1);
172  flags.set(f2);
173  flags.set(f3);
174  flags.set(f4);
175  }
176 
177 
178  // turn all flag values off
179  inline void reset()
180  {
181  flags.reset();
182  }
183 
184  // turn a flag value off
185  inline void reset(const TEnum flag)
186  {
187  flags.reset(flag);
188  }
189 
190 
191  // turn a flag value off
192  inline void reset(const TEnum f1, const TEnum f2)
193  {
194  flags.reset(f1);
195  flags.reset(f2);
196  }
197 
198 
199  // turn a flag value off
200  inline void reset(const TEnum f1, const TEnum f2, const TEnum f3)
201  {
202  flags.reset(f1);
203  flags.reset(f2);
204  flags.reset(f3);
205  }
206 
207 
208  // turn a flag value off
209  inline void reset(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4)
210  {
211  flags.reset(f1);
212  flags.reset(f2);
213  flags.reset(f3);
214  flags.reset(f4);
215  }
216 
217 
218  // flip the value of a bit
219  inline void flip(const TEnum flag)
220  {
221  flags.flip(flag);
222  }
223 
224 
225  // flip all flag values
226  inline void flip()
227  {
228  flags.flip();
229  }
230 
231 
232  // test a flag value
233  inline bool test(const TEnum flag)
234  {
235  return flags.test(flag);
236  }
237 
238 
239  // access the value of a flag
240  inline bool operator [](const TEnum flag) const
241  {
242  return flags.test(flag);
243  }
244 
245  // proxied access of a flag (allows assignment)
246  FlagSetReference operator [](const TEnum flag)
247  {
248  return FlagSetReference(*this, flag);
249  }
250 
251 
252  // test if any flags are set
253  inline bool any() const
254  {
255  return flags.any();
256  }
257 
258  // test for any of the specified flags being set
259  inline bool any(const TEnum flag1, const TEnum flag2)
260  {
261  return flags.test(flag1) || flags.test(flag2);
262  }
263 
264  // test for any of the specified flags being set
265  inline bool any(const TEnum flag1, const TEnum flag2, const TEnum flag3)
266  {
267  return flags.test(flag1) || flags.test(flag2) || flags.test(flag3);
268  }
269 
270 
271  // test if all flags are set
272  inline bool all() const
273  {
274  return flags.all();
275  }
276 
277 
278 
279  // test for all of the specified flags being set
280  inline bool all(const TEnum flag1, const TEnum flag2)
281  {
282  return flags.test(flag1) && flags.test(flag2);
283  }
284 
285  // test for all of the specified flags being set
286  inline bool all(const TEnum flag1, const TEnum flag2, const TEnum flag3)
287  {
288  return flags.test(flag1) && flags.test(flag2) && flags.test(flag3);
289  }
290 
291  // test if no flags are set
292  inline bool none() const
293  {
294  return flags.none();
295  }
296 
297 private:
298 
300 };
301 
302 
303 #endif
304 
FlagSetReference(FlagSet< TEnum, TMaxFlags > &theSet, TEnum pos)
Definition: FlagSet.hpp:80
FlagSet< TEnum, TMaxFlags > * refSet
Definition: FlagSet.hpp:84
FlagSetReference & operator=(bool val)
Definition: FlagSet.hpp:66
void set()
Definition: FlagSet.hpp:145
void reset(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4)
Definition: FlagSet.hpp:209
bool any() const
Definition: FlagSet.hpp:253
void reset(const TEnum f1, const TEnum f2)
Definition: FlagSet.hpp:192
void flip(const TEnum flag)
Definition: FlagSet.hpp:219
bool any(const TEnum flag1, const TEnum flag2)
Definition: FlagSet.hpp:259
FlagSet()
Definition: FlagSet.hpp:89
void set(const TEnum f1, const TEnum f2)
Definition: FlagSet.hpp:152
FlagSet(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4)
Definition: FlagSet.hpp:110
void reset()
Definition: FlagSet.hpp:179
FlagSet(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4, const TEnum f5)
Definition: FlagSet.hpp:118
bool all(const TEnum flag1, const TEnum flag2, const TEnum flag3)
Definition: FlagSet.hpp:286
void set(const TEnum f1, const TEnum f2, const TEnum f3)
Definition: FlagSet.hpp:160
bool any(const TEnum flag1, const TEnum flag2, const TEnum flag3)
Definition: FlagSet.hpp:265
bool operator[](const TEnum flag) const
Definition: FlagSet.hpp:240
FlagSet(const TEnum f1)
Definition: FlagSet.hpp:92
void reset(const TEnum f1, const TEnum f2, const TEnum f3)
Definition: FlagSet.hpp:200
BitSet< TMaxFlags > flags
Definition: FlagSet.hpp:299
FlagSet(const TEnum f1, const TEnum f2)
Definition: FlagSet.hpp:97
void reset(const TEnum flag)
Definition: FlagSet.hpp:185
bool test(const TEnum flag)
Definition: FlagSet.hpp:233
void flip()
Definition: FlagSet.hpp:226
bool none() const
Definition: FlagSet.hpp:292
void set(const TEnum flag, bool val=true)
Definition: FlagSet.hpp:138
bool all() const
Definition: FlagSet.hpp:272
FlagSet(const TEnum f1, const TEnum f2, const TEnum f3)
Definition: FlagSet.hpp:103
bool all(const TEnum flag1, const TEnum flag2)
Definition: FlagSet.hpp:280
void set(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4)
Definition: FlagSet.hpp:169
FlagSet(const TEnum f1, const TEnum f2, const TEnum f3, const TEnum f4, const TEnum f5, const TEnum f6)
Definition: FlagSet.hpp:127