Panda3D
Loading...
Searching...
No Matches
pointerToArray.h
Go to the documentation of this file.
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 pointerToArray.h
10 * @author drose
11 * @date 1999-01-14
12 */
13
14#ifndef POINTERTOARRAY_H
15#define POINTERTOARRAY_H
16
17/*
18 * This file defines the classes PointerToArray and ConstPointerToArray (and
19 * their abbreviations, PTA and CPTA), which are extensions to the PointerTo
20 * class that support reference-counted arrays.
21 *
22 * You may think of a PointerToArray as the same thing as a traditional
23 * C-style array. However, it actually stores a pointer to an STL vector,
24 * which is then reference-counted. Thus, most vector operations may be
25 * applied directly to a PointerToArray object, including dynamic resizing via
26 * push_back() and pop_back().
27 *
28 * Unlike the PointerTo class, the PointerToArray may store pointers to any
29 * kind of object, not just those derived from ReferenceCount.
30 *
31 * Like PointerTo and ConstPointerTo, the macro abbreviations PTA and CPTA are
32 * defined for convenience.
33 *
34 * Some examples of syntax: instead of:
35 *
36 * PTA(int) array(10); int *array = new int[10];
37 * memset(array, 0, sizeof(int) * 10); memset(array, 0, sizeof(int) * 10);
38 * array[i] = array[i+1]; array[i] = array[i+1];
39 * num_elements = array.size(); (no equivalent)
40 *
41 * PTA(int) copy = array; int *copy = array;
42 *
43 * Note that in the above example, unlike an STL vector (but like a C-style
44 * array), assigning a PointerToArray object from another simply copies the
45 * pointer, and does not copy individual elements. (Of course, reference
46 * counts are adjusted appropriately.) If you actually wanted an
47 * element-by-element copy of the array, you would do this:
48 *
49 * PTA(int) copy(0); // Create a pointer to an empty vector.
50 * copy.v() = array.v(); // v() is the STL vector itself.
51 *
52 * The (0) parameter to the constructor in the above example is crucial. When
53 * a numeric length parameter, such as zero, is given to the constructor, it
54 * means to define a new STL vector with that number of elements initially in
55 * it. If no parameter is given, on the other hand, it means the
56 * PointerToArray should point to nothing--no STL vector is created. This is
57 * equivalent to a C array that points to NULL.
58 */
59
60#include "pandabase.h"
61
62#include "pointerToArrayBase.h"
63
64#if (defined(WIN32_VC) || defined(WIN64_VC)) && !defined(__INTEL_COMPILER)
65// disable mysterious MSVC warning for static inline PTA::empty_array method
66// need to chk if vc 7.0 still has this problem, would like to keep it enabled
67#pragma warning (disable : 4506)
68#endif
69
70template <class Element>
72
73/**
74 * A special kind of PointerTo that stores an array of the indicated element
75 * type, instead of a single element. This is actually implemented as an STL
76 * vector, using the RefCountObj class to wrap it up with a reference count.
77 *
78 * We actually inherit from NodeRefCountObj these days, which adds node_ref()
79 * and node_unref() to the standard ref() and unref(). This is particularly
80 * useful for GeomVertexArrayData; other classes may or may not find this
81 * additional counter useful, but since it adds relatively little overhead
82 * (compared with what is presumably a largish array), we go ahead and add it
83 * here, even though it is inherited by many different parts of the system
84 * that may not use it.
85 */
86template <class Element>
87class PointerToArray : public PointerToArrayBase<Element> {
88public:
89 // By hiding this template from interrogate, we would improve compile-time
90 // speed and memory utilization. However, we do want to export a minimal
91 // subset of this class. So we define just the exportable interface here.
92#ifdef CPPPARSER
93PUBLISHED:
94 typedef typename pvector<Element>::size_type size_type;
95 INLINE PointerToArray(TypeHandle type_handle = get_type_handle(Element));
96 INLINE static PointerToArray<Element> empty_array(size_type n, TypeHandle type_handle = get_type_handle(Element));
97 INLINE PointerToArray(const PointerToArray<Element> &copy);
98
99 EXTENSION(PointerToArray(PyObject *self, PyObject *source));
100
101 INLINE void clear();
102
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 EXTENSION(const Element &__getitem__(size_type n) const);
109 EXTENSION(void __setitem__(size_type n, const Element &value));
110 EXTENSION(PyObject *get_data() const);
111 EXTENSION(void set_data(PyObject *data));
112 EXTENSION(PyObject *get_subdata(size_type n, size_type count) const);
113 INLINE void set_subdata(size_type n, size_type count, const std::string &data);
114 INLINE int get_ref_count() const;
115 INLINE int get_node_ref_count() const;
116
117 INLINE size_t count(const Element &) const;
118
119#ifdef HAVE_PYTHON
120 EXTENSION(PyObject *__reduce__(PyObject *self) const);
121
122 EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags));
123 EXTENSION(void __releasebuffer__(PyObject *self, Py_buffer *view) const);
124
125 EXTENSION(PointerToArray<Element> __deepcopy__(PyObject *memo) const);
126#endif
127
128#else // CPPPARSER
129 // This is the actual, complete interface.
130 typedef typename PointerToArrayBase<Element>::To To;
131 typedef typename pvector<Element>::value_type value_type;
132 typedef typename pvector<Element>::reference reference;
133 typedef typename pvector<Element>::const_reference const_reference;
134 typedef typename pvector<Element>::iterator iterator;
135 typedef typename pvector<Element>::const_iterator const_iterator;
136 typedef typename pvector<Element>::reverse_iterator reverse_iterator;
137 typedef typename pvector<Element>::const_reverse_iterator const_reverse_iterator;
138 typedef typename pvector<Element>::difference_type difference_type;
139 typedef typename pvector<Element>::size_type size_type;
140
141public:
142 INLINE PointerToArray(TypeHandle type_handle = get_type_handle(Element));
143 INLINE static PointerToArray<Element> empty_array(size_type n, TypeHandle type_handle = get_type_handle(Element));
144 INLINE PointerToArray(size_type n, const Element &value, TypeHandle type_handle = get_type_handle(Element));
145 INLINE PointerToArray(const Element *begin, const Element *end, TypeHandle type_handle = get_type_handle(Element));
146 INLINE PointerToArray(const PointerToArray<Element> &copy);
147 INLINE PointerToArray(PointerToArray<Element> &&from) noexcept;
148 INLINE explicit PointerToArray(pvector<Element> &&from, TypeHandle type_handle = get_type_handle(Element));
149
150public:
151 // Duplicating the interface of vector. The following member functions are
152 // all const, because they do not reassign the pointer--they operate only
153 // within the vector itself, which is non-const in this class.
154
155 INLINE iterator begin() const;
156 INLINE iterator end() const;
157 INLINE typename PointerToArray<Element>::reverse_iterator rbegin() const;
158 INLINE typename PointerToArray<Element>::reverse_iterator rend() const;
159
160 // Equality and comparison operators are pointerwise for PointerToArrays,
161 // not elementwise as in vector.
162 INLINE size_type size() const;
163 INLINE size_type max_size() const;
164 INLINE bool empty() const;
165
166 INLINE void clear();
167
168 // Functions specific to vectors.
169 INLINE void reserve(size_type n);
170 INLINE void resize(size_type n);
171 INLINE size_type capacity() const;
172 INLINE reference front() const;
173 INLINE reference back() const;
174 INLINE iterator insert(iterator position, const Element &x);
175 INLINE void insert(iterator position, size_type n, const Element &x);
176
177 // We don't define the insert() method that accepts a pair of iterators to
178 // copy from. That's problematic because of the whole member template
179 // thing. If you really need this, use pta.v().insert(...); if you're doing
180 // this on a vector that has to be exported from the DLL, you should use
181 // insert_into_vector(pta.v(), ...).
182
183 INLINE void erase(iterator position);
184 INLINE void erase(iterator first, iterator last);
185
186#if !defined(WIN32_VC) && !defined (WIN64_VC)
187 INLINE reference operator [](size_type n) const;
188 INLINE reference operator [](int n) const;
189#endif
190
191 INLINE void push_back(const Element &x);
192 INLINE void pop_back();
193 INLINE void make_empty();
194
195 INLINE operator Element *() const;
196 INLINE Element *p() const;
197 INLINE pvector<Element> &v() const;
198 INLINE ReferenceCountedVector<Element> *v0() const;
199
200 // Methods to help out Python and other high-level languages.
201 INLINE const Element &get_element(size_type n) const;
202 INLINE void set_element(size_type n, const Element &value);
203 INLINE std::string get_data() const;
204 INLINE void set_data(const std::string &data);
205 INLINE std::string get_subdata(size_type n, size_type count) const;
206 INLINE void set_subdata(size_type n, size_type count, const std::string &data);
207
208 // These functions are only to be used in Reading through BamReader. They
209 // are designed to work in pairs, so that you register what is returned by
210 // get_void_ptr with BamReader and when you are setting another PTA with
211 // what is returned by BamReader, you set it with set_void_ptr. If you used
212 // the provided macro of READ_PTA, this is done for you. So you should
213 // never call these functions directly
214 INLINE void *get_void_ptr() const;
215 INLINE void set_void_ptr(void* p);
216
217 INLINE int get_ref_count() const;
218 INLINE void ref() const;
219 INLINE bool unref() const;
220
221 INLINE int get_node_ref_count() const;
222 INLINE void node_ref() const;
223 INLINE bool node_unref() const;
224
225 INLINE size_t count(const Element &) const;
226
227#endif // CPPPARSER
228
229public:
230 // Reassignment is by pointer, not memberwise as with a vector.
232 operator = (ReferenceCountedVector<Element> *ptr);
234 operator = (const PointerToArray<Element> &copy);
236 operator = (PointerToArray<Element> &&from) noexcept;
237
238private:
239 TypeHandle _type_handle;
240
241 // This static empty array is kept around just so we can return something
242 // meaningful when begin() or end() is called and we have a NULL pointer.
243 // It might not be shared properly between different .so's, since it's a
244 // static member of a template class, but we don't really care.
245 static pvector<Element> _empty_array;
246
247 friend class ConstPointerToArray<Element>;
248};
249
250/**
251 * Similar to PointerToArray, except that its contents may not be modified.
252 */
253template <class Element>
255public:
256 INLINE ConstPointerToArray(TypeHandle type_handle = get_type_handle(Element));
257
258 // By hiding this template from interrogate, we would improve compile-time
259 // speed and memory utilization. However, we do want to export a minimal
260 // subset of this class. So we define just the exportable interface here.
261#ifdef CPPPARSER
262PUBLISHED:
265
266 INLINE void clear();
267
268 typedef typename pvector<Element>::size_type size_type;
269 INLINE size_type size() const;
270 INLINE const Element &get_element(size_type n) const;
271 EXTENSION(const Element &__getitem__(size_type n) const);
272 EXTENSION(PyObject *get_data() const);
273 EXTENSION(PyObject *get_subdata(size_type n, size_type count) const);
274 INLINE int get_ref_count() const;
275 INLINE int get_node_ref_count() const;
276
277 INLINE size_t count(const Element &) const;
278
279#ifdef HAVE_PYTHON
280 EXTENSION(PyObject *__reduce__(PyObject *self) const);
281
282 EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags) const);
283 EXTENSION(void __releasebuffer__(PyObject *self, Py_buffer *view) const);
284
285 EXTENSION(ConstPointerToArray<Element> __deepcopy__(PyObject *memo) const);
286#endif
287
288#else // CPPPARSER
289 // This is the actual, complete interface.
290 typedef typename PointerToArrayBase<Element>::To To;
291 typedef typename pvector<Element>::value_type value_type;
292 typedef typename pvector<Element>::const_reference reference;
293 typedef typename pvector<Element>::const_reference const_reference;
294 typedef typename pvector<Element>::const_iterator iterator;
295 typedef typename pvector<Element>::const_iterator const_iterator;
296#if defined(WIN32_VC) || defined(WIN64_VC)
297 // VC++ seems to break the const_reverse_iterator definition somehow.
298 typedef typename pvector<Element>::reverse_iterator reverse_iterator;
299#else
300 typedef typename pvector<Element>::const_reverse_iterator reverse_iterator;
301#endif
302 typedef typename pvector<Element>::const_reverse_iterator const_reverse_iterator;
303 typedef typename pvector<Element>::difference_type difference_type;
304 typedef typename pvector<Element>::size_type size_type;
305
306 INLINE ConstPointerToArray(const Element *begin, const Element *end, TypeHandle type_handle = get_type_handle(Element));
309 INLINE ConstPointerToArray(PointerToArray<Element> &&from) noexcept;
311 INLINE explicit ConstPointerToArray(pvector<Element> &&from, TypeHandle type_handle = get_type_handle(Element));
312
313 // Duplicating the interface of vector.
314
315 INLINE iterator begin() const;
316 INLINE iterator end() const;
317 INLINE typename ConstPointerToArray<Element>::reverse_iterator rbegin() const;
318 INLINE typename ConstPointerToArray<Element>::reverse_iterator rend() const;
319
320 // Equality and comparison operators are pointerwise for PointerToArrays,
321 // not elementwise as in vector.
322
323 INLINE size_type size() const;
324 INLINE size_type max_size() const;
325 INLINE bool empty() const;
326
327 INLINE void clear();
328
329 // Functions specific to vectors.
330 INLINE size_type capacity() const;
331 INLINE reference front() const;
332 INLINE reference back() const;
333
334#if !defined(WIN32_VC) && !defined(WIN64_VC)
335 INLINE reference operator [](size_type n) const;
336 INLINE reference operator [](int n) const;
337#endif
338
339 INLINE operator const Element *() const;
340 INLINE const Element *p() const;
341 INLINE const pvector<Element> &v() const;
342 INLINE const ReferenceCountedVector<Element> *v0() const;
344
345 // Methods to help out Python and other high-level languages.
346 INLINE const Element &get_element(size_type n) const;
347 INLINE std::string get_data() const;
348 INLINE std::string get_subdata(size_type n, size_type count) const;
349
350 INLINE int get_ref_count() const;
351 INLINE void ref() const;
352 INLINE bool unref() const;
353
354 INLINE int get_node_ref_count() const;
355 INLINE void node_ref() const;
356 INLINE bool node_unref() const;
357
358 INLINE size_t count(const Element &) const;
359
360#endif // CPPPARSER
361
362public:
363 // Reassignment is by pointer, not memberwise as with a vector.
365 operator = (ReferenceCountedVector<Element> *ptr);
367 operator = (const PointerToArray<Element> &copy);
369 operator = (const ConstPointerToArray<Element> &copy);
371 operator = (PointerToArray<Element> &&from) noexcept;
373 operator = (ConstPointerToArray<Element> &&from) noexcept;
374
375private:
376 TypeHandle _type_handle;
377
378 // This static empty array is kept around just so we can return something
379 // meangful when begin() or end() is called and we have a NULL pointer. It
380 // might not be shared properly between different .so's, since it's a static
381 // member of a template class, but we don't really care.
382 static pvector<Element> _empty_array;
383
384 friend class PointerToArray<Element>;
385};
386
387// And the brevity macros.
388#define PTA(type) PointerToArray< type >
389#define CPTA(type) ConstPointerToArray< type >
390
391#include "pointerToArray.I"
392
393#endif // HAVE_POINTERTOARRAY_H
Similar to PointerToArray, except that its contents may not be modified.
int get_ref_count() const
Returns the reference count of the underlying vector.
std::string get_data() const
This method exists mainly to access the data of the array easily from a high-level language such as P...
bool node_unref() const
Decrements the node_ref of the underlying vector.
const Element * p() const
Function p() is similar to the function from ConstPointerTo.
void clear()
To empty the PTA, use the clear() method, since assignment to NULL is problematic (given the ambiguit...
std::string get_subdata(size_type n, size_type count) const
This method exists mainly to access the data of the array easily from a high-level language such as P...
const ReferenceCountedVector< Element > * v0() const
To access the internal ReferenceCountedVector object, for very low-level fiddling.
size_t count(const Element &) const
Counts the frequency at which the given element occurs in the vector.
void node_ref() const
Increments the node_ref of the underlying vector.
int get_node_ref_count() const
Returns the node_ref of the underlying vector.
const pvector< Element > & v() const
To access the vector itself, for more direct fiddling with some of the vector's esoteric functionalit...
bool unref() const
Decrements the reference count of the underlying vector.
const Element & get_element(size_type n) const
This method exists mainly to access the elements of the array easily from a high-level language such ...
PointerToArray< Element > cast_non_const() const
Casts away the constness of the CPTA(Element), and returns an equivalent PTA(Element).
void ref() const
Increments the reference count of the underlying vector.
This is the base class for PointerToArray and ConstPointerToArray.
A special kind of PointerTo that stores an array of the indicated element type, instead of a single e...
void set_data(const std::string &data)
This method exists mainly to access the data of the array easily from a high-level language such as P...
size_t count(const Element &) const
Counts the frequency at which the given element occurs in the vector.
pvector< Element > & v() const
To access the vector itself, for more direct fiddling with some of the vector's esoteric functionalit...
void set_void_ptr(void *p)
Sets this PTA to point to the pointer passed in.
std::string get_subdata(size_type n, size_type count) const
This method exists mainly to access the data of the array easily from a high-level language such as P...
int get_ref_count() const
Returns the reference count of the underlying vector.
static PointerToArray< Element > empty_array(size_type n, TypeHandle type_handle=get_type_handle(Element))
Return an empty array of size n.
void set_subdata(size_type n, size_type count, const std::string &data)
This method exists mainly to access the data of the array easily from a high-level language such as P...
ReferenceCountedVector< Element > * v0() const
To access the internal ReferenceCountedVector object, for very low-level fiddling.
int get_node_ref_count() const
Returns the node_ref of the underlying vector.
const Element & get_element(size_type n) const
This method exists mainly to access the elements of the array easily from a high-level language such ...
void clear()
To empty the PTA, use the clear() method, since assignment to NULL is problematic (given the ambiguit...
void set_element(size_type n, const Element &value)
This method exists mainly to access the elements of the array easily from a high-level language such ...
void * get_void_ptr() const
Returns the reference to memory where the vector is stored.
void ref() const
Increments the reference count of the underlying vector.
void make_empty()
Empties the array pointed to.
bool unref() const
Decrements the reference count of the underlying vector.
Element * p() const
Function p() is similar to the function from PointerTo.
bool node_unref() const
Decrements the node_ref of the underlying vector.
void node_ref() const
Increments the node_ref of the underlying vector.
std::string get_data() const
This method exists mainly to access the data of the array easily from a high-level language such as P...
This defines the object that is actually stored and reference-counted internally by a PointerToArray.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
This is our own Panda specialization on the default STL vector.
Definition pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.