OWLNext
7.0
Borland's Object Windows Library for the modern age
Toggle main menu visibility
Loading...
Searching...
No Matches
bitset.h
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
/// Definition of a bit set and a character set
7
//----------------------------------------------------------------------------
8
9
#if !defined(OWL_BITSET_H)
10
#define OWL_BITSET_H
11
12
#include <
owl/private/defs.h
>
13
#if defined(BI_HAS_PRAGMA_ONCE)
14
# pragma once
15
#endif
16
17
#include <
owl/defs.h
>
18
#include <
owl/objstrm.h
>
19
20
#include <
owl/preclass.h
>
21
22
23
namespace
owl
{
24
25
/// \addtogroup utility
26
/// @{
27
/// \class TBitSet
28
// ~~~~~ ~~~~~~~
29
/// Simplifies setting and testing bits in a 32 count array of uint8 (32 bytes).
30
//
31
/// TBitSet sets or clears a single bit or a group of bits. You can use this class
32
/// to set and clear option flags and to retrieve information about a set of bits.
33
/// The class TCharSet performs similar operations for a string of characters.
34
35
template
<
class
T = u
int
8>
36
class
_OWLCLASS
TBitSet
{
37
public
:
38
TBitSet
();
39
TBitSet
(
const
TBitSet
& src);
40
41
bool
Has
(T item)
const
;
42
43
TBitSet
operator ~()
const
;
44
45
void
DisableItem
(T item);
46
void
EnableItem
(T item);
47
TBitSet
& operator +=(T item);
48
TBitSet
& operator -=(T item);
49
50
void
DisableItem
(
const
TBitSet
& bs);
51
void
EnableItem
(
const
TBitSet
& bs);
52
TBitSet
& operator +=(
const
TBitSet
& bs);
53
TBitSet
& operator |=(
const
TBitSet
& bs);
54
TBitSet
& operator -=(
const
TBitSet
& bs);
55
TBitSet
& operator &=(
const
TBitSet
& bs);
56
57
bool
IsEmpty
()
const
;
58
59
friend
TBitSet
operator &(
const
TBitSet
& bs1,
const
TBitSet
& bs2);
60
61
friend
TBitSet
operator |(
const
TBitSet
& bs1,
const
TBitSet
& bs2);
62
63
friend
int
operator ==(
const
TBitSet
& bs1,
const
TBitSet
& bs2);
64
65
friend
int
operator !=(
const
TBitSet
& bs1,
const
TBitSet
& bs2);
66
67
friend
opstream
& operator <<(
opstream
& out,
const
TBitSet
& bs)
68
{
69
out.
fwriteBytes
(bs.Bits,
sizeof
(bs.Bits));
70
return
out;
71
}
72
friend
ipstream
& operator >>(
ipstream
& in,
TBitSet
& bs)
73
{
74
in.
freadBytes
(bs.Bits,
sizeof
(bs.Bits));
75
return
in;
76
}
77
78
private
:
79
int
Loc(T item)
const
;
80
uint8
Mask(T item)
const
;
81
82
uint8
Bits[1 << (
sizeof
(T) * 8 - 3)];
83
};
84
85
template
<
class
T>
86
int
operator ==
(
const
TBitSet<T>
& bs1,
const
TBitSet<T>
& bs2)
87
{
88
for
(
int
i = 0; i <
sizeof
(bs1.Bits); i++)
89
if
(bs1.Bits[i] != bs2.Bits[i])
90
return
false
;
91
return
true
;
92
}
93
94
template
<
class
T>
95
TBitSet<T>
operator &
(
const
TBitSet<T>
& bs1,
const
TBitSet<T>
& bs2)
96
{
97
TBitSet<T>
temp(bs1);
98
temp &= bs2;
99
return
temp;
100
}
101
102
template
<
class
T>
103
TBitSet<T>
operator |
(
const
TBitSet<T>
& bs1,
const
TBitSet<T>
& bs2)
104
{
105
TBitSet<T>
temp(bs1);
106
temp |= bs2;
107
return
temp;
108
}
109
110
template
<
class
T>
111
int
operator !=
(
const
TBitSet<T>
& bs1,
const
TBitSet<T>
& bs2)
112
{
113
return
!
operator ==
(bs1, bs2);
114
}
115
116
117
//
118
/// \class TCharSet
119
// ~~~~~ ~~~~~~~~
120
/// Derived from TBitSet, TCharSet sets and clears bytes for a group of characters.
121
/// You can use this class to set or clear bits in a group of characters, such as
122
/// the capital letters from "A" through "Z" or the lowercase letters from "a"
123
/// through "z". The class TBitSet performs similar operations for a group of bits.
124
//
125
class
_OWLCLASS
TCharSet
:
public
TBitSet
<tchar> {
126
public
:
127
TCharSet
();
128
TCharSet
(
const
TBitSet<tchar>
& src);
129
TCharSet
(LPCTSTR str);
130
TCharSet
(
const
tstring
& str);
131
};
132
133
/// \class TBitFlags
134
/// TBitFlags is a *lightweight* class for setting, clearing and querrying
135
/// bit flags. It's intenteded to be used with a 'short' or 'long' type
136
/// allowing an easy method to handle 16 and 32 bit flags respectively.
137
//
138
/// For example:
139
/// \code
140
/// class TMyClass : public TMyBase, public TBitFlags<short> {
141
/// \endcode
142
//
143
template
<
class
T>
class
/*_OWLCLASS*/
TBitFlags
{
144
public
:
145
TBitFlags
(T t = 0);
146
147
// Query, Clear and Set flag bits
148
//
149
T
Clear
(T t);
150
T
Set
(T t);
151
bool
IsSet
(T t)
const
;
152
153
protected
:
154
T
Bits
;
155
};
156
157
/// @}
158
159
#include <
owl/posclass.h
>
160
161
//----------------------------------------------------------------------------
162
// Inline implementations
163
//
164
165
//
166
/// Initialize the Flags to the specified parameter
167
//
168
template
<
class
T>
169
TBitFlags<T>::TBitFlags
(T t)
170
:
171
Bits
(t)
172
{
173
}
174
175
//
176
/// Activate the bits that are enabled in the specified parameter
177
//
178
template
<
class
T>
179
T
TBitFlags<T>::Set
(T t)
180
{
181
return
Bits
|= t;
182
}
183
184
//
185
/// Clear the bits that are enabled in the specified parameter
186
//
187
template
<
class
T>
188
T
TBitFlags<T>::Clear
(T t)
189
{
190
return
Bits
&= ~t;
191
}
192
193
//
194
/// Return true of the ON bits of the parameter are currently enabled.
195
/// Return false otherwise.
196
//
197
template
<
class
T>
198
bool
TBitFlags<T>::IsSet
(T t)
const
199
{
200
return
(
Bits
& t) != 0;
201
}
202
203
204
}
// OWL namespace
205
206
207
#endif
// OWL_BITSET_H
owl::TBitFlags::Clear
T Clear(T t)
Clear the bits that are enabled in the specified parameter.
Definition
bitset.h:188
owl::TBitFlags::Bits
T Bits
Definition
bitset.h:154
owl::TBitFlags::IsSet
bool IsSet(T t) const
Return true of the ON bits of the parameter are currently enabled.
Definition
bitset.h:198
owl::TBitFlags::Set
T Set(T t)
Activate the bits that are enabled in the specified parameter.
Definition
bitset.h:179
owl::TBitFlags::TBitFlags
TBitFlags(T t=0)
Initialize the Flags to the specified parameter.
Definition
bitset.h:169
owl::TBitSet::DisableItem
void DisableItem(T item)
Turn off a specific bit.
Definition
bitset.cpp:65
owl::TBitSet::TBitSet
TBitSet()
Constructs a TBitSet object with all bits set to 0.
Definition
bitset.cpp:21
owl::TBitSet::Has
bool Has(T item) const
Return true if a bit is turned on.
Definition
bitset.cpp:42
owl::TBitSet::EnableItem
void EnableItem(T item)
Turn on a specific bit.
Definition
bitset.cpp:75
owl::TBitSet::IsEmpty
bool IsEmpty() const
Return true if all bits are off.
Definition
bitset.cpp:169
owl::TCharSet::TCharSet
TCharSet()
Construct a default character set.
Definition
bitset.cpp:210
owl::ipstream
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition
objstrm.h:391
owl::ipstream::freadBytes
void freadBytes(void *data, size_t sz)
Reads the number of bytes specified by sz into the supplied buffer (data).
Definition
objstrm.cpp:393
owl::opstream
Base class for writing streamable objects.
Definition
objstrm.h:480
owl::opstream::fwriteBytes
void fwriteBytes(const void *data, size_t sz)
Writes the specified number of bytes (sz) from the supplied buffer (data) to the stream.
Definition
objstrm.cpp:794
owl::operator!=
int operator!=(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition
bitset.h:111
owl::operator==
int operator==(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition
bitset.h:86
owl::operator&
TBitSet< T > operator&(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition
bitset.h:95
owl::operator|
TBitSet< T > operator|(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition
bitset.h:103
owl
Object Windows Library (OWLNext Core).
Definition
animctrl.h:22
owl::uint8
unsigned char uint8
Definition
number.h:32
owl::tstring
std::string tstring
Definition
defs.h:79
objstrm.h
defs.h
General definitions used by all ObjectWindows programs.
defs.h
_OWLCLASS
#define _OWLCLASS
Definition
defs.h:338
posclass.h
preclass.h
include
owl
bitset.h
Generated by
1.18.0