pointerToArray.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Filename: pointerToArray.h
  2. // Created by: drose (14Jan99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef POINTERTOARRAY_H
  19. #define POINTERTOARRAY_H
  20. ////////////////////////////////////////////////////////////////////
  21. //
  22. // This file defines the classes PointerToArray and
  23. // ConstPointerToArray (and their abbreviations, PTA and CPTA), which
  24. // are extensions to the PointerTo class that support
  25. // reference-counted arrays.
  26. //
  27. // You may think of a PointerToArray as the same thing as a
  28. // traditional C-style array. However, it actually stores a pointer
  29. // to an STL vector, which is then reference-counted. Thus, most
  30. // vector operations may be applied directly to a PointerToArray
  31. // object, including dynamic resizing via push_back() and pop_back().
  32. //
  33. // Unlike the PointerTo class, the PointerToArray may store pointers
  34. // to any kind of object, not just those derived from ReferenceCount.
  35. //
  36. // Like PointerTo and ConstPointerTo, the macro abbreviations PTA and
  37. // CPTA are defined for convenience.
  38. //
  39. // Some examples of syntax: instead of:
  40. //
  41. // PTA(int) array(10); int *array = new int[10];
  42. // memset(array, 0, sizeof(int) * 10); memset(array, 0, sizeof(int) * 10);
  43. // array[i] = array[i+1]; array[i] = array[i+1];
  44. // num_elements = array.size(); (no equivalent)
  45. //
  46. // PTA(int) copy = array; int *copy = array;
  47. //
  48. // Note that in the above example, unlike an STL vector (but like a
  49. // C-style array), assigning a PointerToArray object from another
  50. // simply copies the pointer, and does not copy individual elements.
  51. // (Of course, reference counts are adjusted appropriately.) If you
  52. // actually wanted an element-by-element copy of the array, you would
  53. // do this:
  54. //
  55. // PTA(int) copy(0); // Create a pointer to an empty vector.
  56. // copy.v() = array.v(); // v() is the STL vector itself.
  57. //
  58. // The (0) parameter to the constructor in the above example is
  59. // crucial. When a numeric length parameter, such as zero, is given
  60. // to the constructor, it means to define a new STL vector with that
  61. // number of elements initially in it. If no parameter is given, on
  62. // the other hand, it means the PointerToArray should point to
  63. // nothing--no STL vector is created. This is equivalent to a C array
  64. // that points to NULL.
  65. //
  66. ////////////////////////////////////////////////////////////////////
  67. #include "pandabase.h"
  68. #include "referenceCount.h"
  69. #include "pointerTo.h"
  70. #include "pvector.h"
  71. #if defined(WIN32_VC) && !defined(__INTEL_COMPILER)
  72. // disable mysterious MSVC warning for static inline PTA::empty_array method
  73. // need to chk if vc 7.0 still has this problem, would like to keep it enabled
  74. #pragma warning (disable : 4506)
  75. #endif
  76. ////////////////////////////////////////////////////////////////////
  77. // Class : PointerToArray
  78. // Description : A special kind of PointerTo that stores an array of
  79. // the indicated element type, instead of a single
  80. // element. This is actually implemented as an STL
  81. // vector, using the RefCountObj class to wrap it up
  82. // with a reference count.
  83. ////////////////////////////////////////////////////////////////////
  84. template <class Element>
  85. class PointerToArray : public PointerToBase<RefCountObj<pvector<Element> > > {
  86. public:
  87. typedef TYPENAME pvector<Element>::value_type value_type;
  88. typedef TYPENAME pvector<Element>::reference reference;
  89. typedef TYPENAME pvector<Element>::const_reference const_reference;
  90. typedef TYPENAME pvector<Element>::iterator iterator;
  91. typedef TYPENAME pvector<Element>::const_iterator const_iterator;
  92. typedef TYPENAME pvector<Element>::reverse_iterator reverse_iterator;
  93. typedef TYPENAME pvector<Element>::const_reverse_iterator const_reverse_iterator;
  94. typedef TYPENAME pvector<Element>::difference_type difference_type;
  95. typedef TYPENAME pvector<Element>::size_type size_type;
  96. PUBLISHED:
  97. INLINE PointerToArray();
  98. // INLINE PointerToArray(size_type n); this is too dangerous to use, since arrays created automatically for any const parameter, use empty_array instead
  99. INLINE static PointerToArray<Element> empty_array(size_type n);
  100. INLINE PointerToArray(size_type n, const Element &value);
  101. INLINE PointerToArray(const PointerToArray<Element> &copy);
  102. public:
  103. // Duplicating the interface of vector. The following member
  104. // functions are all const, because they do not reassign the
  105. // pointer--they operate only within the vector itself, which is
  106. // non-const in this class.
  107. INLINE iterator begin() const;
  108. INLINE iterator end() const;
  109. INLINE TYPENAME PointerToArray<Element>::reverse_iterator rbegin() const;
  110. INLINE TYPENAME PointerToArray<Element>::reverse_iterator rend() const;
  111. // Equality and comparison operators are pointerwise for
  112. // PointerToArrays, not elementwise as in vector.
  113. PUBLISHED:
  114. INLINE size_type size() const;
  115. public:
  116. INLINE size_type max_size() const;
  117. INLINE bool empty() const;
  118. // Functions specific to vectors.
  119. INLINE void reserve(size_type n);
  120. INLINE size_type capacity() const;
  121. INLINE reference front() const;
  122. INLINE reference back() const;
  123. INLINE iterator insert(iterator position, const Element &x) const;
  124. INLINE void insert(iterator position, size_type n, const Element &x) const;
  125. // We don't define the insert() method that accepts a pair of
  126. // iterators to copy from. That's problematic because of the whole
  127. // member template thing. If you really need this, use
  128. // pta.v().insert(...); if you're doing this on a vector that has to
  129. // be exported from the DLL, you should use
  130. // insert_into_vector(pta.v(), ...).
  131. INLINE void erase(iterator position) const;
  132. INLINE void erase(iterator first, iterator last) const;
  133. PUBLISHED:
  134. #if !defined(WIN32_VC)
  135. INLINE reference operator [](size_type n) const;
  136. INLINE reference operator [](int n) const;
  137. #endif
  138. INLINE void push_back(const Element &x);
  139. INLINE void pop_back();
  140. INLINE void make_empty();
  141. public:
  142. INLINE operator Element *() const;
  143. INLINE Element *p() const;
  144. INLINE pvector<Element> &v() const;
  145. //These functions are only to be used in Reading through BamReader.
  146. //They are designed to work in pairs, so that you register what is
  147. //returned by get_void_ptr with BamReader and when you are setting
  148. //another PTA with what is returned by BamReader, you set it with
  149. //set_void_ptr. If you used the provided macro of READ_PTA, this is
  150. //done for you. So you should never call these functions directly
  151. INLINE void *get_void_ptr() const;
  152. INLINE void set_void_ptr(void* p);
  153. INLINE int get_ref_count() const;
  154. // Reassignment is by pointer, not memberwise as with a vector.
  155. INLINE PointerToArray<Element> &
  156. operator = (RefCountObj<pvector<Element> > *ptr);
  157. INLINE PointerToArray<Element> &
  158. operator = (const PointerToArray<Element> &copy);
  159. INLINE void clear();
  160. private:
  161. // This static empty array is kept around just so we can return
  162. // something meangful when begin() or end() is called and we have a
  163. // NULL pointer. It might not be shared properly between different
  164. // .so's, since it's a static member of a template class, but we
  165. // don't really care.
  166. static pvector<Element> _empty_array;
  167. };
  168. ////////////////////////////////////////////////////////////////////
  169. // Class : ConstPointerToArray
  170. // Description : Similar to PointerToArray, except that its contents
  171. // may not be modified.
  172. ////////////////////////////////////////////////////////////////////
  173. template <class Element>
  174. class ConstPointerToArray : public PointerToBase<RefCountObj<pvector<Element> > > {
  175. public:
  176. typedef TYPENAME pvector<Element>::value_type value_type;
  177. typedef TYPENAME pvector<Element>::const_reference reference;
  178. typedef TYPENAME pvector<Element>::const_reference const_reference;
  179. typedef TYPENAME pvector<Element>::const_iterator iterator;
  180. typedef TYPENAME pvector<Element>::const_iterator const_iterator;
  181. #ifdef WIN32_VC
  182. // VC++ seems to break the const_reverse_iterator definition somehow.
  183. typedef TYPENAME pvector<Element>::reverse_iterator reverse_iterator;
  184. #else
  185. typedef TYPENAME pvector<Element>::const_reverse_iterator reverse_iterator;
  186. #endif
  187. typedef TYPENAME pvector<Element>::const_reverse_iterator const_reverse_iterator;
  188. typedef TYPENAME pvector<Element>::difference_type difference_type;
  189. typedef TYPENAME pvector<Element>::size_type size_type;
  190. INLINE ConstPointerToArray();
  191. INLINE ConstPointerToArray(const PointerToArray<Element> &copy);
  192. INLINE ConstPointerToArray(const ConstPointerToArray<Element> &copy);
  193. // Duplicating the interface of vector.
  194. INLINE iterator begin() const;
  195. INLINE iterator end() const;
  196. INLINE TYPENAME ConstPointerToArray<Element>::reverse_iterator rbegin() const;
  197. INLINE TYPENAME ConstPointerToArray<Element>::reverse_iterator rend() const;
  198. // Equality and comparison operators are pointerwise for
  199. // PointerToArrays, not elementwise as in vector.
  200. INLINE size_type size() const;
  201. INLINE size_type max_size() const;
  202. INLINE bool empty() const;
  203. // Functions specific to vectors.
  204. INLINE size_type capacity() const;
  205. #ifndef WIN32_VC
  206. INLINE reference operator [](size_type n) const;
  207. INLINE reference operator [](int n) const;
  208. #endif
  209. INLINE reference front() const;
  210. INLINE reference back() const;
  211. INLINE operator const Element *() const;
  212. INLINE const Element *p() const;
  213. INLINE const pvector<Element> &v() const;
  214. INLINE int get_ref_count() const;
  215. // Reassignment is by pointer, not memberwise as with a vector.
  216. INLINE ConstPointerToArray<Element> &
  217. operator = (RefCountObj<pvector<Element> > *ptr);
  218. INLINE ConstPointerToArray<Element> &
  219. operator = (const PointerToArray<Element> &copy);
  220. INLINE ConstPointerToArray<Element> &
  221. operator = (const ConstPointerToArray<Element> &copy);
  222. INLINE void clear();
  223. private:
  224. // This static empty array is kept around just so we can return
  225. // something meangful when begin() or end() is called and we have a
  226. // NULL pointer. It might not be shared properly between different
  227. // .so's, since it's a static member of a template class, but we
  228. // don't really care.
  229. static pvector<Element> _empty_array;
  230. };
  231. // And the brevity macros.
  232. #define PTA(type) PointerToArray< type >
  233. #define CPTA(type) ConstPointerToArray< type >
  234. #include "pointerToArray.I"
  235. #endif