OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bitset.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of TBitSet, a set of up to 256 bit flags
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/bitset.h>
10#include <owl/objstrm.h>
11#include <type_traits>
12
13namespace owl {
14
16
17//
18/// Constructs a TBitSet object with all bits set to 0.
19//
20template <class T>
22{
23 for (unsigned int i = 0; i < sizeof(Bits); i++)
24 Bits[i] = 0;
25}
26
27//
28/// Constructs a TBitSet object as a copy of another TBitSet.
29//
30template <class T>
32{
33 for (unsigned int i = 0; i < sizeof(Bits); i++)
34 Bits[i] = bs.Bits[i];
35}
36
37//
38/// Return true if a bit is turned on.
39//
40template <class T>
41bool
42TBitSet<T>::Has(T item) const
44 return (Bits[Loc(item)] & Mask(item)) != 0;
47//
48/// Negate a bit.
49//
50template <class T>
55 for (unsigned int i = 0; i < sizeof(Bits); i++)
56 temp.Bits[i] = uint8(~Bits[i]);
57 return temp;
58}
59
60//
61/// Turn off a specific bit.
62//
63template <class T>
64void
66{
67 Bits[Loc(item)] &= uint8(~Mask(item));
68}
69
70//
71/// Turn on a specific bit.
72//
73template <class T>
74void
76{
77 Bits[Loc(item)] |= Mask(item);
78}
79
80//
81/// Enable a specific bit.
82//
83template <class T>
85{
86 EnableItem(item);
87 return *this;
88}
89
90//
91/// Disable a specific bit.
92//
93template <class T>
95{
96 DisableItem(item);
97 return *this;
98}
99
100//
101/// Turn off all the given bits.
102//
103template <class T>
104void
106{
107 for (unsigned int i = 0; i < sizeof(Bits); i++)
108 Bits[i] &= uint8(~(bs.Bits[i]));
109}
110
111//
112/// Turn on all the given bits.
113//
114template <class T>
115void
117{
118 for (unsigned int i = 0; i < sizeof(Bits); i++)
119 Bits[i] |= bs.Bits[i];
120}
121
122//
123/// Enable all the given bits.
124//
125template <class T>
127{
128 EnableItem(bs);
129 return *this;
130}
131
132//
133/// Enable all the given bits.
134//
135template <class T>
137{
138 EnableItem(bs);
139 return *this;
140}
141
142//
143/// Disable all the given bits.
144//
145template <class T>
147{
148 DisableItem(bs);
149 return *this;
150}
151
152//
153/// Logically AND each individual bits.
154//
155template <class T>
158{
159 for (unsigned int i = 0; i < sizeof(Bits); i++)
160 Bits[i] &= bs.Bits[i];
161 return *this;
162}
163
164//
165/// Return true if all bits are off.
166//
167template <class T>
168bool
170{
171 for (unsigned int i = 0; i < sizeof(Bits); i++)
172 if (Bits[i] != 0)
173 return false;
174 return true;
175}
176
177//
178/// Return the index of the item searched for.
179//
180template <class T>
181int TBitSet<T>::Loc(T item) const
182{
183 return static_cast<typename std::make_unsigned<T>::type>(item) / 8;
184}
185
186//
187/// Return the mask needed to look for a specific bit.
188//
189template <class T>
190uint8 TBitSet<T>::Mask(T item) const
191{
192 return static_cast<uint8>(1 << (static_cast<typename std::make_unsigned<T>::type>(item) % 8));
193}
194
195//
196// Explicit instantiations.
197// These are the only instantiations that are supported.
198// (Otherwise the implementation above needs to be moved to the header,
199// so that the user can instantiate it with other template arguments).
200//
201template class TBitSet<uint8>;
202template class TBitSet<tchar>;
203
204//----------------------------------------------------------------------------
205
206//
207/// Construct a default character set.
208/// All items are set to 0.
209//
211:
212 TBitSet<tchar>()
213{
214}
215
216//
217/// Copy the bitset into this character set.
218//
220:
222{
223}
224
225//
226/// Construct a character set from a string.
227/// The characters act as the index for the bitset.
228//
230:
231 TBitSet<tchar>()
232{
233 for (LPCTSTR p = str; *p; p++) {
234 if (*p == _T('\\'))
235 p++;
236 else if (*p == _T('-') && p > str && p[1]) { // handle "A-Z" type shorthands
237 p++;
238 for (tchar c = tchar(p[-2]+1); c < *p; c++) // replace "-" with "B..Y"
239 EnableItem(static_cast<uint8>(c));
240 }
241 EnableItem(static_cast<uint8>(*p));
242 }
243}
244
245//
246/// Construct a character set from a string.
247/// The characters act as the index for the bitset.
248//
250:
251 TBitSet<tchar>()
252{
253 const LPCTSTR str = s.c_str();
254 for (LPCTSTR p = str; *p; p++) {
255 if (*p == _T('\\'))
256 p++;
257 else if (*p == _T('-') && p > str && p[1]) { // handle "A-Z" type shorthands
258 p++;
259 for (tchar c = tchar(p[-2]+1); c < *p; c++) // replace "-" with "B..Y"
260 EnableItem(static_cast<uint8>(c));
261 }
262 EnableItem(static_cast<uint8>(*p));
263 }
264}
265
266} // OWL namespace
267//==============================================================================
Definition of a bit set and a character set.
Simplifies setting and testing bits in a 32 count array of uint8 (32 bytes).
Definition bitset.h:36
TBitSet & operator-=(T item)
Disable a specific bit.
Definition bitset.cpp:94
TBitSet operator~() const
Negate a bit.
Definition bitset.cpp:52
void DisableItem(T item)
Turn off a specific bit.
Definition bitset.cpp:65
TBitSet & operator+=(T item)
Enable a specific bit.
Definition bitset.cpp:84
TBitSet()
Constructs a TBitSet object with all bits set to 0.
Definition bitset.cpp:21
bool Has(T item) const
Return true if a bit is turned on.
Definition bitset.cpp:42
TBitSet & operator|=(const TBitSet &bs)
Enable all the given bits.
Definition bitset.cpp:136
void EnableItem(T item)
Turn on a specific bit.
Definition bitset.cpp:75
bool IsEmpty() const
Return true if all bits are off.
Definition bitset.cpp:169
TBitSet & operator&=(const TBitSet &bs)
Logically AND each individual bits.
Definition bitset.cpp:157
TCharSet()
Construct a default character set.
Definition bitset.cpp:210
#define _T(x)
Definition cygwin.h:51
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned char uint8
Definition number.h:32
char tchar
Definition defs.h:77
OWL_DIAGINFO
Definition animctrl.cpp:14
std::string tstring
Definition defs.h:79