nodeAttributes.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Filename: nodeAttributes.cxx
  2. // Created by: drose (20Mar00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "nodeAttributes.h"
  6. #include "nodeTransitionCache.h"
  7. #include "config_graph.h"
  8. #include "setTransitionHelpers.h"
  9. ////////////////////////////////////////////////////////////////////
  10. // Function: NodeAttributes::Constructor
  11. // Access: Public
  12. // Description:
  13. ////////////////////////////////////////////////////////////////////
  14. NodeAttributes::
  15. NodeAttributes() {
  16. }
  17. ////////////////////////////////////////////////////////////////////
  18. // Function: NodeAttributes::Copy Constructor
  19. // Access: Public
  20. // Description:
  21. ////////////////////////////////////////////////////////////////////
  22. NodeAttributes::
  23. NodeAttributes(const NodeAttributes &copy) :
  24. _attributes(copy._attributes)
  25. {
  26. }
  27. ////////////////////////////////////////////////////////////////////
  28. // Function: NodeAttributes::Copy Assignment Operator
  29. // Access: Public
  30. // Description:
  31. ////////////////////////////////////////////////////////////////////
  32. void NodeAttributes::
  33. operator = (const NodeAttributes &copy) {
  34. _attributes = copy._attributes;
  35. }
  36. ////////////////////////////////////////////////////////////////////
  37. // Function: NodeAttributes::Destructor
  38. // Access: Public
  39. // Description:
  40. ////////////////////////////////////////////////////////////////////
  41. NodeAttributes::
  42. ~NodeAttributes() {
  43. }
  44. ////////////////////////////////////////////////////////////////////
  45. // Function: NodeAttributes::is_empty
  46. // Access: Public
  47. // Description: Returns true if there are no Attributes stored in
  48. // the set, or false if there are any (even initial)
  49. // Attributes.
  50. ////////////////////////////////////////////////////////////////////
  51. bool NodeAttributes::
  52. is_empty() const {
  53. return _attributes.empty();
  54. }
  55. ////////////////////////////////////////////////////////////////////
  56. // Function: NodeAttributes::set_attribute
  57. // Access: Public
  58. // Description: Stores the indicated attribute pointer in the
  59. // NodeAttributes set, according to the indicated
  60. // TypeHandle. Node that attributes are stored
  61. // according to the TypeHandle of the associated
  62. // *transition*, not of the attributes' own TypeHandle.
  63. // Thus, there cannot be a flavor of set_attribute()
  64. // that automatically infers the correct TypeHandle
  65. // based on the attribute type.
  66. //
  67. // The NodeAttribute may be NULL indicating that the
  68. // attribute should be cleared. If the NodeAttribute is
  69. // not NULL, it must match the type indicated by the
  70. // TypeHandle.
  71. //
  72. // The return value is a pointer to the *previous*
  73. // attribute in the set, if any, or NULL if there was
  74. // none.
  75. ////////////////////////////////////////////////////////////////////
  76. PT(NodeAttribute) NodeAttributes::
  77. set_attribute(TypeHandle handle, NodeAttribute *attrib) {
  78. if (attrib == (NodeAttribute *)NULL) {
  79. return clear_attribute(handle);
  80. } else {
  81. Attributes::iterator ti;
  82. ti = _attributes.find(handle);
  83. if (ti != _attributes.end()) {
  84. PT(NodeAttribute) result = (*ti).second;
  85. (*ti).second = attrib;
  86. return result;
  87. }
  88. _attributes.insert(Attributes::value_type(handle, attrib));
  89. return NULL;
  90. }
  91. }
  92. ////////////////////////////////////////////////////////////////////
  93. // Function: NodeAttributes::clear_attribute
  94. // Access: Public
  95. // Description: Removes any attribute associated with the indicated
  96. // handle from the set.
  97. //
  98. // The return value is a pointer to the previous
  99. // attribute in the set, if any, or NULL if there was
  100. // none.
  101. ////////////////////////////////////////////////////////////////////
  102. PT(NodeAttribute) NodeAttributes::
  103. clear_attribute(TypeHandle handle) {
  104. nassertr(handle != TypeHandle::none(), NULL);
  105. Attributes::iterator ti;
  106. ti = _attributes.find(handle);
  107. if (ti != _attributes.end()) {
  108. PT(NodeAttribute) result = (*ti).second;
  109. _attributes.erase(ti);
  110. return result;
  111. }
  112. return NULL;
  113. }
  114. ////////////////////////////////////////////////////////////////////
  115. // Function: NodeAttributes::has_attribute
  116. // Access: Public
  117. // Description: Returns true if ab attribute associated with the
  118. // indicated handle has been stored in the set (even if
  119. // it is the initial attribute), or false otherwise.
  120. ////////////////////////////////////////////////////////////////////
  121. bool NodeAttributes::
  122. has_attribute(TypeHandle handle) const {
  123. nassertr(handle != TypeHandle::none(), false);
  124. return _attributes.count(handle) != 0;
  125. }
  126. ////////////////////////////////////////////////////////////////////
  127. // Function: NodeAttributes::get_attribute
  128. // Access: Public
  129. // Description: Returns the attribute associated with the indicated
  130. // handle, or NULL if no such attribute has been stored
  131. // in the set.
  132. ////////////////////////////////////////////////////////////////////
  133. NodeAttribute *NodeAttributes::
  134. get_attribute(TypeHandle handle) const {
  135. nassertr(handle != TypeHandle::none(), NULL);
  136. Attributes::const_iterator ai;
  137. ai = _attributes.find(handle);
  138. if (ai != _attributes.end()) {
  139. return (*ai).second;
  140. }
  141. return NULL;
  142. }
  143. ////////////////////////////////////////////////////////////////////
  144. // Function: NodeAttributes::clear
  145. // Access: Public
  146. // Description:
  147. ////////////////////////////////////////////////////////////////////
  148. void NodeAttributes::
  149. clear() {
  150. _attributes.clear();
  151. }
  152. ////////////////////////////////////////////////////////////////////
  153. // Function: NodeAttributes::is_initial
  154. // Access: Public
  155. // Description:
  156. ////////////////////////////////////////////////////////////////////
  157. bool NodeAttributes::
  158. is_initial() const {
  159. Attributes::const_iterator ai;
  160. for (ai = _attributes.begin(); ai != _attributes.end(); ++ai) {
  161. if ((*ai).second != (NodeAttribute *)NULL) {
  162. // && !(*ai).second->is_initial()
  163. return false;
  164. }
  165. }
  166. return true;
  167. }
  168. ////////////////////////////////////////////////////////////////////
  169. // Function: NodeAttributes::compare_to
  170. // Access: Public
  171. // Description:
  172. ////////////////////////////////////////////////////////////////////
  173. int NodeAttributes::
  174. compare_to(const NodeAttributes &other) const {
  175. return tmap_compare_attr(_attributes.begin(), _attributes.end(),
  176. other._attributes.begin(), other._attributes.end());
  177. }
  178. ////////////////////////////////////////////////////////////////////
  179. // Function: NodeAttributes::apply_from
  180. // Access: Public
  181. // Description: Modifies the current NodeAttributes object to reflect
  182. // the application of the indicated NodeAttributes and
  183. // the indicated transitions. The "other"
  184. // NodeAttributes object may be the same as this.
  185. ////////////////////////////////////////////////////////////////////
  186. void NodeAttributes::
  187. apply_from(const NodeAttributes &other, const NodeTransitionCache &trans) {
  188. Attributes temp;
  189. tmap_apply(other._attributes.begin(), other._attributes.end(),
  190. trans._cache.begin(), trans._cache.end(),
  191. inserter(temp, temp.begin()));
  192. _attributes.swap(temp);
  193. }
  194. ////////////////////////////////////////////////////////////////////
  195. // Function: NodeAttributes::output
  196. // Access: Public
  197. // Description:
  198. ////////////////////////////////////////////////////////////////////
  199. void NodeAttributes::
  200. output(ostream &out) const {
  201. bool written_any = false;
  202. Attributes::const_iterator ai;
  203. for (ai = _attributes.begin(); ai != _attributes.end(); ++ai) {
  204. if ((*ai).second != (NodeAttribute *)NULL) {
  205. if (written_any) {
  206. out << " ";
  207. }
  208. out << *(*ai).second;
  209. written_any = true;
  210. }
  211. }
  212. }
  213. ////////////////////////////////////////////////////////////////////
  214. // Function: NodeAttributes::write
  215. // Access: Public
  216. // Description:
  217. ////////////////////////////////////////////////////////////////////
  218. void NodeAttributes::
  219. write(ostream &out, int indent_level) const {
  220. Attributes::const_iterator ai;
  221. for (ai = _attributes.begin(); ai != _attributes.end(); ++ai) {
  222. if ((*ai).second != (NodeAttribute *)NULL) {
  223. (*ai).second->write(out, indent_level);
  224. }
  225. }
  226. }