OWLNext    7.0
Borland's Object Windows Library for the modern age
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
23namespace 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
35template <class T = uint8>
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);
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
68 {
69 out.fwriteBytes(bs.Bits, sizeof(bs.Bits));
70 return out;
71 }
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
85template<class T>
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
94template<class T>
96{
98 temp &= bs2;
99 return temp;
100}
101
102template<class T>
104{
106 temp |= bs2;
107 return temp;
108}
109
110template<class T>
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//
125class _OWLCLASS TCharSet : public TBitSet<tchar> {
126 public:
127 TCharSet();
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//
143template <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:
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//
168template <class T>
170:
171 Bits(t)
172{
173}
174
175//
176/// Activate the bits that are enabled in the specified parameter
177//
178template <class T>
180{
181 return Bits |= t;
182}
183
184//
185/// Clear the bits that are enabled in the specified parameter
186//
187template <class 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//
197template <class T>
198bool 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
For example:
Definition bitset.h:143
T Clear(T t)
Clear the bits that are enabled in the specified parameter.
Definition bitset.h:188
bool IsSet(T t) const
Return true of the ON bits of the parameter are currently enabled.
Definition bitset.h:198
T Set(T t)
Activate the bits that are enabled in the specified parameter.
Definition bitset.h:179
TBitFlags(T t=0)
Initialize the Flags to the specified parameter.
Definition bitset.h:169
Simplifies setting and testing bits in a 32 count array of uint8 (32 bytes).
Definition bitset.h:36
Derived from TBitSet, TCharSet sets and clears bytes for a group of characters.
Definition bitset.h:125
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
Base class for writing streamable objects.
Definition objstrm.h:480
int operator!=(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition bitset.h:111
int operator==(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition bitset.h:86
TBitSet< T > operator&(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition bitset.h:95
TBitSet< T > operator|(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition bitset.h:103
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned char uint8
Definition number.h:32
std::string tstring
Definition defs.h:79
General definitions used by all ObjectWindows programs.
#define _OWLCLASS
Definition defs.h:338