Panda3D
 All Classes Functions Variables Enumerations
pointerToArray.I
1 // Filename: pointerToArray.I
2 // Created by: drose (07Jan00)
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 
15 #ifndef CPPPARSER
16 
17 template<class Element>
19 
20 template<class Element>
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: PointerToArray::Constructor
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 template<class Element>
30 PointerToArray(TypeHandle type_handle) :
31  PointerToArrayBase<Element>((ReferenceCountedVector<Element> *)NULL),
32  _type_handle(type_handle)
33 {
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: PointerToArray::empty_array
38 // Access: Public, Static
39 // Description: Return an empty array of size n
40 ////////////////////////////////////////////////////////////////////
41 template<class Element>
44  PointerToArray<Element> temp(type_handle);
45  temp.reassign(new ReferenceCountedVector<Element>(type_handle));
46 
47  To new_array(n, type_handle);
48  ((To *)(temp._void_ptr))->swap(new_array);
49  return temp;
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: PointerToArray::Constructor
54 // Access: Public
55 // Description:
56 ////////////////////////////////////////////////////////////////////
57 template<class Element>
59 PointerToArray(size_type n, const Element &value, TypeHandle type_handle) :
60  PointerToArrayBase<Element>(new ReferenceCountedVector<Element>(type_handle)),
61  _type_handle(type_handle)
62 {
63  ((To *)(this->_void_ptr))->reserve(n);
64  insert(begin(), n, value);
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: PointerToArray::Copy Constructor
69 // Access: Public
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 template<class Element>
75  PointerToArrayBase<Element>(copy),
76  _type_handle(copy._type_handle)
77 {
78 }
79 
80 #ifdef USE_MOVE_SEMANTICS
81 ////////////////////////////////////////////////////////////////////
82 // Function: PointerToArray::Move Constructor
83 // Access: Public
84 // Description:
85 ////////////////////////////////////////////////////////////////////
86 template<class Element>
88 PointerToArray(PointerToArray<Element> &&from) NOEXCEPT :
89  PointerToArrayBase<Element>(move(from)),
90  _type_handle(from._type_handle)
91 {
92 }
93 #endif // USE_MOVE_SEMANTICS
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: PointerToArray::begin
97 // Access: Public
98 // Description:
99 ////////////////////////////////////////////////////////////////////
100 template<class Element>
101 INLINE TYPENAME PointerToArray<Element>::iterator PointerToArray<Element>::
102 begin() const {
103  if ((this->_void_ptr) == NULL) {
104  return _empty_array.begin();
105  }
106  return ((To *)(this->_void_ptr))->begin();
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: PointerToArray::end
111 // Access: Public
112 // Description:
113 ////////////////////////////////////////////////////////////////////
114 template<class Element>
115 INLINE TYPENAME PointerToArray<Element>::iterator PointerToArray<Element>::
116 end() const {
117  if ((this->_void_ptr) == NULL) {
118  return _empty_array.begin();
119  }
120  return ((To *)(this->_void_ptr))->end();
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: PointerToArray::rbegin
125 // Access: Public
126 // Description:
127 ////////////////////////////////////////////////////////////////////
128 template<class Element>
129 INLINE TYPENAME PointerToArray<Element>::reverse_iterator PointerToArray<Element>::
130 rbegin() const {
131  if ((this->_void_ptr) == NULL) {
132  return _empty_array.rbegin();
133  }
134  return ((To *)(this->_void_ptr))->rbegin();
135 }
136 
137 ////////////////////////////////////////////////////////////////////
138 // Function: PointerToArray::rend
139 // Access: Public
140 // Description:
141 ////////////////////////////////////////////////////////////////////
142 template<class Element>
143 INLINE TYPENAME PointerToArray<Element>::reverse_iterator PointerToArray<Element>::
144 rend() const {
145  if ((this->_void_ptr) == NULL) {
146  return _empty_array.rbegin();
147  }
148  return ((To *)(this->_void_ptr))->rend();
149 }
150 
151 ////////////////////////////////////////////////////////////////////
152 // Function: PointerToArray::size
153 // Access: Published
154 // Description:
155 ////////////////////////////////////////////////////////////////////
156 template<class Element>
157 INLINE TYPENAME PointerToArray<Element>::size_type PointerToArray<Element>::
158 size() const {
159  return ((this->_void_ptr) == NULL) ? 0 : ((To *)(this->_void_ptr))->size();
160 }
161 
162 ////////////////////////////////////////////////////////////////////
163 // Function: PointerToArray::max_size
164 // Access: Public
165 // Description:
166 ////////////////////////////////////////////////////////////////////
167 template<class Element>
168 INLINE TYPENAME PointerToArray<Element>::size_type PointerToArray<Element>::
169 max_size() const {
170  nassertd((this->_void_ptr) != NULL) {
171  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
172  }
173  return ((To *)(this->_void_ptr))->max_size();
174 }
175 
176 ////////////////////////////////////////////////////////////////////
177 // Function: PointerToArray::empty
178 // Access: Public
179 // Description:
180 ////////////////////////////////////////////////////////////////////
181 template<class Element>
182 INLINE bool PointerToArray<Element>::
183 empty() const {
184  return ((this->_void_ptr) == NULL) ? true : ((To *)(this->_void_ptr))->empty();
185 }
186 
187 ////////////////////////////////////////////////////////////////////
188 // Function: PointerToArray::reserve
189 // Access: Public
190 // Description:
191 ////////////////////////////////////////////////////////////////////
192 template<class Element>
193 INLINE void PointerToArray<Element>::
194 reserve(TYPENAME PointerToArray<Element>::size_type n) {
195  if ((this->_void_ptr) == NULL) {
196  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
197  }
198  ((To *)(this->_void_ptr))->reserve(n);
199 }
200 
201 ////////////////////////////////////////////////////////////////////
202 // Function: PointerToArray::resize
203 // Access: Public
204 // Description:
205 ////////////////////////////////////////////////////////////////////
206 template<class Element>
207 INLINE void PointerToArray<Element>::
208 resize(TYPENAME PointerToArray<Element>::size_type n) {
209  if ((this->_void_ptr) == NULL) {
210  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
211  }
212  ((To *)(this->_void_ptr))->resize(n);
213 }
214 
215 ////////////////////////////////////////////////////////////////////
216 // Function: PointerToArray::capacity
217 // Access: Public
218 // Description:
219 ////////////////////////////////////////////////////////////////////
220 template<class Element>
221 INLINE TYPENAME PointerToArray<Element>::size_type PointerToArray<Element>::
222 capacity() const {
223  nassertr((this->_void_ptr) != NULL, 0);
224  return ((To *)(this->_void_ptr))->capacity();
225 }
226 
227 ////////////////////////////////////////////////////////////////////
228 // Function: PointerToArray::front
229 // Access: Public
230 // Description:
231 ////////////////////////////////////////////////////////////////////
232 template<class Element>
233 INLINE TYPENAME PointerToArray<Element>::reference PointerToArray<Element>::
234 front() const {
235  nassertd((this->_void_ptr) != NULL) {
236  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
237  }
238  nassertd(!((To *)(this->_void_ptr))->empty()) {
239  ((To *)(this->_void_ptr))->push_back(Element());
240  }
241  return ((To *)(this->_void_ptr))->front();
242 }
243 
244 ////////////////////////////////////////////////////////////////////
245 // Function: PointerToArray::back
246 // Access: Public
247 // Description:
248 ////////////////////////////////////////////////////////////////////
249 template<class Element>
250 INLINE TYPENAME PointerToArray<Element>::reference PointerToArray<Element>::
251 back() const {
252  nassertd((this->_void_ptr) != NULL) {
253  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
254  }
255  nassertd(!((To *)(this->_void_ptr))->empty()) {
256  ((To *)(this->_void_ptr))->push_back(Element());
257  }
258  return ((To *)(this->_void_ptr))->back();
259 }
260 
261 ////////////////////////////////////////////////////////////////////
262 // Function: PointerToArray::insert
263 // Access: Public
264 // Description:
265 ////////////////////////////////////////////////////////////////////
266 template<class Element>
267 INLINE TYPENAME PointerToArray<Element>::iterator PointerToArray<Element>::
268 insert(iterator position, const Element &x) {
269  if ((this->_void_ptr) == NULL) {
270  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
271  position = end();
272  }
273  nassertr(position >= ((To *)(this->_void_ptr))->begin() &&
274  position <= ((To *)(this->_void_ptr))->end(), position);
275  return ((To *)(this->_void_ptr))->insert(position, x);
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function: PointerToArray::insert
280 // Access: Public
281 // Description:
282 ////////////////////////////////////////////////////////////////////
283 template<class Element>
284 INLINE void PointerToArray<Element>::
285 insert(iterator position, size_type n, const Element &x) {
286  if ((this->_void_ptr) == NULL) {
287  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
288  position = end();
289  }
290  nassertv(position >= ((To *)(this->_void_ptr))->begin() &&
291  position <= ((To *)(this->_void_ptr))->end());
292  ((To *)(this->_void_ptr))->insert(position, n, x);
293 }
294 
295 ////////////////////////////////////////////////////////////////////
296 // Function: PointerToArray::erase
297 // Access: Public
298 // Description:
299 ////////////////////////////////////////////////////////////////////
300 template<class Element>
301 INLINE void PointerToArray<Element>::
302 erase(iterator position) {
303  nassertv((this->_void_ptr) != NULL);
304  nassertv(position >= ((To *)(this->_void_ptr))->begin() &&
305  position <= ((To *)(this->_void_ptr))->end());
306  ((To *)(this->_void_ptr))->erase(position);
307 }
308 
309 ////////////////////////////////////////////////////////////////////
310 // Function: PointerToArray::erase
311 // Access: Public
312 // Description:
313 ////////////////////////////////////////////////////////////////////
314 template<class Element>
315 INLINE void PointerToArray<Element>::
316 erase(iterator first, iterator last) {
317  nassertv((this->_void_ptr) != NULL);
318  nassertv(first >= ((To *)(this->_void_ptr))->begin() && first <= ((To *)(this->_void_ptr))->end());
319  nassertv(last >= ((To *)(this->_void_ptr))->begin() && last <= ((To *)(this->_void_ptr))->end());
320  ((To *)(this->_void_ptr))->erase(first, last);
321 }
322 
323 #if !defined(WIN32_VC) && !defined(WIN64_VC)
324 ////////////////////////////////////////////////////////////////////
325 // Function: PointerToArray::Indexing operator
326 // Access: Public
327 // Description:
328 ////////////////////////////////////////////////////////////////////
329 template<class Element>
330 INLINE TYPENAME PointerToArray<Element>::reference PointerToArray<Element>::
331 operator [](size_type n) const {
332  nassertd((this->_void_ptr) != NULL) {
333  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
334  }
335  nassertd(!((To *)(this->_void_ptr))->empty()) {
336  ((To *)(this->_void_ptr))->push_back(Element());
337  }
338  nassertr(n < ((To *)(this->_void_ptr))->size(), ((To *)(this->_void_ptr))->operator[](0));
339  return ((To *)(this->_void_ptr))->operator[](n);
340 }
341 
342 ////////////////////////////////////////////////////////////////////
343 // Function: PointerToArray::Indexing operator
344 // Access: Public
345 // Description:
346 ////////////////////////////////////////////////////////////////////
347 template<class Element>
348 INLINE TYPENAME PointerToArray<Element>::reference PointerToArray<Element>::
349 operator [](int n) const {
350  return operator[]((size_type)n);
351 }
352 #endif
353 
354 ////////////////////////////////////////////////////////////////////
355 // Function: PointerToArray::push_back
356 // Access: Public
357 // Description:
358 ////////////////////////////////////////////////////////////////////
359 template<class Element>
360 INLINE void PointerToArray<Element>::
361 push_back(const Element &x) {
362  if ((this->_void_ptr) == NULL) {
363  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
364  }
365  ((To *)(this->_void_ptr))->push_back(x);
366 }
367 
368 ////////////////////////////////////////////////////////////////////
369 // Function: PointerToArray::pop_back
370 // Access: Public
371 // Description:
372 ////////////////////////////////////////////////////////////////////
373 template<class Element>
374 INLINE void PointerToArray<Element>::
375 pop_back() {
376  nassertd((this->_void_ptr) != NULL) {
377  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
378  }
379  nassertv(!((To *)(this->_void_ptr))->empty());
380  ((To *)(this->_void_ptr))->pop_back();
381 }
382 
383 ////////////////////////////////////////////////////////////////////
384 // Function: PointerToArray::make_empty
385 // Access: Public
386 // Description: Empties the array pointed to. This is different from
387 // clear(), which reassigns the pointer to a NULL
388 // pointer.
389 ////////////////////////////////////////////////////////////////////
390 template<class Element>
391 INLINE void PointerToArray<Element>::
393  nassertd((this->_void_ptr) != NULL) {
394  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
395  }
396  nassertv(!((To *)(this->_void_ptr))->empty());
397  ((To *)(this->_void_ptr))->clear();
398 }
399 
400 ////////////////////////////////////////////////////////////////////
401 // Function: PointerToArray::Typecast operator
402 // Access: Public
403 // Description: The pointer typecast operator is convenient for
404 // maintaining the fiction that we actually have a
405 // C-style array. It returns the address of the first
406 // element in the array, unless the pointer is
407 // unassigned, in which case it returns NULL.
408 ////////////////////////////////////////////////////////////////////
409 template<class Element>
411 operator Element *() const {
412  To *vec = (To *)(this->_void_ptr);
413  return ((vec == NULL)||(vec->empty())) ? (Element *)NULL : &(vec->front());
414 }
415 
416 ////////////////////////////////////////////////////////////////////
417 // Function: PointerToArray::p
418 // Access: Public
419 // Description: Function p() is similar to the function from
420 // PointerTo. It does the same thing: it returns the
421 // same thing as the typecast operator, above.
422 ////////////////////////////////////////////////////////////////////
423 template<class Element>
424 INLINE Element *PointerToArray<Element>::
425 p() const {
426  To *vec = (To *)(this->_void_ptr);
427  return ((vec == NULL)||(vec->empty())) ? (Element *)NULL : &(vec->front());
428 }
429 
430 ////////////////////////////////////////////////////////////////////
431 // Function: PointerToArray::v
432 // Access: Public
433 // Description: To access the vector itself, for more direct fiddling
434 // with some of the vector's esoteric functionality.
435 ////////////////////////////////////////////////////////////////////
436 template<class Element>
438 v() const {
439  if ((this->_void_ptr) == NULL) {
440  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
441  }
442  return *((To *)(this->_void_ptr));
443 }
444 
445 ////////////////////////////////////////////////////////////////////
446 // Function: PointerToArray::v0
447 // Access: Public
448 // Description: To access the internal ReferenceCountedVector object,
449 // for very low-level fiddling. Know what you are doing!
450 ////////////////////////////////////////////////////////////////////
451 template<class Element>
453 v0() const {
454  return (To *)(this->_void_ptr);
455 }
456 
457 ////////////////////////////////////////////////////////////////////
458 // Function: PointerToArray::get_element
459 // Access: Published
460 // Description: This method exists mainly to access the elements of
461 // the array easily from a high-level language such as
462 // Python, especially on Windows, where the above index
463 // element accessor methods can't be defined because of
464 // a confusion with the pointer typecast operator.
465 ////////////////////////////////////////////////////////////////////
466 template<class Element>
467 INLINE const Element &PointerToArray<Element>::
468 get_element(size_type n) const {
469  return (*this)[n];
470 }
471 
472 ////////////////////////////////////////////////////////////////////
473 // Function: PointerToArray::set_element
474 // Access: Published
475 // Description: This method exists mainly to access the elements of
476 // the array easily from a high-level language such as
477 // Python, especially on Windows, where the above index
478 // element accessor methods can't be defined because of
479 // a confusion with the pointer typecast operator.
480 ////////////////////////////////////////////////////////////////////
481 template<class Element>
482 INLINE void PointerToArray<Element>::
483 set_element(size_type n, const Element &value) {
484  nassertv(n < ((To *)(this->_void_ptr))->size());
485  (*this)[n] = value;
486 }
487 
488 ////////////////////////////////////////////////////////////////////
489 // Function: PointerToArray::get_data
490 // Access: Published
491 // Description: This method exists mainly to access the data of
492 // the array easily from a high-level language such as
493 // Python.
494 //
495 // It returns the entire contents of the vector as a
496 // block of raw data in a string.
497 ////////////////////////////////////////////////////////////////////
498 template<class Element>
499 INLINE string PointerToArray<Element>::
500 get_data() const {
501  return get_subdata(0, size());
502 }
503 
504 ////////////////////////////////////////////////////////////////////
505 // Function: PointerToArray::set_data
506 // Access: Published
507 // Description: This method exists mainly to access the data of
508 // the array easily from a high-level language such as
509 // Python.
510 //
511 // It replaces the entire contents of the vector from a
512 // block of raw data in a string.
513 ////////////////////////////////////////////////////////////////////
514 template<class Element>
515 INLINE void PointerToArray<Element>::
516 set_data(const string &data) {
517  set_subdata(0, size(), data);
518 }
519 
520 ////////////////////////////////////////////////////////////////////
521 // Function: PointerToArray::get_subdata
522 // Access: Published
523 // Description: This method exists mainly to access the data of
524 // the array easily from a high-level language such as
525 // Python.
526 //
527 // It returns the contents of a portion of the
528 // vector--from element (n) through element (n + count -
529 // 1)--as a block of raw data in a string.
530 ////////////////////////////////////////////////////////////////////
531 template<class Element>
532 INLINE string PointerToArray<Element>::
533 get_subdata(size_type n, size_type count) const {
534  n = min(n, size());
535  count = max(count, n);
536  count = min(count, size() - n);
537  return string((const char *)(p() + n), sizeof(Element) * count);
538 }
539 
540 ////////////////////////////////////////////////////////////////////
541 // Function: PointerToArray::set_subdata
542 // Access: Published
543 // Description: This method exists mainly to access the data of
544 // the array easily from a high-level language such as
545 // Python.
546 //
547 // It replaces the contents of a portion of the
548 // vector--from element (n) through element (n + count -
549 // 1)--as a block of raw data in a string. The length
550 // of the string must be an even multiple of Element
551 // size bytes. The array may be expanded or truncated
552 // if the length of the string does not correspond to
553 // exactly count elements.
554 ////////////////////////////////////////////////////////////////////
555 template<class Element>
556 INLINE void PointerToArray<Element>::
557 set_subdata(size_type n, size_type count, const string &data) {
558  nassertv((data.length() % sizeof(Element)) == 0);
559  nassertv(n <= size() && n + count <= size());
560  if ((this->_void_ptr) == NULL) {
561  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
562  }
563  size_type ncount = data.length() / sizeof(Element);
564  if (ncount < count) {
565  // Reduce the array.
566  erase(begin() + n + ncount, begin() + n + count);
567  } else if (count < ncount) {
568  // Expand the array.
569  insert(begin() + n + count, ncount - count, Element());
570  }
571 
572  // Now boldly replace the data. Hope there aren't any constructors
573  // or destructors involved here. The user better know what she is
574  // doing.
575  memcpy(p() + n, data.data(), sizeof(Element) * ncount);
576 }
577 
578 ////////////////////////////////////////////////////////////////////
579 // Function: PointerToArray::get(this->_void_ptr)
580 // Access: Public
581 // Description: Returns the reference to memory where the vector
582 // is stored. To be used only with set_void_ptr
583 ////////////////////////////////////////////////////////////////////
584 template<class Element>
585 INLINE void *PointerToArray<Element>::
586 get_void_ptr() const {
587  return (this->_void_ptr);
588 }
589 
590 ////////////////////////////////////////////////////////////////////
591 // Function: PointerToArray::set_void_ptr
592 // Access: Public
593 // Description: Sets this PTA to point to the pointer passed in
594 ////////////////////////////////////////////////////////////////////
595 template<class Element>
596 INLINE void PointerToArray<Element>::
597 set_void_ptr(void *p) {
598  ((PointerToArray<Element> *)this)->reassign((To *)p);
599 }
600 
601 ////////////////////////////////////////////////////////////////////
602 // Function: PointerToArray::get_ref_count
603 // Access: Public
604 // Description: Returns the reference count of the underlying vector.
605 ////////////////////////////////////////////////////////////////////
606 template<class Element>
607 INLINE int PointerToArray<Element>::
608 get_ref_count() const {
609  return ((this->_void_ptr) == NULL) ? 0 : ((To *)(this->_void_ptr))->get_ref_count();
610 }
611 
612 ////////////////////////////////////////////////////////////////////
613 // Function: PointerToArray::ref
614 // Access: Public
615 // Description: Increments the reference count of the underlying vector.
616 ////////////////////////////////////////////////////////////////////
617 template<class Element>
618 INLINE void PointerToArray<Element>::
619 ref() const {
620  if ((this->_void_ptr) == NULL) {
621  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
622  }
623  ((To *)(this->_void_ptr))->ref();
624 }
625 
626 ////////////////////////////////////////////////////////////////////
627 // Function: PointerToArray::unref
628 // Access: Public
629 // Description: Decrements the reference count of the underlying vector.
630 ////////////////////////////////////////////////////////////////////
631 template<class Element>
632 INLINE bool PointerToArray<Element>::
633 unref() const {
634  nassertr((this->_void_ptr) != NULL, true);
635  return ((To *)(this->_void_ptr))->unref();
636 }
637 
638 ////////////////////////////////////////////////////////////////////
639 // Function: PointerToArray::get_node_ref_count
640 // Access: Public
641 // Description: Returns the node_ref of the underlying vector.
642 ////////////////////////////////////////////////////////////////////
643 template<class Element>
644 INLINE int PointerToArray<Element>::
646  return ((this->_void_ptr) == NULL) ? 0 : ((To *)(this->_void_ptr))->get_node_ref_count();
647 }
648 
649 ////////////////////////////////////////////////////////////////////
650 // Function: PointerToArray::node_ref
651 // Access: Public
652 // Description: Increments the node_ref of the underlying vector.
653 ////////////////////////////////////////////////////////////////////
654 template<class Element>
655 INLINE void PointerToArray<Element>::
656 node_ref() const {
657  if ((this->_void_ptr) == NULL) {
658  ((PointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
659  }
660  ((To *)(this->_void_ptr))->node_ref();
661 }
662 
663 ////////////////////////////////////////////////////////////////////
664 // Function: PointerToArray::node_unref
665 // Access: Public
666 // Description: Decrements the node_ref of the underlying vector.
667 ////////////////////////////////////////////////////////////////////
668 template<class Element>
669 INLINE bool PointerToArray<Element>::
670 node_unref() const {
671  nassertr((this->_void_ptr) != NULL, true);
672  return ((To *)(this->_void_ptr))->node_unref();
673 }
674 
675 ////////////////////////////////////////////////////////////////////
676 // Function: PointerToArray::Assignment operator
677 // Access: Public
678 // Description:
679 ////////////////////////////////////////////////////////////////////
680 template<class Element>
683  ((PointerToArray<Element> *)this)->reassign(ptr);
684  return *this;
685 }
686 
687 ////////////////////////////////////////////////////////////////////
688 // Function: PointerToArray::Assignment operator
689 // Access: Public
690 // Description:
691 ////////////////////////////////////////////////////////////////////
692 template<class Element>
695  _type_handle = copy._type_handle;
696  ((PointerToArray<Element> *)this)->reassign(copy);
697  return *this;
698 }
699 
700 #ifdef USE_MOVE_SEMANTICS
701 ////////////////////////////////////////////////////////////////////
702 // Function: PointerToArray::Assignment operator
703 // Access: Public
704 // Description:
705 ////////////////////////////////////////////////////////////////////
706 template<class Element>
708 operator = (PointerToArray<Element> &&from) NOEXCEPT {
709  _type_handle = from._type_handle;
710  ((PointerToArray<Element> *)this)->reassign(move(from));
711  return *this;
712 }
713 #endif // USE_MOVE_SEMANTICS
714 
715 ////////////////////////////////////////////////////////////////////
716 // Function: PointerToArray::clear
717 // Access: Public
718 // Description: To empty the PTA, use the clear() method, since
719 // assignment to NULL is problematic (given the
720 // ambiguity of the pointer type of NULL).
721 ////////////////////////////////////////////////////////////////////
722 template<class Element>
723 INLINE void PointerToArray<Element>::
724 clear() {
725  ((PointerToArray<Element> *)this)->reassign((ReferenceCountedVector<Element> *)NULL);
726 }
727 
728 
729 
730 ////////////////////////////////////////////////////////////////////
731 // Function: ConstPointerToArray::Constructor
732 // Access: Public
733 // Description:
734 ////////////////////////////////////////////////////////////////////
735 template<class Element>
737 ConstPointerToArray(TypeHandle type_handle) :
738  PointerToArrayBase<Element>((ReferenceCountedVector<Element> *)NULL),
739  _type_handle(type_handle)
740 {
741 }
742 
743 ////////////////////////////////////////////////////////////////////
744 // Function: ConstPointerToArray::Copy Constructor
745 // Access: Public
746 // Description:
747 ////////////////////////////////////////////////////////////////////
748 template<class Element>
751  PointerToArrayBase<Element>(copy),
752  _type_handle(copy._type_handle)
753 {
754 }
755 
756 ////////////////////////////////////////////////////////////////////
757 // Function: ConstPointerToArray::Copy Constructor
758 // Access: Public
759 // Description:
760 ////////////////////////////////////////////////////////////////////
761 template<class Element>
764  PointerToArrayBase<Element>(copy),
765  _type_handle(copy._type_handle)
766 {
767 }
768 
769 #ifdef USE_MOVE_SEMANTICS
770 ////////////////////////////////////////////////////////////////////
771 // Function: ConstPointerToArray::Move Constructor
772 // Access: Public
773 // Description:
774 ////////////////////////////////////////////////////////////////////
775 template<class Element>
778  PointerToArrayBase<Element>(move(from)),
779  _type_handle(from._type_handle)
780 {
781 }
782 #endif // USE_MOVE_SEMANTICS
783 
784 #ifdef USE_MOVE_SEMANTICS
785 ////////////////////////////////////////////////////////////////////
786 // Function: ConstPointerToArray::Move Constructor
787 // Access: Public
788 // Description:
789 ////////////////////////////////////////////////////////////////////
790 template<class Element>
793  PointerToArrayBase<Element>(move(from)),
794  _type_handle(from._type_handle)
795 {
796 }
797 #endif // USE_MOVE_SEMANTICS
798 
799 ////////////////////////////////////////////////////////////////////
800 // Function: ConstPointerToArray::begin
801 // Access: Public
802 // Description:
803 ////////////////////////////////////////////////////////////////////
804 template<class Element>
805 INLINE TYPENAME ConstPointerToArray<Element>::iterator ConstPointerToArray<Element>::
806 begin() const {
807  if ((this->_void_ptr) == NULL) {
808  return _empty_array.begin();
809  }
810  return ((To *)(this->_void_ptr))->begin();
811 }
812 
813 ////////////////////////////////////////////////////////////////////
814 // Function: ConstPointerToArray::end
815 // Access: Public
816 // Description:
817 ////////////////////////////////////////////////////////////////////
818 template<class Element>
819 INLINE TYPENAME ConstPointerToArray<Element>::iterator ConstPointerToArray<Element>::
820 end() const {
821  if ((this->_void_ptr) == NULL) {
822  return _empty_array.begin();
823  }
824  return ((To *)(this->_void_ptr))->end();
825 }
826 
827 ////////////////////////////////////////////////////////////////////
828 // Function: ConstPointerToArray::rbegin
829 // Access: Public
830 // Description:
831 ////////////////////////////////////////////////////////////////////
832 template<class Element>
833 INLINE TYPENAME ConstPointerToArray<Element>::reverse_iterator ConstPointerToArray<Element>::
834 rbegin() const {
835  if ((this->_void_ptr) == NULL) {
836  return _empty_array.rbegin();
837  }
838  return ((To *)(this->_void_ptr))->rbegin();
839 }
840 
841 ////////////////////////////////////////////////////////////////////
842 // Function: ConstPointerToArray::rend
843 // Access: Public
844 // Description:
845 ////////////////////////////////////////////////////////////////////
846 template<class Element>
847 INLINE TYPENAME ConstPointerToArray<Element>::reverse_iterator ConstPointerToArray<Element>::
848 rend() const {
849  if ((this->_void_ptr) == NULL) {
850  return _empty_array.rbegin();
851  }
852  return ((To *)(this->_void_ptr))->rend();
853 }
854 
855 ////////////////////////////////////////////////////////////////////
856 // Function: ConstPointerToArray::size
857 // Access: Public
858 // Description:
859 ////////////////////////////////////////////////////////////////////
860 template<class Element>
861 INLINE TYPENAME ConstPointerToArray<Element>::size_type ConstPointerToArray<Element>::
862 size() const {
863  return ((this->_void_ptr) == NULL) ? 0 : ((To *)(this->_void_ptr))->size();
864 }
865 
866 ////////////////////////////////////////////////////////////////////
867 // Function: ConstPointerToArray::max_size
868 // Access: Public
869 // Description:
870 ////////////////////////////////////////////////////////////////////
871 template<class Element>
872 INLINE TYPENAME ConstPointerToArray<Element>::size_type ConstPointerToArray<Element>::
873 max_size() const {
874  nassertd((this->_void_ptr) != NULL) {
875  ((ConstPointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
876  }
877  return ((To *)(this->_void_ptr))->max_size();
878 }
879 
880 ////////////////////////////////////////////////////////////////////
881 // Function: ConstPointerToArray::empty
882 // Access: Public
883 // Description:
884 ////////////////////////////////////////////////////////////////////
885 template<class Element>
887 empty() const {
888  return ((this->_void_ptr) == NULL) ? true : ((To *)(this->_void_ptr))->empty();
889 }
890 
891 ////////////////////////////////////////////////////////////////////
892 // Function: ConstPointerToArray::capacity
893 // Access: Public
894 // Description:
895 ////////////////////////////////////////////////////////////////////
896 template<class Element>
897 INLINE TYPENAME ConstPointerToArray<Element>::size_type ConstPointerToArray<Element>::
898 capacity() const {
899  nassertd((this->_void_ptr) != NULL) {
900  ((ConstPointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
901  }
902  return ((To *)(this->_void_ptr))->capacity();
903 }
904 
905 ////////////////////////////////////////////////////////////////////
906 // Function: ConstPointerToArray::front
907 // Access: Public
908 // Description:
909 ////////////////////////////////////////////////////////////////////
910 template<class Element>
911 INLINE TYPENAME ConstPointerToArray<Element>::reference ConstPointerToArray<Element>::
912 front() const {
913  nassertd((this->_void_ptr) != NULL) {
914  ((ConstPointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
915  }
916  nassertd(!((To *)(this->_void_ptr))->empty()) {
917  ((To *)(this->_void_ptr))->push_back(Element());
918  }
919  return ((To *)(this->_void_ptr))->front();
920 }
921 
922 ////////////////////////////////////////////////////////////////////
923 // Function: ConstPointerToArray::back
924 // Access: Public
925 // Description:
926 ////////////////////////////////////////////////////////////////////
927 template<class Element>
928 INLINE TYPENAME ConstPointerToArray<Element>::reference ConstPointerToArray<Element>::
929 back() const {
930  nassertd((this->_void_ptr) != NULL) {
931  ((ConstPointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
932  }
933  nassertd(!((To *)(this->_void_ptr))->empty()) {
934  ((To *)(this->_void_ptr))->push_back(Element());
935  }
936  return ((To *)(this->_void_ptr))->back();
937 }
938 
939 #if !defined(WIN32_VC) && !defined(WIN64_VC)
940 ////////////////////////////////////////////////////////////////////
941 // Function: ConstPointerToArray::Indexing operator
942 // Access: Public
943 // Description:
944 ////////////////////////////////////////////////////////////////////
945 template<class Element>
946 INLINE TYPENAME ConstPointerToArray<Element>::reference ConstPointerToArray<Element>::
947 operator [](size_type n) const {
948  nassertd((this->_void_ptr) != NULL) {
949  ((ConstPointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
950  }
951  nassertd(!((To *)(this->_void_ptr))->empty()) {
952  ((To *)(this->_void_ptr))->push_back(Element());
953  }
954  nassertr(n < ((To *)(this->_void_ptr))->size(), ((To *)(this->_void_ptr))->operator[](0));
955  return ((To *)(this->_void_ptr))->operator[](n);
956 }
957 
958 ////////////////////////////////////////////////////////////////////
959 // Function: ConstPointerToArray::Indexing operator
960 // Access: Public
961 // Description:
962 ////////////////////////////////////////////////////////////////////
963 template<class Element>
964 INLINE TYPENAME ConstPointerToArray<Element>::reference ConstPointerToArray<Element>::
965 operator [](int n) const {
966  return operator[]((size_type)n);
967 }
968 #endif
969 
970 ////////////////////////////////////////////////////////////////////
971 // Function: ConstPointerToArray::Typecast operator
972 // Access: Public
973 // Description: The pointer typecast operator is convenient for
974 // maintaining the fiction that we actually have a
975 // C-style array. It returns the address of the first
976 // element in the array, unless the pointer is
977 // unassigned, in which case it returns NULL.
978 ////////////////////////////////////////////////////////////////////
979 template<class Element>
981 operator const Element *() const {
982  const To *vec = (const To *)(this->_void_ptr);
983  return ((vec == NULL)||(vec->empty())) ? (const Element *)NULL : &(vec->front());
984 }
985 
986 ////////////////////////////////////////////////////////////////////
987 // Function: ConstPointerToArray::p
988 // Access: Public
989 // Description: Function p() is similar to the function from
990 // ConstPointerTo. It does the same thing: it returns the
991 // same thing as the typecast operator, above.
992 ////////////////////////////////////////////////////////////////////
993 template<class Element>
994 INLINE const Element *ConstPointerToArray<Element>::
995 p() const {
996  const To *vec = (const To *)(this->_void_ptr);
997  return ((vec == NULL)||(vec->empty())) ? (const Element *)NULL : &(vec->front());
998 }
999 
1000 ////////////////////////////////////////////////////////////////////
1001 // Function: ConstPointerToArray::v
1002 // Access: Public
1003 // Description: To access the vector itself, for more direct fiddling
1004 // with some of the vector's esoteric functionality.
1005 ////////////////////////////////////////////////////////////////////
1006 template<class Element>
1008 v() const {
1009  nassertd((this->_void_ptr) != NULL) {
1010  ((ConstPointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
1011  }
1012  return *(const To *)(this->_void_ptr);
1013 }
1014 
1015 ////////////////////////////////////////////////////////////////////
1016 // Function: ConstPointerToArray::v0
1017 // Access: Public
1018 // Description: To access the internal ReferenceCountedVector object,
1019 // for very low-level fiddling. Know what you are doing!
1020 ////////////////////////////////////////////////////////////////////
1021 template<class Element>
1023 v0() const {
1024  return (const To *)(this->_void_ptr);
1025 }
1026 
1027 ////////////////////////////////////////////////////////////////////
1028 // Function: ConstPointerToArray::cast_non_const
1029 // Access: Public
1030 // Description: Casts away the constness of the CPTA(Element), and
1031 // returns an equivalent PTA(Element).
1032 ////////////////////////////////////////////////////////////////////
1033 template<class Element>
1036  PointerToArray<Element> non_const;
1037  non_const = (To *)(this->_void_ptr);
1038  return non_const;
1039 }
1040 
1041 ////////////////////////////////////////////////////////////////////
1042 // Function: ConstPointerToArray::get_element
1043 // Access: Published
1044 // Description: This method exists mainly to access the elements of
1045 // the array easily from a high-level language such as
1046 // Python, especially on Windows, where the above index
1047 // element accessor methods can't be defined because of
1048 // a confusion with the pointer typecast operator.
1049 ////////////////////////////////////////////////////////////////////
1050 template<class Element>
1051 INLINE const Element &ConstPointerToArray<Element>::
1052 get_element(size_type n) const {
1053  return (*this)[n];
1054 }
1055 
1056 ////////////////////////////////////////////////////////////////////
1057 // Function: ConstPointerToArray::get_data
1058 // Access: Published
1059 // Description: This method exists mainly to access the data of
1060 // the array easily from a high-level language such as
1061 // Python.
1062 //
1063 // It returns the entire contents of the vector as a
1064 // block of raw data in a string.
1065 ////////////////////////////////////////////////////////////////////
1066 template<class Element>
1067 INLINE string ConstPointerToArray<Element>::
1068 get_data() const {
1069  return get_subdata(0, size());
1070 }
1071 
1072 ////////////////////////////////////////////////////////////////////
1073 // Function: ConstPointerToArray::get_subdata
1074 // Access: Published
1075 // Description: This method exists mainly to access the data of
1076 // the array easily from a high-level language such as
1077 // Python.
1078 //
1079 // It returns the contents of a portion of the
1080 // vector--from element (n) through element (n + count -
1081 // 1)--as a block of raw data in a string.
1082 ////////////////////////////////////////////////////////////////////
1083 template<class Element>
1084 INLINE string ConstPointerToArray<Element>::
1085 get_subdata(size_type n, size_type count) const {
1086  n = min(n, size());
1087  count = max(count, n);
1088  count = min(count, size() - n);
1089  return string((const char *)(p() + n), sizeof(Element) * count);
1090 }
1091 
1092 ////////////////////////////////////////////////////////////////////
1093 // Function: ConstPointerToArray::get_ref_count
1094 // Access: Public
1095 // Description: Returns the reference count of the underlying vector.
1096 ////////////////////////////////////////////////////////////////////
1097 template<class Element>
1099 get_ref_count() const {
1100  return ((this->_void_ptr) == NULL) ? 0 : ((To *)(this->_void_ptr))->get_ref_count();
1101 }
1102 
1103 ////////////////////////////////////////////////////////////////////
1104 // Function: ConstPointerToArray::ref
1105 // Access: Public
1106 // Description: Increments the reference count of the underlying vector.
1107 ////////////////////////////////////////////////////////////////////
1108 template<class Element>
1110 ref() const {
1111  if ((this->_void_ptr) == NULL) {
1112  ((ConstPointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
1113  }
1114  ((To *)(this->_void_ptr))->ref();
1115 }
1116 
1117 ////////////////////////////////////////////////////////////////////
1118 // Function: ConstPointerToArray::unref
1119 // Access: Public
1120 // Description: Decrements the reference count of the underlying vector.
1121 ////////////////////////////////////////////////////////////////////
1122 template<class Element>
1124 unref() const {
1125  nassertr((this->_void_ptr) != NULL, true);
1126  return ((To *)(this->_void_ptr))->unref();
1127 }
1128 
1129 ////////////////////////////////////////////////////////////////////
1130 // Function: ConstPointerToArray::get_node_ref_count
1131 // Access: Public
1132 // Description: Returns the node_ref of the underlying vector.
1133 ////////////////////////////////////////////////////////////////////
1134 template<class Element>
1137  return ((this->_void_ptr) == NULL) ? 0 : ((To *)(this->_void_ptr))->get_node_ref_count();
1138 }
1139 
1140 ////////////////////////////////////////////////////////////////////
1141 // Function: ConstPointerToArray::node_ref
1142 // Access: Public
1143 // Description: Increments the node_ref of the underlying vector.
1144 ////////////////////////////////////////////////////////////////////
1145 template<class Element>
1147 node_ref() const {
1148  if ((this->_void_ptr) == NULL) {
1149  ((ConstPointerToArray<Element> *)this)->reassign(new ReferenceCountedVector<Element>(_type_handle));
1150  }
1151  ((To *)(this->_void_ptr))->node_ref();
1152 }
1153 
1154 ////////////////////////////////////////////////////////////////////
1155 // Function: ConstPointerToArray::node_unref
1156 // Access: Public
1157 // Description: Decrements the node_ref of the underlying vector.
1158 ////////////////////////////////////////////////////////////////////
1159 template<class Element>
1161 node_unref() const {
1162  nassertr((this->_void_ptr) != NULL, true);
1163  return ((To *)(this->_void_ptr))->node_unref();
1164 }
1165 
1166 ////////////////////////////////////////////////////////////////////
1167 // Function: ConstPointerToArray::Assignment operator
1168 // Access: Public
1169 // Description:
1170 ////////////////////////////////////////////////////////////////////
1171 template<class Element>
1174  ((ConstPointerToArray<Element> *)this)->reassign(ptr);
1175  return *this;
1176 }
1177 
1178 ////////////////////////////////////////////////////////////////////
1179 // Function: ConstPointerToArray::Assignment operator
1180 // Access: Public
1181 // Description:
1182 ////////////////////////////////////////////////////////////////////
1183 template<class Element>
1185 operator = (const PointerToArray<Element> &copy) {
1186  _type_handle = copy._type_handle;
1187  ((ConstPointerToArray<Element> *)this)->reassign(copy);
1188  return *this;
1189 }
1190 
1191 ////////////////////////////////////////////////////////////////////
1192 // Function: ConstPointerToArray::Assignment operator
1193 // Access: Public
1194 // Description:
1195 ////////////////////////////////////////////////////////////////////
1196 template<class Element>
1199  _type_handle = copy._type_handle;
1200  ((ConstPointerToArray<Element> *)this)->reassign(copy);
1201  return *this;
1202 }
1203 
1204 #ifdef USE_MOVE_SEMANTICS
1205 ////////////////////////////////////////////////////////////////////
1206 // Function: ConstPointerToArray::Assignment operator
1207 // Access: Public
1208 // Description:
1209 ////////////////////////////////////////////////////////////////////
1210 template<class Element>
1212 operator = (PointerToArray<Element> &&from) NOEXCEPT {
1213  _type_handle = from._type_handle;
1214  ((ConstPointerToArray<Element> *)this)->reassign(move(from));
1215  return *this;
1216 }
1217 #endif // USE_MOVE_SEMANTICS
1218 
1219 #ifdef USE_MOVE_SEMANTICS
1220 ////////////////////////////////////////////////////////////////////
1221 // Function: ConstPointerToArray::Assignment operator
1222 // Access: Public
1223 // Description:
1224 ////////////////////////////////////////////////////////////////////
1225 template<class Element>
1227 operator = (ConstPointerToArray<Element> &&from) NOEXCEPT {
1228  _type_handle = from._type_handle;
1229  ((ConstPointerToArray<Element> *)this)->reassign(move(from));
1230  return *this;
1231 }
1232 #endif // USE_MOVE_SEMANTICS
1233 
1234 ////////////////////////////////////////////////////////////////////
1235 // Function: ConstPointerToArray::clear
1236 // Access: Public
1237 // Description: To empty the PTA, use the clear() method, since
1238 // assignment to NULL is problematic (given the
1239 // ambiguity of the pointer type of NULL).
1240 ////////////////////////////////////////////////////////////////////
1241 template<class Element>
1245 }
1246 
1247 #endif // CPPPARSER
int get_node_ref_count() const
Returns 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&#39;s esoteric functionalit...
void * get_void_ptr() const
Returns the reference to memory where the vector is stored.
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...
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 ...
int get_ref_count() const
Returns the reference count of the underlying vector.
bool node_unref() const
Decrements the node_ref of the underlying vector.
void node_ref() const
Increments the node_ref of the underlying vector.
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 ...
A special kind of PointerTo that stores an array of the indicated element type, instead of a single e...
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...
bool unref() const
Decrements the reference count of the underlying vector.
void ref() const
Increments 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.
int get_ref_count() const
Returns the reference count of the underlying vector.
void make_empty()
Empties the array pointed to.
void set_void_ptr(void *p)
Sets this PTA to point to the pointer passed in.
void set_data(const string &data)
This method exists mainly to access the data of the array easily from a high-level language such as P...
void node_ref() const
Increments the node_ref of the underlying vector.
pvector< Element > & v() const
To access the vector itself, for more direct fiddling with some of the vector&#39;s esoteric functionalit...
This is the base class for PointerToArray and ConstPointerToArray.
void set_subdata(size_type n, size_type count, const string &data)
This method exists mainly to access the data of the array easily from a high-level language such as P...
void clear()
To empty the PTA, use the clear() method, since assignment to NULL is problematic (given the ambiguit...
const ReferenceCountedVector< Element > * v0() const
To access the internal ReferenceCountedVector object, for very low-level fiddling.
PointerToArray< Element > cast_non_const() const
Casts away the constness of the CPTA(Element), and returns an equivalent PTA(Element).
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...
string get_data() const
This method exists mainly to access the data of the array easily from a high-level language such as P...
void ref() const
Increments the reference count of the underlying vector.
bool node_unref() const
Decrements the node_ref of the underlying vector.
ReferenceCountedVector< Element > * v0() const
To access the internal ReferenceCountedVector object, for very low-level fiddling.
void swap(PointerToVoid &other) NOEXCEPT
Swaps the contents of this PointerTo with the other, without touching the reference counts...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
string get_data() const
This method exists mainly to access the data of the array easily from a high-level language such as P...
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 ...
This defines the object that is actually stored and reference-counted internally by a PointerToArray...
Element * p() const
Function p() is similar to the function from PointerTo.
bool unref() const
Decrements the reference count of the underlying vector.
Similar to PointerToArray, except that its contents may not be modified.