dbhandle.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Copyright 2015 Autodesk, Inc. All rights reserved.
  5. //
  6. // Use of this software is subject to the terms of the Autodesk license
  7. // agreement provided at the time of installation or download, or which
  8. // otherwise accompanies this software in either electronic or hard copy form.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //
  12. //
  13. // DESCRIPTION:
  14. //
  15. // This file contains the interface of the AcDbHandle class.
  16. // Instances of this class encapsulate an 8-byte AutoCAD database
  17. // handle value.
  18. #ifndef AD_DBHANDLE_H
  19. #define AD_DBHANDLE_H 1
  20. #include "adesk.h"
  21. #pragma pack (push, 8)
  22. class AcDbHandle
  23. {
  24. public:
  25. AcDbHandle(); // this ctor does not initialize the handle
  26. AcDbHandle(int lo, int hi); // this one does - useful for creating null handles
  27. AcDbHandle(const ACHAR*);
  28. AcDbHandle(const Adesk::UInt64 &);
  29. AcDbHandle& operator=(const AcDbHandle&);
  30. AcDbHandle& operator=(const ACHAR*);
  31. // This gets the hex digits into a string buffer.
  32. bool getIntoAsciiBuffer(ACHAR* pBuf, size_t nBufLen) const;
  33. enum {kStrSiz = 17}; // chars needed to hold handle (plus terminator) as a string
  34. // Helper template for fixed size arrays
  35. template<size_t nBufLen> inline bool getIntoAsciiBuffer(wchar_t (& buf)[nBufLen]) const
  36. {
  37. return this->getIntoAsciiBuffer(buf, nBufLen);
  38. }
  39. bool operator == (const AcDbHandle&) const;
  40. bool operator != (const AcDbHandle&) const;
  41. bool isNull() const;
  42. void setNull();
  43. Adesk::UInt32 low() const;
  44. Adesk::UInt32 high() const;
  45. void setLow(Adesk::UInt32 low);
  46. void setHigh(Adesk::UInt32 high);
  47. bool isOne(void) const;
  48. /////// Implementation Class Members: Not for 3rd Party Consumption ///////
  49. //
  50. AcDbHandle& operator++();
  51. AcDbHandle operator++(int);
  52. void increment(void);
  53. void decrement(void);
  54. AcDbHandle operator + (const AcDbHandle&) const;
  55. AcDbHandle slowOperatorPlus(const AcDbHandle&) const;
  56. AcDbHandle& operator += (const AcDbHandle&);
  57. AcDbHandle operator - (const AcDbHandle&) const;
  58. AcDbHandle& operator -= (const AcDbHandle&);
  59. bool operator > (const AcDbHandle&) const;
  60. bool operator >= (const AcDbHandle&) const;
  61. bool operator < (const AcDbHandle&) const;
  62. bool operator <= (const AcDbHandle&) const;
  63. int compare(const AcDbHandle&) const;
  64. void copyToOldType(Adesk::UInt8 hand[8]) const;
  65. void copyFromOldType(const Adesk::UInt8 hand[8]);
  66. void getValueBytes(Adesk::UInt8*, Adesk::UInt8*) const;
  67. void setValueBytes(Adesk::UInt8, const Adesk::UInt8*);
  68. // Value assignment/accessor methods using UInt32 are deprecated
  69. // and should not be used
  70. AcDbHandle& operator=(Adesk::UInt32);
  71. operator Adesk::UInt32() const;
  72. AcDbHandle& operator=(Adesk::UInt64);
  73. operator Adesk::UInt64() const;
  74. AcDbHandle operator + (Adesk::ULongPtr) const;
  75. void print() const;
  76. enum { kMaxValueBytes = 8 };
  77. int byte(Adesk::UInt32 i) const;
  78. bool restZeros(int i) const;
  79. private:
  80. Adesk::UInt64 & get64BitVal()
  81. { return *reinterpret_cast<Adesk::UInt64 *>(&mLow); }
  82. const Adesk::UInt64 & get64BitVal() const
  83. { return *reinterpret_cast<const Adesk::UInt64 *>(&mLow); }
  84. Adesk::UInt32 mLow;
  85. Adesk::UInt32 mHigh;
  86. friend class AcDbHandleTable;
  87. friend class HandleDataBase;
  88. };
  89. // This value is used to indicate cases where a conversion from
  90. // an AcDbHandle to an Adesk::UInt32 has gone bad because the
  91. // handle value was too great.
  92. //
  93. const Adesk::UInt32 kBadUInt32Handle = 0xFFFFFFFF;
  94. inline AcDbHandle::AcDbHandle()
  95. {
  96. }
  97. inline AcDbHandle::AcDbHandle(int lo, int hi) : mLow(lo), mHigh(hi)
  98. {
  99. }
  100. inline
  101. AcDbHandle::AcDbHandle(const Adesk::UInt64 &val)
  102. {
  103. this->get64BitVal() = val;
  104. }
  105. inline AcDbHandle&
  106. AcDbHandle::operator=(const AcDbHandle& handle)
  107. {
  108. this->get64BitVal() = handle.get64BitVal();
  109. return *this;
  110. }
  111. inline AcDbHandle&
  112. AcDbHandle::operator=(Adesk::UInt32 val)
  113. { mHigh = 0; mLow = val; return *this; }
  114. inline AcDbHandle&
  115. AcDbHandle::operator=(Adesk::UInt64 val)
  116. {
  117. this->get64BitVal() = val;
  118. return *this;
  119. }
  120. inline AcDbHandle
  121. AcDbHandle::operator+(const AcDbHandle& handle) const
  122. {
  123. AcDbHandle tHandle(*this);
  124. tHandle.get64BitVal() += handle.get64BitVal();
  125. return tHandle;
  126. }
  127. inline AcDbHandle
  128. AcDbHandle::operator+(Adesk::ULongPtr val) const
  129. {
  130. AcDbHandle tHandle(*this);
  131. tHandle.get64BitVal() += val;
  132. return tHandle;
  133. }
  134. inline bool
  135. AcDbHandle::operator > (const AcDbHandle& handle) const
  136. {
  137. return this->get64BitVal() > handle.get64BitVal();
  138. }
  139. inline int
  140. AcDbHandle::compare(const AcDbHandle& handle) const
  141. {
  142. if (this->get64BitVal() > handle.get64BitVal())
  143. return -1;
  144. else if (this->get64BitVal() == handle.get64BitVal())
  145. return 0;
  146. else
  147. return 1;
  148. }
  149. inline bool
  150. AcDbHandle::operator==(const AcDbHandle &handle) const
  151. {
  152. return this->get64BitVal() == handle.get64BitVal();
  153. }
  154. inline bool
  155. AcDbHandle::operator!=(const AcDbHandle &handle) const
  156. {
  157. return this->get64BitVal() != handle.get64BitVal();
  158. }
  159. inline bool
  160. AcDbHandle::isNull(void) const
  161. {
  162. return this->get64BitVal() == 0;
  163. }
  164. inline void AcDbHandle::setNull(void)
  165. {
  166. mHigh = mLow = 0;
  167. }
  168. inline bool
  169. AcDbHandle::isOne(void) const
  170. {
  171. return this->get64BitVal() == 1;
  172. }
  173. inline AcDbHandle&
  174. AcDbHandle::operator++(void) // ++AcDbHandle
  175. {
  176. this->get64BitVal()++;
  177. return *this;
  178. }
  179. inline void
  180. AcDbHandle::increment(void) // AcDbHandle = AcDbHandle + 1;
  181. {
  182. this->get64BitVal()++;
  183. }
  184. inline void
  185. AcDbHandle::decrement(void) // AcDbHandle = AcDbHandle - 1;
  186. {
  187. this->get64BitVal()--;
  188. }
  189. inline AcDbHandle
  190. AcDbHandle::operator++(int) // AcDbHandle++
  191. {
  192. AcDbHandle tempHandle = *this;
  193. ++(*this);
  194. return tempHandle;
  195. }
  196. inline AcDbHandle::operator Adesk::UInt32() const
  197. {
  198. if (mHigh != 0)
  199. return kBadUInt32Handle;
  200. return mLow;
  201. }
  202. inline AcDbHandle::operator Adesk::UInt64() const
  203. {
  204. return get64BitVal();
  205. }
  206. inline bool
  207. AcDbHandle::operator < (const AcDbHandle& handle) const
  208. {
  209. return this->get64BitVal() < handle.get64BitVal();
  210. }
  211. inline int AcDbHandle::byte(Adesk::UInt32 i) const
  212. {
  213. // Unicode: leave as unsigned char *
  214. return *((unsigned char *)&mLow + i);
  215. }
  216. inline bool AcDbHandle::restZeros(int i) const
  217. {
  218. if (i < 4) {
  219. const Adesk::UInt32 mask = ~0 << (i << 3);
  220. return !(mHigh | (mLow & mask));
  221. } else {
  222. const Adesk::UInt32 mask = ~0 << ((i - 4) << 3);
  223. return !(mHigh & mask);
  224. }
  225. }
  226. inline Adesk::UInt32 AcDbHandle::low() const
  227. {
  228. return mLow;
  229. }
  230. inline Adesk::UInt32 AcDbHandle::high() const
  231. {
  232. return mHigh;
  233. }
  234. inline void AcDbHandle::setLow(Adesk::UInt32 low)
  235. {
  236. mLow = low;
  237. return;
  238. }
  239. inline void AcDbHandle::setHigh(Adesk::UInt32 high)
  240. {
  241. mHigh = high;
  242. return;
  243. }
  244. #pragma pack (pop)
  245. #endif