Panda3D
Public Types | Public Member Functions | List of all members
PointerTo< T > Class Template Reference

This file defines the classes PointerTo and ConstPointerTo (and their abbreviations, PT and CPT). More...

#include "pointerTo.h"

Inheritance diagram for PointerTo< T >:
PointerToBase< T > PointerToVoid MemoryBase

Public Types

typedef PointerToBase< T >::To To
 
- Public Types inherited from PointerToBase< T >
typedef T To
 

Public Member Functions

constexpr PointerTo (std::nullptr_t) noexcept
 
 PointerTo (To *ptr) noexcept
 
 PointerTo (const PointerTo< T > &copy)
 
 PointerTo (PointerTo< T > &&from) noexcept
 
template<class Y >
 PointerTo (Y *ptr) noexcept
 
template<class Y >
 PointerTo (const PointerTo< Y > &r) noexcept
 
template<class Y >
 PointerTo (PointerTo< Y > &&r) noexcept
 
T *& cheat ()
 Returns a reference to the underlying pointer. More...
 
constexpr To & operator * () const noexcept
 
constexpr To * operator -> () const noexcept
 
constexpr operator T * () const noexcept
 We also have the typecast operator to automatically convert PointerTo's to the required kind of actual pointer. More...
 
PointerTo< T > & operator= (PointerTo< T > &&from) noexcept
 
template<class Y >
PointerTo< T > & operator= (const PointerTo< Y > &r) noexcept
 
template<class Y >
PointerTo< T > & operator= (PointerTo< Y > &&r) noexcept
 
PointerTo< T > & operator= (To *ptr)
 
PointerTo< T > & operator= (const PointerTo< T > &copy)
 
constexpr To * p () const noexcept
 Returns an ordinary pointer instead of a PointerTo. More...
 
- Public Member Functions inherited from PointerToBase< T >
void clear ()
 A convenient way to set the PointerTo object to NULL. More...
 
void output (std::ostream &out) const
 A handy function to output PointerTo's as a hex pointer followed by a reference count. More...
 
- Public Member Functions inherited from PointerToVoid
size_t get_hash () const
 
constexpr bool is_null () const
 Returns true if the PointerTo is a NULL pointer, false otherwise. More...
 
bool operator != (const PointerToVoid &other) const
 
bool operator< (const void *other) const
 
bool operator< (const PointerToVoid &other) const
 
bool operator== (const PointerToVoid &other) const
 
void swap (PointerToVoid &other) noexcept
 Swaps the contents of this PointerTo with the other, without touching the reference counts. More...
 

Detailed Description

template<class T>
class PointerTo< T >

This file defines the classes PointerTo and ConstPointerTo (and their abbreviations, PT and CPT).

These should be used in place of traditional C-style pointers wherever implicit reference counting is desired.

The syntax is: instead of:

PointerTo<MyClass> p; MyClass *p; PT(MyClass) p;

ConstPointerTo<MyClass> p; const MyClass *p; CPT(MyClass) p;

PointerTo and ConstPointerTo will automatically increment the object's reference count while the pointer is kept. When the PointerTo object is reassigned or goes out of scope, the reference count is automatically decremented. If the reference count reaches zero, the object is freed.

Note that const PointerTo<MyClass> is different from ConstPointerTo<MyClass>. A const PointerTo may not reassign its pointer, but it may still modify the contents at that address. On the other hand, a ConstPointerTo may reassign its pointer at will, but may not modify the contents. It is like the difference between (MyClass * const) and (const MyClass *).

In order to use PointerTo, it is necessary that the thing pointed to –MyClass in the above example–either inherits from ReferenceCount, or is a proxy built with RefCountProxy or RefCountObj (see referenceCount.h). However, also see PointerToArray, which does not have this restriction.

It is crucial that the PointerTo object is only used to refer to objects allocated from the free store, for which delete is a sensible thing to do. If you assign a PointerTo to an automatic variable (allocated from the stack, for instance), bad things will certainly happen when the reference count reaches zero and it tries to delete it.

It's also important to remember that, as always, a virtual destructor is required if you plan to support polymorphism. That is, if you define a PointerTo to some base type, and assign to it instances of a class derived from that base class, the base class must have a virtual destructor in order to properly destruct the derived object when it is deleted. PointerTo is a template class which implements a smart pointer to an object derived from ReferenceCount.

Definition at line 69 of file pointerTo.h.

Member Function Documentation

◆ cheat()

template<class T >
T *& PointerTo< T >::cheat ( )
inline

Returns a reference to the underlying pointer.

This is a very unsafe method. It's only used by some interrogate code. If you think this method might be useful to you, you're probably wrong.

Promise me you won't use this, okay?

Definition at line 150 of file pointerTo.I.

◆ operator T *()

template<class T >
constexpr PointerTo< T >::operator T * ( ) const
noexcept

We also have the typecast operator to automatically convert PointerTo's to the required kind of actual pointer.

This introduces ambiguities which the compiler will resolve one way or the other, but we don't care which way it goes because either will be correct.

Definition at line 137 of file pointerTo.I.

◆ p()

template<class T >
constexpr PointerTo< T >::To * PointerTo< T >::p ( ) const
noexcept

Returns an ordinary pointer instead of a PointerTo.

Useful to work around compiler problems, particularly for implicit upcasts.

Definition at line 160 of file pointerTo.I.


The documentation for this class was generated from the following files: