mask.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Bit masks
  4. //
  5. //////////////////////////////////////////////////////////////////////////////
  6. #ifndef _mask_H_
  7. #define _mask_H_
  8. //////////////////////////////////////////////////////////////////////////////
  9. //
  10. // This class is a type safe way of dealing with flag words see surface.h
  11. // for example usage.
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. template<class Type, class WordType>
  15. class TBitMask
  16. {
  17. private:
  18. WordType m_word;
  19. public: //KGJV protected:
  20. TBitMask(WordType word) :
  21. m_word(word)
  22. {
  23. }
  24. public:
  25. TBitMask() :
  26. m_word(0)
  27. {
  28. }
  29. TBitMask(const TBitMask& bm) :
  30. m_word(bm.m_word)
  31. {
  32. }
  33. void operator=(const TBitMask& bm)
  34. {
  35. m_word = bm.m_word;
  36. }
  37. friend TBitMask operator|(const TBitMask& bm1, const TBitMask& bm2)
  38. {
  39. return bm1.m_word | bm2.m_word;
  40. }
  41. friend bool operator==(const TBitMask& bm1, const TBitMask& bm2)
  42. {
  43. return bm1.m_word == bm2.m_word;
  44. }
  45. friend bool operator!=(const TBitMask& bm1, const TBitMask& bm2)
  46. {
  47. return bm1.m_word != bm2.m_word;
  48. }
  49. TBitMask& Set(const TBitMask& bm)
  50. {
  51. m_word |= bm.m_word;
  52. return *this;
  53. }
  54. TBitMask& Clear(const TBitMask& bm)
  55. {
  56. m_word &= ~bm.m_word;
  57. return *this;
  58. }
  59. TBitMask& Mask(const TBitMask& bm)
  60. {
  61. m_word &= bm.m_word;
  62. return *this;
  63. }
  64. bool Test(const TBitMask& bm) const
  65. {
  66. return (m_word & bm.m_word) != 0;
  67. }
  68. bool Test(const TBitMask& bm, const TBitMask& bmResult) const
  69. {
  70. return (m_word & bm.m_word) == bmResult.m_word;
  71. }
  72. //
  73. // Unsafe accessors needed for interface with Win32
  74. //
  75. void SetWord(WordType word)
  76. {
  77. m_word = word;
  78. }
  79. WordType GetWord() const
  80. {
  81. return m_word;
  82. }
  83. };
  84. class BitMask : public TBitMask<DWORD, DWORD>
  85. {
  86. public:
  87. BitMask(DWORD dw) :
  88. TBitMask<DWORD, DWORD>(dw)
  89. {
  90. }
  91. BitMask() :
  92. TBitMask<DWORD, DWORD>(0)
  93. {
  94. }
  95. };
  96. //////////////////////////////////////////////////////////////////////////////
  97. //
  98. // This class implements a flag word with more than 32 bits.
  99. //
  100. //////////////////////////////////////////////////////////////////////////////
  101. template<int nBits> class TLargeBitMask
  102. {
  103. public:
  104. TLargeBitMask(void)
  105. {
  106. //ClearAll();
  107. }
  108. TLargeBitMask(const BYTE* bits)
  109. {
  110. Set(bits);
  111. }
  112. void Set(const BYTE* bits)
  113. {
  114. memcpy(&(m_bits[0]), bits, sizeof(m_bits));
  115. }
  116. void ClearAll(void)
  117. {
  118. memset(&(m_bits[0]), 0, sizeof(m_bits));
  119. }
  120. void SetAll(void)
  121. {
  122. memset(&(m_bits[0]), 0xff, sizeof(m_bits));
  123. }
  124. void ClearBit(int i)
  125. {
  126. int byte = i >> 3;
  127. int bit = i & 0x07;
  128. m_bits[byte] &= (0xff7f >> bit); //pad the high byte with 1's
  129. }
  130. void SetBit(int i)
  131. {
  132. int byte = i >> 3;
  133. int bit = i & 0x07;
  134. m_bits[byte] |= (0x80 >> bit);
  135. }
  136. bool GetBit(int i) const
  137. {
  138. int byte = i >> 3;
  139. int bit = i & 0x07;
  140. return 0 != (m_bits[byte] & (0x80 >> bit));
  141. }
  142. bool GetAllZero(void) const
  143. {
  144. for (int i = 0; (i < (nBits / 8)); i++)
  145. {
  146. if (m_bits[i] != 0)
  147. return false;
  148. }
  149. return true;
  150. }
  151. void ToString(char* pszBytes, int cch) const
  152. {
  153. int cb = min(cch / 2, sizeof(m_bits));
  154. for (int i = 0; i < cb; ++i)
  155. {
  156. char szByte[3];
  157. sprintf(szByte, "%02X", m_bits[i]);
  158. CopyMemory(pszBytes + (i * 2), szByte, 2);
  159. }
  160. }
  161. bool FromString(const char* pszBits)
  162. {
  163. int cch = strlen(pszBits);
  164. if (cch % 2)
  165. return false;
  166. BYTE bits[sizeof(m_bits)];
  167. ZeroMemory(bits, sizeof(bits));
  168. int cb = min(cch / 2, sizeof(m_bits));
  169. for (int i = 0; i < cb; ++i)
  170. {
  171. char szByte[3];
  172. CopyMemory(szByte, pszBits + (i * 2), 2);
  173. szByte[2] = '\0';
  174. long nBits = strtoul(szByte, NULL, 16);
  175. if ((0 == nBits || ULONG_MAX == nBits) && ERANGE == errno)
  176. return false;
  177. bits[i] = (BYTE)nBits;
  178. }
  179. Set(bits);
  180. return true;
  181. }
  182. TLargeBitMask& operator = (const TLargeBitMask& tbm)
  183. {
  184. memcpy(m_bits, tbm.m_bits, sizeof(m_bits));
  185. return *this;
  186. }
  187. TLargeBitMask& operator |= (const TLargeBitMask& tbm)
  188. {
  189. for (int i = 0; (i < (nBits / 8)); i++)
  190. m_bits[i] |= tbm.m_bits[i];
  191. return *this;
  192. }
  193. TLargeBitMask operator | (const TLargeBitMask& tbm) const
  194. {
  195. TLargeBitMask<nBits> r;
  196. for (int i = 0; (i < (nBits / 8)); i++)
  197. r.m_bits[i] = m_bits[i] | tbm.m_bits[i];
  198. return r;
  199. }
  200. TLargeBitMask& operator &= (const TLargeBitMask& tbm)
  201. {
  202. for (int i = 0; (i < (nBits / 8)); i++)
  203. m_bits[i] &= tbm.m_bits[i];
  204. return *this;
  205. }
  206. TLargeBitMask operator & (const TLargeBitMask& tbm) const
  207. {
  208. TLargeBitMask<nBits> r;
  209. for (int i = 0; (i < (nBits / 8)); i++)
  210. r.m_bits[i] = m_bits[i] & tbm.m_bits[i];
  211. return r;
  212. }
  213. bool operator == (const TLargeBitMask& tbm) const
  214. {
  215. bool rc = true;
  216. for (int i = 0; (i < (nBits / 8)); i++)
  217. {
  218. if (m_bits[i] != tbm.m_bits[i])
  219. {
  220. rc = false;
  221. break;
  222. }
  223. }
  224. return rc;
  225. }
  226. bool operator != (const TLargeBitMask& tbm) const
  227. {
  228. bool rc = true;
  229. for (int i = 0; (i < (nBits / 8)); i++)
  230. {
  231. if (m_bits[i] != tbm.m_bits[i])
  232. {
  233. rc = false;
  234. break;
  235. }
  236. }
  237. return !rc;
  238. }
  239. //Subset
  240. bool operator <= (const TLargeBitMask& tbm) const
  241. {
  242. bool rc = true;
  243. for (int i = 0; (i < (nBits / 8)); i++)
  244. {
  245. if ((m_bits[i] & tbm.m_bits[i]) != m_bits[i])
  246. {
  247. rc = false;
  248. break;
  249. }
  250. }
  251. return rc;
  252. }
  253. //Superset
  254. bool operator >= (const TLargeBitMask& tbm) const
  255. {
  256. bool rc = true;
  257. for (int i = 0; (i < (nBits / 8)); i++)
  258. {
  259. if ((m_bits[i] & tbm.m_bits[i]) != tbm.m_bits[i])
  260. {
  261. rc = false;
  262. break;
  263. }
  264. }
  265. return rc;
  266. }
  267. //"Difference": A - B == A & ~B
  268. //e.g. remove all the bits in B from A
  269. TLargeBitMask& operator -= (const TLargeBitMask& tbm)
  270. {
  271. for (int i = 0; (i < (nBits / 8)); i++)
  272. m_bits[i] &= ~tbm.m_bits[i];
  273. return *this;
  274. }
  275. TLargeBitMask operator - (const TLargeBitMask& tbm) const
  276. {
  277. TLargeBitMask<nBits> r;
  278. for (int i = 0; (i < (nBits / 8)); i++)
  279. r.m_bits[i] = m_bits[i] & ~tbm.m_bits[i];
  280. return r;
  281. }
  282. private:
  283. BYTE m_bits[((nBits - 1) / 8) + 1];
  284. };
  285. #endif