weakNodePath.I 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file weakNodePath.I
  10. * @author drose
  11. * @date 2004-09-29
  12. */
  13. /**
  14. *
  15. */
  16. INLINE WeakNodePath::
  17. WeakNodePath(const NodePath &node_path) :
  18. _head(node_path._head),
  19. _backup_key(0)
  20. {
  21. }
  22. /**
  23. *
  24. */
  25. INLINE WeakNodePath::
  26. WeakNodePath(const WeakNodePath &copy) :
  27. _head(copy._head),
  28. _backup_key(copy._backup_key)
  29. {
  30. }
  31. /**
  32. *
  33. */
  34. INLINE WeakNodePath::
  35. ~WeakNodePath() {
  36. }
  37. /**
  38. *
  39. */
  40. INLINE void WeakNodePath::
  41. operator = (const NodePath &node_path) {
  42. _head = node_path._head;
  43. _backup_key = 0;
  44. }
  45. /**
  46. *
  47. */
  48. INLINE void WeakNodePath::
  49. operator = (const WeakNodePath &copy) {
  50. _head = copy._head;
  51. _backup_key = copy._backup_key;
  52. }
  53. /**
  54. * Sets this NodePath to the empty NodePath. It will no longer point to any
  55. * node.
  56. */
  57. INLINE void WeakNodePath::
  58. clear() {
  59. _head.clear();
  60. _backup_key = 0;
  61. }
  62. /**
  63. * Returns true if this NodePath points to a valid, non-null node.
  64. */
  65. INLINE WeakNodePath::
  66. operator bool () const {
  67. return _head.is_valid_pointer();
  68. }
  69. /**
  70. * Returns true if the NodePath contains no nodes, or if it has been deleted.
  71. */
  72. INLINE bool WeakNodePath::
  73. is_empty() const {
  74. return _head == nullptr || _head.was_deleted();
  75. }
  76. /**
  77. * Returns true if the NodePath we were referencing has been quietly deleted
  78. * outside of the WeakNodePath.
  79. */
  80. INLINE bool WeakNodePath::
  81. was_deleted() const {
  82. return _head != nullptr && _head.was_deleted();
  83. }
  84. /**
  85. * Returns the NodePath held within this object, or an empty NodePath with the
  86. * error flag set if the object was deleted.
  87. */
  88. INLINE NodePath WeakNodePath::
  89. get_node_path() const {
  90. NodePath result;
  91. result._head = _head.lock();
  92. if (!_head.is_null() && result._head == nullptr) {
  93. result._error_type = NodePath::ET_fail;
  94. }
  95. return result;
  96. }
  97. /**
  98. * Returns the PandaNode held within this object, or nullptr if the object was
  99. * deleted.
  100. */
  101. INLINE PT(PandaNode) WeakNodePath::
  102. node() const {
  103. if (auto head = _head.lock()) {
  104. return head->get_node();
  105. } else {
  106. return nullptr;
  107. }
  108. }
  109. /**
  110. * Returns true if the two paths are equivalent; that is, if they contain the
  111. * same list of nodes in the same order.
  112. */
  113. INLINE bool WeakNodePath::
  114. operator == (const NodePath &other) const {
  115. return _head.get_orig() == other._head && !_head.was_deleted();
  116. }
  117. /**
  118. * Returns true if the two paths are not equivalent.
  119. */
  120. INLINE bool WeakNodePath::
  121. operator != (const NodePath &other) const {
  122. return !operator == (other);
  123. }
  124. /**
  125. * Returns true if this NodePath sorts before the other one, false otherwise.
  126. * The sorting order of two nonequivalent NodePaths is consistent but
  127. * undefined, and is useful only for storing NodePaths in a sorted container
  128. * like an STL set.
  129. */
  130. INLINE bool WeakNodePath::
  131. operator < (const NodePath &other) const {
  132. return _head.owner_before(other._head);
  133. }
  134. /**
  135. * Returns a number less than zero if this NodePath sorts before the other
  136. * one, greater than zero if it sorts after, or zero if they are equivalent.
  137. *
  138. * Two NodePaths are considered equivalent if they consist of exactly the same
  139. * list of nodes in the same order. Otherwise, they are different; different
  140. * NodePaths will be ranked in a consistent but undefined ordering; the
  141. * ordering is useful only for placing the NodePaths in a sorted container
  142. * like an STL set.
  143. */
  144. INLINE int WeakNodePath::
  145. compare_to(const NodePath &other) const {
  146. if (operator != (other)) {
  147. return _head.owner_before(other._head) ? -1 : 1;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * Returns true if the two paths are equivalent; that is, if they contain the
  153. * same list of nodes in the same order.
  154. */
  155. INLINE bool WeakNodePath::
  156. operator == (const WeakNodePath &other) const {
  157. return !_head.owner_before(other._head) && !other._head.owner_before(_head);
  158. }
  159. /**
  160. * Returns true if the two paths are not equivalent.
  161. */
  162. INLINE bool WeakNodePath::
  163. operator != (const WeakNodePath &other) const {
  164. return _head.owner_before(other._head) || other._head.owner_before(_head);
  165. }
  166. /**
  167. * Returns true if this WeakNodePath sorts before the other one, false
  168. * otherwise. The sorting order of two nonequivalent WeakNodePaths is
  169. * consistent but undefined, and is useful only for storing WeakNodePaths in a
  170. * sorted container like an STL set.
  171. */
  172. INLINE bool WeakNodePath::
  173. operator < (const WeakNodePath &other) const {
  174. return _head.owner_before(other._head);
  175. }
  176. /**
  177. * Returns a number less than zero if this WeakNodePath sorts before the other
  178. * one, greater than zero if it sorts after, or zero if they are equivalent.
  179. *
  180. * Two WeakNodePaths are considered equivalent if they consist of exactly the
  181. * same list of nodes in the same order. Otherwise, they are different;
  182. * different WeakNodePaths will be ranked in a consistent but undefined
  183. * ordering; the ordering is useful only for placing the WeakNodePaths in a
  184. * sorted container like an STL set.
  185. */
  186. INLINE int WeakNodePath::
  187. compare_to(const WeakNodePath &other) const {
  188. return other._head.owner_before(_head) - _head.owner_before(other._head);
  189. }
  190. /**
  191. * Returns the same values as NodePath::get_key().
  192. */
  193. INLINE int WeakNodePath::
  194. get_key() const {
  195. if (auto head = _head.lock()) {
  196. _backup_key = head->get_key();
  197. }
  198. return _backup_key;
  199. }
  200. INLINE std::ostream &operator << (std::ostream &out, const WeakNodePath &node_path) {
  201. node_path.output(out);
  202. return out;
  203. }