BitMask.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
11 
12 #include <stdio.h>
13 
15 
16 enum class BitMaskOperation
17  {
18  Or = 1,
19  And = 2,
20  Xor = 3,
21  OrNot = 4,
22  AndNot = 5,
23  };
24 
25 /*=================================================================================**/
38 typedef BitMaskP* BitMaskH;
39 
40 /*=================================================================================**/
54 {
55 private:
56  virtual void MakeClassAbstract() = 0;
57 public:
58 public:
59 
65 DGNPLATFORM_EXPORT static BitMaskP Create (bool defaultBitValue);
66 
72 DGNPLATFORM_EXPORT static BitMaskP Clone (BitMaskCR source);
73 
77 DGNPLATFORM_EXPORT static void FreeAndClear (BitMaskH bitMask);
78 
80 DGNPLATFORM_EXPORT void Free ();
81 
85 DGNPLATFORM_EXPORT UInt32 GetCapacity () const;
86 
92 DGNPLATFORM_EXPORT StatusInt EnsureCapacity (UInt32 numBits);
93 
98 DGNPLATFORM_EXPORT StatusInt SetCapacity (UInt32 numBits);
99 
103 DGNPLATFORM_EXPORT bool GetDefaultBitValue () const;
104 
108 DGNPLATFORM_EXPORT void SetDefaultBitValue (bool value);
109 
113 DGNPLATFORM_EXPORT bool Test (UInt32 bitIndex) const;
114 
118 DGNPLATFORM_EXPORT bool Test (BitMaskCR testMask) const;
119 
120 
124 DGNPLATFORM_EXPORT StatusInt SetBit (UInt32 bitIndex, bool value);
125 
128 DGNPLATFORM_EXPORT StatusInt Set (UInt32 bitIndex);
129 
131 DGNPLATFORM_EXPORT void SetAll ();
132 
136 DGNPLATFORM_EXPORT StatusInt Clear (UInt32 bitIndex);
137 
139 DGNPLATFORM_EXPORT void ClearAll ();
140 
143 DGNPLATFORM_EXPORT StatusInt Invert (UInt32 bitIndex);
144 
146 DGNPLATFORM_EXPORT void InvertAll ();
147 
151 DGNPLATFORM_EXPORT void Or (BitMaskCR rhs);
152 
156 DGNPLATFORM_EXPORT void And (BitMaskCR rhs);
157 
161 DGNPLATFORM_EXPORT void XOr (BitMaskCR rhs);
162 
167 
172 
178 DGNPLATFORM_EXPORT StatusInt SetBits (UInt32 arraySize, const UInt32 *bitIndexArray, bool value);
179 
182 DGNPLATFORM_EXPORT StatusInt SetFromBitMask (BitMaskCR source);
183 
188 DGNPLATFORM_EXPORT StatusInt SetFromBitArray (UInt32 numValidBits, const UInt16* bitArray);
189 
194 DGNPLATFORM_EXPORT void SetFromString (WStringCR sourceString, UInt32 indexOrigin, UInt32 maxIndex);
195 
198 DGNPLATFORM_EXPORT UInt32 GetNumBitsSet () const;
199 
204 DGNPLATFORM_EXPORT bool IsEqual (BitMaskCP other) const;
205 
209 DGNPLATFORM_EXPORT bool AnyBitSet () const;
210 
215 DGNPLATFORM_EXPORT void LogicalOperation (BitMaskCR rhs, BitMaskOperation operation);
216 
221 DGNPLATFORM_EXPORT void Dump (FILE *file, bool asString, UInt32 indexOrigin) const;
222 
223 
227 DGNPLATFORM_EXPORT void ToString (WStringR outString, UInt32 indexOrigin) const;
228 };
229 
230 /*=================================================================================**/
235 {
236 private:
237  BitMaskP m_mask;
238 
239 public:
242 explicit BitMaskHolder (bool defaultBitValue=false) {m_mask = BitMask::Create (defaultBitValue);}
243 
245 ~BitMaskHolder() {m_mask->Free();}
246 
250 BitMaskHolder (BitMaskHolder const& source) {m_mask = BitMask::Clone(*source.m_mask);}
251 
255 explicit BitMaskHolder (BitMask const& source) {m_mask = BitMask::Clone(source);}
256 
258 BitMaskHolder& operator=(BitMaskHolder const& source) {if (this == &source) return *this; m_mask->Free(); m_mask = BitMask::Clone(*source.m_mask); return *this;}
259 
261 BitMaskHolder& operator=(BitMaskCR source) {m_mask->Free(); m_mask = BitMask::Clone(source); return *this;}
262 
264 void SetBitMask (BitMaskP source)
265  {
266  if (source == m_mask)
267  return;
268 
269  if (NULL != m_mask)
270  m_mask->Free();
271 
272  m_mask = source;
273  }
274 
276 BitMaskP operator->() {return m_mask;}
277 
278 // Access the BitMask's methods
279 BitMaskCP operator->() const {return m_mask;}
280 
282 BitMaskR operator*() {return *m_mask;}
283 
285 BitMaskP *operator& () { return &m_mask; }
286 
288 operator BitMaskP () { return m_mask; }
289 
290 // Access the BitMask's methods
291 BitMaskCR operator*() const {return *m_mask;}
292 
294 BitMaskCP get() const {return m_mask;}
295 };
296 
297 //=======================================================================================
298 // @bsiclass Bentley Systems
299 //=======================================================================================
300 template <bool defaultVal> struct BitMaskRef
301 {
302 private:
303  BitMaskP m_mask;
304 
305 public:
306  void Clear() {if (m_mask) m_mask->Free(); m_mask=NULL;}
307  explicit BitMaskRef () {m_mask = NULL;}
308  ~BitMaskRef () {Clear();}
309 
310  BitMaskP GetBitMask() {if (NULL == m_mask) {m_mask=BitMask::Create(defaultVal);} return m_mask;}
311  bool IsValid() const {return NULL != m_mask;}
312 };
313 
BitMaskP * BitMaskH
Definition: BitMask.h:38
BitMaskHolder(BitMaskHolder const &source)
Construct a new Instance of BitMaskHolder from an existing BitMaskHolder.
Definition: BitMask.h:250
struct Bentley::BitMask const & BitMaskCR
Definition: DgnPlatform.h:134
BitMaskP operator->()
Access the BitMask held by this BitMaskHolder.
Definition: BitMask.h:276
struct Bentley::BitMask * BitMaskP
Definition: DgnPlatform.h:134
struct Bentley::WString const & WStringCR
Definition: Bentley.h:239
~BitMaskRef()
Definition: BitMask.h:308
BitMaskHolder(bool defaultBitValue=false)
Construct a new Instance of BitMaskHolder.
Definition: BitMask.h:242
BitMaskP GetBitMask()
Definition: BitMask.h:310
uint16_t UInt16
Definition: Bentley.r.h:118
#define DGNPLATFORM_EXPORT
Definition: DgnPlatform/ExportMacros.h:58
~BitMaskHolder()
Destuctor.
Definition: BitMask.h:245
uint32_t UInt32
Definition: Bentley.r.h:128
BitMaskHolder & operator=(BitMaskCR source)
Assign a new BitMask to this BitMaskHolder.
Definition: BitMask.h:261
#define NULL
Definition: Bentley.h:157
Definition: BitMask.h:300
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
BitMaskHolder(BitMask const &source)
Construct a new Instance of BitMaskHolder from an existing BitMask.
Definition: BitMask.h:255
BitMaskCP operator->() const
Definition: BitMask.h:279
void Clear()
Definition: BitMask.h:306
BitMaskRef()
Definition: BitMask.h:307
Base class to make a class non-copyable.
Definition: NonCopyableClass.h:23
A Bitmask holds a variable-sized array of bits.
Definition: BitMask.h:53
int StatusInt
Definition: Bentley.h:222
void SetBitMask(BitMaskP source)
Assign a new BitMask to this BitMaskHolder. Takes over responsibility for freeing the source...
Definition: BitMask.h:264
struct Bentley::BitMask const * BitMaskCP
Definition: DgnPlatform.h:134
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
BitMaskHolder & operator=(BitMaskHolder const &source)
Assign a new BitMask to this BitMaskHolder.
Definition: BitMask.h:258
DgnECHostType operator&(DgnECHostType a, DgnECHostType b)
Definition: DgnECInstance.h:92
bool IsValid() const
Definition: BitMask.h:311
BitMaskR operator*()
Access the BitMask held by this BitMaskHolder.
Definition: BitMask.h:282
BitMaskCR operator*() const
Definition: BitMask.h:291
BitMaskHolder acts as a smart pointer.
Definition: BitMask.h:234
BitMaskOperation
Definition: BitMask.h:16
A string class that has many of the same capabilities as std::string, plus additional functions such ...
Definition: WString.h:51

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.