pointerToArray.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Filename: pointerToArray.h
  2. // Created by: drose (14Jan99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #ifndef POINTERTOARRAY_H
  15. #define POINTERTOARRAY_H
  16. ////////////////////////////////////////////////////////////////////
  17. //
  18. // This file defines the classes PointerToArray and
  19. // ConstPointerToArray (and their abbreviations, PTA and CPTA), which
  20. // are extensions to the PointerTo class that support
  21. // reference-counted arrays.
  22. //
  23. // You may think of a PointerToArray as the same thing as a
  24. // traditional C-style array. However, it actually stores a pointer
  25. // to an STL vector, which is then reference-counted. Thus, most
  26. // vector operations may be applied directly to a PointerToArray
  27. // object, including dynamic resizing via push_back() and pop_back().
  28. //
  29. // Unlike the PointerTo class, the PointerToArray may store pointers
  30. // to any kind of object, not just those derived from ReferenceCount.
  31. //
  32. // Like PointerTo and ConstPointerTo, the macro abbreviations PTA and
  33. // CPTA are defined for convenience.
  34. //
  35. // Some examples of syntax: instead of:
  36. //
  37. // PTA(int) array(10); int *array = new int[10];
  38. // memset(array, 0, sizeof(int) * 10); memset(array, 0, sizeof(int) * 10);
  39. // array[i] = array[i+1]; array[i] = array[i+1];
  40. // num_elements = array.size(); (no equivalent)
  41. //
  42. // PTA(int) copy = array; int *copy = array;
  43. //
  44. // Note that in the above example, unlike an STL vector (but like a
  45. // C-style array), assigning a PointerToArray object from another
  46. // simply copies the pointer, and does not copy individual elements.
  47. // (Of course, reference counts are adjusted appropriately7.) If you
  48. // actually wanted an element-by-element copy of the array, you would
  49. // do this:
  50. //
  51. // PTA(int) copy(0); // Create a pointer to an empty vector.
  52. // copy.v() = array.v(); // v() is the STL vector itself.
  53. //
  54. // The (0) parameter to the constructor in the above example is
  55. // crucial. When a numeric length parameter, such as zero, is given
  56. // to the constructor, it means to define a new STL vector with that
  57. // number of elements initially in it. If no parameter is given, on
  58. // the other hand, it means the PointerToArray should point to
  59. // nothing--no STL vector is created. This is equivalent to a C array
  60. // that points to NULL.
  61. //
  62. ////////////////////////////////////////////////////////////////////
  63. #include "pandabase.h"
  64. #include "pointerToArrayBase.h"
  65. #if defined(WIN32_VC) && !defined(__INTEL_COMPILER)
  66. // disable mysterious MSVC warning for static inline PTA::empty_array method
  67. // need to chk if vc 7.0 still has this problem, would like to keep it enabled
  68. #pragma warning (disable : 4506)
  69. #endif
  70. template <class Element>
  71. class ConstPointerToArray;
  72. ////////////////////////////////////////////////////////////////////
  73. // Class : PointerToArray
  74. // Description : A special kind of PointerTo that stores an array of
  75. // the indicated element type, instead of a single
  76. // element. This is actually implemented as an STL
  77. // vector, using the RefCountObj class to wrap it up
  78. // with a reference count.
  79. //
  80. // We actually inherit from NodeRefCountObj these days,
  81. // which adds node_ref() and node_unref() to the
  82. // standard ref() and unref(). This is particularly
  83. // useful for GeomVertexArrayData; other classes may or
  84. // may not find this additional counter useful, but
  85. // since it adds relatively little overhead (compared
  86. // with what is presumably a largish array), we go ahead
  87. // and add it here, even though it is inherited by many
  88. // different parts of the system that may not use it.
  89. ////////////////////////////////////////////////////////////////////
  90. template <class Element>
  91. class PointerToArray : public PointerToArrayBase<Element> {
  92. public:
  93. // By hiding this template from interrogate, we would improve
  94. // compile-time speed and memory utilization. However, we do want
  95. // to export a minimal subset of this class. So we define just the
  96. // exportable interface here.
  97. #ifdef CPPPARSER
  98. PUBLISHED:
  99. typedef TYPENAME pvector<Element>::size_type size_type;
  100. INLINE PointerToArray(TypeHandle type_handle = get_type_handle(Element));
  101. INLINE static PointerToArray<Element> empty_array(size_type n, TypeHandle type_handle = get_type_handle(Element));
  102. INLINE PointerToArray(const PointerToArray<Element> &copy);
  103. INLINE size_type size() const;
  104. INLINE void push_back(const Element &x);
  105. INLINE void pop_back();
  106. INLINE const Element &get_element(size_type n) const;
  107. INLINE void set_element(size_type n, const Element &value);
  108. INLINE const Element &__getitem__(size_type n) const;
  109. INLINE void __setitem__(size_type n, const Element &value);
  110. INLINE string get_data() const;
  111. INLINE void set_data(const string &data);
  112. INLINE string get_subdata(size_type n, size_type count) const;
  113. INLINE void set_subdata(size_type n, size_type count, const string &data);
  114. INLINE int get_ref_count() const;
  115. INLINE int get_node_ref_count() const;
  116. #else // CPPPARSER
  117. // This is the actual, complete interface.
  118. typedef TYPENAME PointerToArrayBase<Element>::To To;
  119. typedef TYPENAME pvector<Element>::value_type value_type;
  120. typedef TYPENAME pvector<Element>::reference reference;
  121. typedef TYPENAME pvector<Element>::const_reference const_reference;
  122. typedef TYPENAME pvector<Element>::iterator iterator;
  123. typedef TYPENAME pvector<Element>::const_iterator const_iterator;
  124. typedef TYPENAME pvector<Element>::reverse_iterator reverse_iterator;
  125. typedef TYPENAME pvector<Element>::const_reverse_iterator const_reverse_iterator;
  126. typedef TYPENAME pvector<Element>::difference_type difference_type;
  127. typedef TYPENAME pvector<Element>::size_type size_type;
  128. public:
  129. INLINE PointerToArray(TypeHandle type_handle = get_type_handle(Element));
  130. INLINE static PointerToArray<Element> empty_array(size_type n, TypeHandle type_handle = get_type_handle(Element));
  131. INLINE PointerToArray(size_type n, const Element &value, TypeHandle type_handle = get_type_handle(Element));
  132. INLINE PointerToArray(const PointerToArray<Element> &copy);
  133. public:
  134. // Duplicating the interface of vector. The following member
  135. // functions are all const, because they do not reassign the
  136. // pointer--they operate only within the vector itself, which is
  137. // non-const in this class.
  138. INLINE iterator begin() const;
  139. INLINE iterator end() const;
  140. INLINE TYPENAME PointerToArray<Element>::reverse_iterator rbegin() const;
  141. INLINE TYPENAME PointerToArray<Element>::reverse_iterator rend() const;
  142. // Equality and comparison operators are pointerwise for
  143. // PointerToArrays, not elementwise as in vector.
  144. INLINE size_type size() const;
  145. INLINE size_type max_size() const;
  146. INLINE bool empty() const;
  147. // Functions specific to vectors.
  148. INLINE void reserve(size_type n);
  149. INLINE void resize(size_type n);
  150. INLINE size_type capacity() const;
  151. INLINE reference front() const;
  152. INLINE reference back() const;
  153. INLINE iterator insert(iterator position, const Element &x);
  154. INLINE void insert(iterator position, size_type n, const Element &x);
  155. // We don't define the insert() method that accepts a pair of
  156. // iterators to copy from. That's problematic because of the whole
  157. // member template thing. If you really need this, use
  158. // pta.v().insert(...); if you're doing this on a vector that has to
  159. // be exported from the DLL, you should use
  160. // insert_into_vector(pta.v(), ...).
  161. INLINE void erase(iterator position);
  162. INLINE void erase(iterator first, iterator last);
  163. #if !defined(WIN32_VC)
  164. INLINE reference operator [](size_type n) const;
  165. INLINE reference operator [](int n) const;
  166. #endif
  167. INLINE void push_back(const Element &x);
  168. INLINE void pop_back();
  169. INLINE void make_empty();
  170. INLINE operator Element *() const;
  171. INLINE Element *p() const;
  172. INLINE pvector<Element> &v() const;
  173. INLINE ReferenceCountedVector<Element> *v0() const;
  174. // Methods to help out Python and other high-level languages.
  175. INLINE const Element &get_element(size_type n) const;
  176. INLINE void set_element(size_type n, const Element &value);
  177. INLINE const Element &__getitem__(size_type n) const;
  178. INLINE void __setitem__(size_type n, const Element &value);
  179. INLINE string get_data() const;
  180. INLINE void set_data(const string &data);
  181. INLINE string get_subdata(size_type n, size_type count) const;
  182. INLINE void set_subdata(size_type n, size_type count, const string &data);
  183. //These functions are only to be used in Reading through BamReader.
  184. //They are designed to work in pairs, so that you register what is
  185. //returned by get_void_ptr with BamReader and when you are setting
  186. //another PTA with what is returned by BamReader, you set it with
  187. //set_void_ptr. If you used the provided macro of READ_PTA, this is
  188. //done for you. So you should never call these functions directly
  189. INLINE void *get_void_ptr() const;
  190. INLINE void set_void_ptr(void* p);
  191. INLINE int get_ref_count() const;
  192. INLINE int get_node_ref_count() const;
  193. INLINE void node_ref() const;
  194. INLINE bool node_unref() const;
  195. // Reassignment is by pointer, not memberwise as with a vector.
  196. INLINE PointerToArray<Element> &
  197. operator = (ReferenceCountedVector<Element> *ptr);
  198. INLINE PointerToArray<Element> &
  199. operator = (const PointerToArray<Element> &copy);
  200. INLINE void clear();
  201. private:
  202. TypeHandle _type_handle;
  203. private:
  204. // This static empty array is kept around just so we can return
  205. // something meaningful when begin() or end() is called and we have a
  206. // NULL pointer. It might not be shared properly between different
  207. // .so's, since it's a static member of a template class, but we
  208. // don't really care.
  209. static pvector<Element> _empty_array;
  210. #endif // CPPPARSER
  211. friend class ConstPointerToArray<Element>;
  212. };
  213. ////////////////////////////////////////////////////////////////////
  214. // Class : ConstPointerToArray
  215. // Description : Similar to PointerToArray, except that its contents
  216. // may not be modified.
  217. ////////////////////////////////////////////////////////////////////
  218. template <class Element>
  219. class ConstPointerToArray : public PointerToArrayBase<Element> {
  220. public:
  221. // By hiding this template from interrogate, we would improve
  222. // compile-time speed and memory utilization. However, we do want
  223. // to export a minimal subset of this class. So we define just the
  224. // exportable interface here.
  225. #ifdef CPPPARSER
  226. PUBLISHED:
  227. INLINE ConstPointerToArray(const PointerToArray<Element> &copy);
  228. INLINE ConstPointerToArray(const ConstPointerToArray<Element> &copy);
  229. typedef TYPENAME pvector<Element>::size_type size_type;
  230. INLINE size_type size() const;
  231. INLINE const Element &get_element(size_type n) const;
  232. INLINE const Element &__getitem__(size_type n) const;
  233. INLINE string get_data() const;
  234. INLINE string get_subdata(size_type n, size_type count) const;
  235. INLINE int get_ref_count() const;
  236. INLINE int get_node_ref_count() const;
  237. #else // CPPPARSER
  238. // This is the actual, complete interface.
  239. typedef TYPENAME PointerToArrayBase<Element>::To To;
  240. typedef TYPENAME pvector<Element>::value_type value_type;
  241. typedef TYPENAME pvector<Element>::const_reference reference;
  242. typedef TYPENAME pvector<Element>::const_reference const_reference;
  243. typedef TYPENAME pvector<Element>::const_iterator iterator;
  244. typedef TYPENAME pvector<Element>::const_iterator const_iterator;
  245. #ifdef WIN32_VC
  246. // VC++ seems to break the const_reverse_iterator definition somehow.
  247. typedef TYPENAME pvector<Element>::reverse_iterator reverse_iterator;
  248. #else
  249. typedef TYPENAME pvector<Element>::const_reverse_iterator reverse_iterator;
  250. #endif
  251. typedef TYPENAME pvector<Element>::const_reverse_iterator const_reverse_iterator;
  252. typedef TYPENAME pvector<Element>::difference_type difference_type;
  253. typedef TYPENAME pvector<Element>::size_type size_type;
  254. INLINE ConstPointerToArray(TypeHandle type_handle = get_type_handle(Element));
  255. INLINE ConstPointerToArray(const PointerToArray<Element> &copy);
  256. INLINE ConstPointerToArray(const ConstPointerToArray<Element> &copy);
  257. // Duplicating the interface of vector.
  258. INLINE iterator begin() const;
  259. INLINE iterator end() const;
  260. INLINE TYPENAME ConstPointerToArray<Element>::reverse_iterator rbegin() const;
  261. INLINE TYPENAME ConstPointerToArray<Element>::reverse_iterator rend() const;
  262. // Equality and comparison operators are pointerwise for
  263. // PointerToArrays, not elementwise as in vector.
  264. INLINE size_type size() const;
  265. INLINE size_type max_size() const;
  266. INLINE bool empty() const;
  267. // Functions specific to vectors.
  268. INLINE size_type capacity() const;
  269. INLINE reference front() const;
  270. INLINE reference back() const;
  271. #ifndef WIN32_VC
  272. INLINE reference operator [](size_type n) const;
  273. INLINE reference operator [](int n) const;
  274. #endif
  275. INLINE operator const Element *() const;
  276. INLINE const Element *p() const;
  277. INLINE const pvector<Element> &v() const;
  278. INLINE const ReferenceCountedVector<Element> *v0() const;
  279. INLINE PointerToArray<Element> cast_non_const() const;
  280. // Methods to help out Python and other high-level languages.
  281. INLINE const Element &get_element(size_type n) const;
  282. INLINE const Element &__getitem__(size_type n) const;
  283. INLINE string get_data() const;
  284. INLINE string get_subdata(size_type n, size_type count) const;
  285. INLINE int get_ref_count() const;
  286. INLINE int get_node_ref_count() const;
  287. INLINE void node_ref() const;
  288. INLINE bool node_unref() const;
  289. // Reassignment is by pointer, not memberwise as with a vector.
  290. INLINE ConstPointerToArray<Element> &
  291. operator = (ReferenceCountedVector<Element> *ptr);
  292. INLINE ConstPointerToArray<Element> &
  293. operator = (const PointerToArray<Element> &copy);
  294. INLINE ConstPointerToArray<Element> &
  295. operator = (const ConstPointerToArray<Element> &copy);
  296. INLINE void clear();
  297. private:
  298. TypeHandle _type_handle;
  299. private:
  300. // This static empty array is kept around just so we can return
  301. // something meangful when begin() or end() is called and we have a
  302. // NULL pointer. It might not be shared properly between different
  303. // .so's, since it's a static member of a template class, but we
  304. // don't really care.
  305. static pvector<Element> _empty_array;
  306. #endif // CPPPARSER
  307. friend class PointerToArray<Element>;
  308. };
  309. // And the brevity macros.
  310. #define PTA(type) PointerToArray< type >
  311. #define CPTA(type) ConstPointerToArray< type >
  312. #include "pointerToArray.I"
  313. #endif