pointerTo.I 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // Filename: pointerTo.I
  2. // Created by: drose (10Feb99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. ////////////////////////////////////////////////////////////////////
  6. // Function: PointerToBase::Constructor
  7. // Access: Protected
  8. // Description:
  9. ////////////////////////////////////////////////////////////////////
  10. template<class T>
  11. INLINE PointerToBase<T>::
  12. PointerToBase(To *ptr) {
  13. _ptr = (To *)NULL;
  14. reassign(ptr);
  15. }
  16. ////////////////////////////////////////////////////////////////////
  17. // Function: PointerToBase::Copy Constructor
  18. // Access: Protected
  19. // Description:
  20. ////////////////////////////////////////////////////////////////////
  21. template<class T>
  22. INLINE PointerToBase<T>::
  23. PointerToBase(const PointerToBase<T> &copy) {
  24. _ptr = (To *)NULL;
  25. reassign(copy);
  26. }
  27. ////////////////////////////////////////////////////////////////////
  28. // Function: PointerToBase::Destructor
  29. // Access: Protected
  30. // Description:
  31. ////////////////////////////////////////////////////////////////////
  32. template<class T>
  33. INLINE PointerToBase<T>::
  34. ~PointerToBase() {
  35. reassign((To *)NULL);
  36. }
  37. ////////////////////////////////////////////////////////////////////
  38. // Function: PointerToBase::reassign
  39. // Access: Protected
  40. // Description: This is the main work of the PointerTo family. When
  41. // the pointer is reassigned, decrement the old
  42. // reference count and increment the new one.
  43. ////////////////////////////////////////////////////////////////////
  44. template<class T>
  45. void PointerToBase<T>::
  46. reassign(To *ptr) {
  47. if (ptr != _ptr) {
  48. if (_ptr != (To *)NULL) {
  49. unref_delete(_ptr);
  50. }
  51. _ptr = ptr;
  52. if (_ptr != (To *)NULL) {
  53. _ptr->ref();
  54. #ifndef NDEBUG
  55. if (MemoryUsage::get_track_memory_usage()) {
  56. // Make sure the MemoryUsage record knows what the TypeHandle
  57. // is, if we know it ourselves.
  58. TypeHandle type = get_type_handle(To);
  59. if (type == TypeHandle::none()) {
  60. do_init_type(To);
  61. type = get_type_handle(To);
  62. }
  63. if (type != TypeHandle::none()) {
  64. MemoryUsage::update_type(_ptr, type);
  65. }
  66. }
  67. #endif
  68. }
  69. }
  70. }
  71. ////////////////////////////////////////////////////////////////////
  72. // Function: PointerToBase::reassign
  73. // Access: Protected
  74. // Description:
  75. ////////////////////////////////////////////////////////////////////
  76. template<class T>
  77. INLINE void PointerToBase<T>::
  78. reassign(const PointerToBase<To> &copy) {
  79. reassign(copy._ptr);
  80. }
  81. #ifndef CPPPARSER
  82. #ifndef WIN32_VC
  83. ////////////////////////////////////////////////////////////////////
  84. // Function: PointerToBase::Equivalence operator
  85. // Access: Public
  86. // Description:
  87. ////////////////////////////////////////////////////////////////////
  88. template<class T>
  89. INLINE bool PointerToBase<T>::
  90. operator == (const To *other) const {
  91. return _ptr == other;
  92. }
  93. ////////////////////////////////////////////////////////////////////
  94. // Function: PointerToBase::Nonequivalence operator
  95. // Access: Public
  96. // Description:
  97. ////////////////////////////////////////////////////////////////////
  98. template<class T>
  99. INLINE bool PointerToBase<T>::
  100. operator != (const To *other) const {
  101. return _ptr != other;
  102. }
  103. ////////////////////////////////////////////////////////////////////
  104. // Function: PointerToBase::Greater-than operator
  105. // Access: Public
  106. // Description:
  107. ////////////////////////////////////////////////////////////////////
  108. template<class T>
  109. INLINE bool PointerToBase<T>::
  110. operator > (const To *other) const {
  111. return _ptr > other;
  112. }
  113. ////////////////////////////////////////////////////////////////////
  114. // Function: PointerToBase::Less-than-or-equal operator
  115. // Access: Public
  116. // Description:
  117. ////////////////////////////////////////////////////////////////////
  118. template<class T>
  119. INLINE bool PointerToBase<T>::
  120. operator <= (const To *other) const {
  121. return _ptr <= other;
  122. }
  123. ////////////////////////////////////////////////////////////////////
  124. // Function: PointerToBase::Greater-than-or-equal operator
  125. // Access: Public
  126. // Description:
  127. ////////////////////////////////////////////////////////////////////
  128. template<class T>
  129. INLINE bool PointerToBase<T>::
  130. operator >= (const To *other) const {
  131. return _ptr >= other;
  132. }
  133. ////////////////////////////////////////////////////////////////////
  134. // Function: PointerToBase::Equivalence operator
  135. // Access: Public
  136. // Description:
  137. ////////////////////////////////////////////////////////////////////
  138. template<class T>
  139. INLINE bool PointerToBase<T>::
  140. operator == (const PointerToBase<To> &other) const {
  141. return _ptr == other._ptr;
  142. }
  143. ////////////////////////////////////////////////////////////////////
  144. // Function: PointerToBase::Nonequivalence operator
  145. // Access: Public
  146. // Description:
  147. ////////////////////////////////////////////////////////////////////
  148. template<class T>
  149. INLINE bool PointerToBase<T>::
  150. operator != (const PointerToBase<To> &other) const {
  151. return _ptr != other._ptr;
  152. }
  153. ////////////////////////////////////////////////////////////////////
  154. // Function: PointerToBase::Greater-than operator
  155. // Access: Public
  156. // Description:
  157. ////////////////////////////////////////////////////////////////////
  158. template<class T>
  159. INLINE bool PointerToBase<T>::
  160. operator > (const PointerToBase<To> &other) const {
  161. return _ptr > other._ptr;
  162. }
  163. ////////////////////////////////////////////////////////////////////
  164. // Function: PointerToBase::Less-than-or-equal operator
  165. // Access: Public
  166. // Description:
  167. ////////////////////////////////////////////////////////////////////
  168. template<class T>
  169. INLINE bool PointerToBase<T>::
  170. operator <= (const PointerToBase<To> &other) const {
  171. return _ptr <= other._ptr;
  172. }
  173. ////////////////////////////////////////////////////////////////////
  174. // Function: PointerToBase::Greater-than-or-equal operator
  175. // Access: Public
  176. // Description:
  177. ////////////////////////////////////////////////////////////////////
  178. template<class T>
  179. INLINE bool PointerToBase<T>::
  180. operator >= (const PointerToBase<To> &other) const {
  181. return _ptr >= other._ptr;
  182. }
  183. #endif // WIN32_VC
  184. ////////////////////////////////////////////////////////////////////
  185. // Function: PointerToBase::Less-than operator
  186. // Access: Public
  187. // Description:
  188. ////////////////////////////////////////////////////////////////////
  189. template<class T>
  190. INLINE bool PointerToBase<T>::
  191. operator < (const To *other) const {
  192. return _ptr < other;
  193. }
  194. ////////////////////////////////////////////////////////////////////
  195. // Function: PointerToBase::Less-than operator
  196. // Access: Public
  197. // Description:
  198. ////////////////////////////////////////////////////////////////////
  199. template<class T>
  200. INLINE bool PointerToBase<T>::
  201. operator < (const PointerToBase<To> &other) const {
  202. return _ptr < other._ptr;
  203. }
  204. #endif // CPPPARSER
  205. ////////////////////////////////////////////////////////////////////
  206. // Function: PointerToBase::is_null
  207. // Access: Public
  208. // Description: Returns true if the PointerTo is a NULL pointer,
  209. // false otherwise. (Direct comparison to a NULL
  210. // pointer also works.)
  211. ////////////////////////////////////////////////////////////////////
  212. template<class T>
  213. INLINE bool PointerToBase<T>::
  214. is_null() const {
  215. return (_ptr == NULL);
  216. }
  217. ////////////////////////////////////////////////////////////////////
  218. // Function: PointerToBase::clear
  219. // Access: Public
  220. // Description: A convenient way to set the PointerTo object to NULL.
  221. // (Assignment to a NULL pointer also works, of course.)
  222. ////////////////////////////////////////////////////////////////////
  223. template<class T>
  224. INLINE void PointerToBase<T>::
  225. clear() {
  226. reassign((To *)NULL);
  227. }
  228. ////////////////////////////////////////////////////////////////////
  229. // Function: PointerToBase::output
  230. // Access: Public
  231. // Description: A handy function to output PointerTo's as a hex
  232. // pointer followed by a reference count.
  233. ////////////////////////////////////////////////////////////////////
  234. template<class T>
  235. INLINE void PointerToBase<T>::
  236. output(ostream &out) const {
  237. out << (void *)_ptr;
  238. if (_ptr != (To *)NULL) {
  239. out << ":" << _ptr->get_ref_count();
  240. }
  241. }
  242. ////////////////////////////////////////////////////////////////////
  243. // Function: PointerTo::Constructor
  244. // Access: Public
  245. // Description:
  246. ////////////////////////////////////////////////////////////////////
  247. template<class T>
  248. INLINE PointerTo<T>::
  249. PointerTo(To *ptr) : PointerToBase<T>(ptr) {
  250. }
  251. ////////////////////////////////////////////////////////////////////
  252. // Function: PointerTo::Copy Constructor
  253. // Access: Public
  254. // Description:
  255. ////////////////////////////////////////////////////////////////////
  256. template<class T>
  257. INLINE PointerTo<T>::
  258. PointerTo(const PointerTo<T> &copy) :
  259. PointerToBase<T>((const PointerToBase<T> &)copy)
  260. {
  261. }
  262. ////////////////////////////////////////////////////////////////////
  263. // Function: PointerTo::Dereference operator
  264. // Access: Public
  265. // Description:
  266. ////////////////////////////////////////////////////////////////////
  267. template<class T>
  268. INLINE PointerTo<T>::To &PointerTo<T>::
  269. operator *() const {
  270. return *_ptr;
  271. }
  272. ////////////////////////////////////////////////////////////////////
  273. // Function: PointerTo::Member access operator
  274. // Access: Public
  275. // Description:
  276. ////////////////////////////////////////////////////////////////////
  277. template<class T>
  278. INLINE PointerTo<T>::To *PointerTo<T>::
  279. operator -> () const {
  280. return _ptr;
  281. }
  282. ////////////////////////////////////////////////////////////////////
  283. // Function: PointerTo::Typecast operator
  284. // Access: Public
  285. // Description: We also have the typecast operator to automatically
  286. // convert PointerTo's to the required kind of actual
  287. // pointer. This introduces ambiguities which the
  288. // compiler will resolve one way or the other, but we
  289. // don't care which way it goes because either will be
  290. // correct.
  291. ////////////////////////////////////////////////////////////////////
  292. template<class T>
  293. INLINE PointerTo<T>::
  294. operator PointerToBase<T>::To *() const {
  295. return _ptr;
  296. }
  297. ////////////////////////////////////////////////////////////////////
  298. // Function: PointerTo::p
  299. // Access: Public
  300. // Description: Returns an ordinary pointer instead of a PointerTo.
  301. // Useful to work around compiler problems, particularly
  302. // for implicit upcasts.
  303. ////////////////////////////////////////////////////////////////////
  304. template<class T>
  305. INLINE PointerTo<T>::To *PointerTo<T>::
  306. p() const {
  307. return _ptr;
  308. }
  309. ////////////////////////////////////////////////////////////////////
  310. // Function: PointerTo::Assignment operator
  311. // Access: Public
  312. // Description:
  313. ////////////////////////////////////////////////////////////////////
  314. template<class T>
  315. INLINE PointerTo<T> &PointerTo<T>::
  316. operator = (To *ptr) {
  317. reassign(ptr);
  318. return *this;
  319. }
  320. ////////////////////////////////////////////////////////////////////
  321. // Function: PointerTo::Assignment operator
  322. // Access: Public
  323. // Description:
  324. ////////////////////////////////////////////////////////////////////
  325. template<class T>
  326. INLINE PointerTo<T> &PointerTo<T>::
  327. operator = (const PointerTo<T> &copy) {
  328. reassign((const PointerToBase<T> &)copy);
  329. return *this;
  330. }
  331. ////////////////////////////////////////////////////////////////////
  332. // Function: ConstPointerTo::Constructor
  333. // Access: Public
  334. // Description:
  335. ////////////////////////////////////////////////////////////////////
  336. template<class T>
  337. INLINE ConstPointerTo<T>::
  338. ConstPointerTo(const To *ptr) :
  339. PointerToBase<T>((ConstPointerTo<T>::To *)ptr)
  340. {
  341. }
  342. ////////////////////////////////////////////////////////////////////
  343. // Function: ConstPointerTo::Copy Constructor
  344. // Access: Public
  345. // Description:
  346. ////////////////////////////////////////////////////////////////////
  347. template<class T>
  348. INLINE ConstPointerTo<T>::
  349. ConstPointerTo(const PointerTo<T> &copy) :
  350. PointerToBase<T>((const PointerToBase<T> &)copy)
  351. {
  352. }
  353. ////////////////////////////////////////////////////////////////////
  354. // Function: ConstPointerTo::Copy Constructor
  355. // Access: Public
  356. // Description:
  357. ////////////////////////////////////////////////////////////////////
  358. template<class T>
  359. INLINE ConstPointerTo<T>::
  360. ConstPointerTo(const ConstPointerTo<T> &copy) :
  361. PointerToBase<T>((const PointerToBase<T> &)copy)
  362. {
  363. }
  364. ////////////////////////////////////////////////////////////////////
  365. // Function: ConstPointerTo::Dereference operator
  366. // Access: Public
  367. // Description:
  368. ////////////////////////////////////////////////////////////////////
  369. template<class T>
  370. INLINE const ConstPointerTo<T>::To &ConstPointerTo<T>::
  371. operator *() const {
  372. return *_ptr;
  373. }
  374. ////////////////////////////////////////////////////////////////////
  375. // Function: ConstPointerTo::Member access operator
  376. // Access: Public
  377. // Description:
  378. ////////////////////////////////////////////////////////////////////
  379. template<class T>
  380. INLINE const ConstPointerTo<T>::To *ConstPointerTo<T>::
  381. operator -> () const {
  382. return _ptr;
  383. }
  384. ////////////////////////////////////////////////////////////////////
  385. // Function: ConstPointerTo::Typecast operator
  386. // Access: Public
  387. // Description: We also have the typecast operator to automatically
  388. // convert ConstPointerTo's to the required kind of actual
  389. // pointer. This introduces ambiguities which the
  390. // compiler will resolve one way or the other, but we
  391. // don't care which way it goes because either will be
  392. // correct.
  393. ////////////////////////////////////////////////////////////////////
  394. template<class T>
  395. INLINE ConstPointerTo<T>::
  396. operator const PointerToBase<T>::To *() const {
  397. return _ptr;
  398. }
  399. ////////////////////////////////////////////////////////////////////
  400. // Function: ConstPointerTo::p
  401. // Access: Public
  402. // Description: Returns an ordinary pointer instead of a ConstPointerTo.
  403. // Useful to work around compiler problems, particularly
  404. // for implicit upcasts.
  405. ////////////////////////////////////////////////////////////////////
  406. template<class T>
  407. INLINE const ConstPointerTo<T>::To *ConstPointerTo<T>::
  408. p() const {
  409. return _ptr;
  410. }
  411. ////////////////////////////////////////////////////////////////////
  412. // Function: ConstPointerTo::Assignment operator
  413. // Access: Public
  414. // Description:
  415. ////////////////////////////////////////////////////////////////////
  416. template<class T>
  417. INLINE ConstPointerTo<T> &ConstPointerTo<T>::
  418. operator = (const To *ptr) {
  419. reassign((To *)ptr);
  420. return *this;
  421. }
  422. ////////////////////////////////////////////////////////////////////
  423. // Function: ConstPointerTo::Assignment operator
  424. // Access: Public
  425. // Description:
  426. ////////////////////////////////////////////////////////////////////
  427. template<class T>
  428. INLINE ConstPointerTo<T> &ConstPointerTo<T>::
  429. operator = (const ConstPointerTo<T> &copy) {
  430. reassign((const PointerToBase<T> &)copy);
  431. return *this;
  432. }
  433. ////////////////////////////////////////////////////////////////////
  434. // Function: ConstPointerTo::Assignment operator
  435. // Access: Public
  436. // Description:
  437. ////////////////////////////////////////////////////////////////////
  438. template<class T>
  439. INLINE ConstPointerTo<T> &ConstPointerTo<T>::
  440. operator = (const PointerTo<T> &copy) {
  441. reassign((const PointerToBase<T> &)copy);
  442. return *this;
  443. }