| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- // Filename: weakPointerToBase.I
- // Created by: drose (27Sep04)
- //
- ////////////////////////////////////////////////////////////////////
- //
- // PANDA 3D SOFTWARE
- // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
- //
- // All use of this software is subject to the terms of the Panda 3d
- // Software license. You should have received a copy of this license
- // along with this source code; you will also find a current copy of
- // the license at http://etc.cmu.edu/panda3d/docs/license/ .
- //
- // To contact the maintainers of this program write to
- // [email protected] .
- //
- ////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Constructor
- // Access: Protected
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE WeakPointerToBase<T>::
- WeakPointerToBase(To *ptr) {
- reassign(ptr);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Copy Constructor
- // Access: Protected
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE WeakPointerToBase<T>::
- WeakPointerToBase(const PointerToBase<T> ©) {
- reassign(copy);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Copy Constructor
- // Access: Protected
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE WeakPointerToBase<T>::
- WeakPointerToBase(const WeakPointerToBase<T> ©) {
- reassign(copy);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Destructor
- // Access: Protected
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE WeakPointerToBase<T>::
- ~WeakPointerToBase() {
- reassign((To *)NULL);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::reassign
- // Access: Protected
- // Description: This is the main work of the PointerTo family. When
- // the pointer is reassigned, decrement the old
- // reference count and increment the new one.
- ////////////////////////////////////////////////////////////////////
- template<class T>
- void WeakPointerToBase<T>::
- reassign(To *ptr) {
- if (ptr != (To *)_void_ptr || _ptr_was_deleted) {
- To *old_ptr = (To *)_void_ptr;
- _void_ptr = (void *)ptr;
- if (ptr != (To *)NULL) {
- ptr->weak_ref(this);
- #ifdef DO_MEMORY_USAGE
- if (MemoryUsage::get_track_memory_usage()) {
- // Make sure the MemoryUsage record knows what the TypeHandle
- // is, if we know it ourselves.
- TypeHandle type = get_type_handle(To);
- if (type == TypeHandle::none()) {
- do_init_type(To);
- type = get_type_handle(To);
- }
- if (type != TypeHandle::none()) {
- MemoryUsage::update_type(ptr, type);
- }
- }
- #endif
- }
- // Now remove the old reference.
- if (old_ptr != (To *)NULL && !_ptr_was_deleted) {
- old_ptr->weak_unref(this);
- }
- _ptr_was_deleted = false;
- }
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::reassign
- // Access: Protected
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE void WeakPointerToBase<T>::
- reassign(const PointerToBase<To> ©) {
- // This double-casting is a bit of a cheat to get around the
- // inheritance issue--it's difficult to declare a template class to
- // be a friend.
- reassign((To *)((const WeakPointerToBase<To> *)©)->_void_ptr);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::reassign
- // Access: Protected
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE void WeakPointerToBase<T>::
- reassign(const WeakPointerToBase<To> ©) {
- nassertv(!copy.was_deleted());
- reassign((To *)copy._void_ptr);
- }
- #ifndef CPPPARSER
- #ifndef WIN32_VC
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Equivalence operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator == (const To *other) const {
- return (To *)_void_ptr == other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Nonequivalence operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator != (const To *other) const {
- return (To *)_void_ptr != other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Greater-than operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator > (const To *other) const {
- return (To *)_void_ptr > other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Less-than-or-equal operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator <= (const To *other) const {
- return (To *)_void_ptr <= other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Greater-than-or-equal operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator >= (const To *other) const {
- return (To *)_void_ptr >= other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Equivalence operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator == (To *other) const {
- return (To *)_void_ptr == other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Nonequivalence operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator != (To *other) const {
- return (To *)_void_ptr != other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Greater-than operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator > (To *other) const {
- return (To *)_void_ptr > other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Less-than-or-equal operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator <= (To *other) const {
- return (To *)_void_ptr <= other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Greater-than-or-equal operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator >= (To *other) const {
- return (To *)_void_ptr >= other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Equivalence operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator == (const WeakPointerToBase<To> &other) const {
- return (To *)_void_ptr == (To *)other._void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Nonequivalence operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator != (const WeakPointerToBase<To> &other) const {
- return (To *)_void_ptr != (To *)other._void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Greater-than operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator > (const WeakPointerToBase<To> &other) const {
- return (To *)_void_ptr > (To *)other._void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Less-than-or-equal operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator <= (const WeakPointerToBase<To> &other) const {
- return (To *)_void_ptr <= (To *)other._void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Greater-than-or-equal operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator >= (const WeakPointerToBase<To> &other) const {
- return (To *)_void_ptr >= (To *)other._void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Equivalence operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator == (const PointerToBase<To> &other) const {
- return (To *)_void_ptr == (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Nonequivalence operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator != (const PointerToBase<To> &other) const {
- return (To *)_void_ptr != (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Greater-than operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator > (const PointerToBase<To> &other) const {
- return (To *)_void_ptr > (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Less-than-or-equal operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator <= (const PointerToBase<To> &other) const {
- return (To *)_void_ptr <= (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Greater-than-or-equal operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator >= (const PointerToBase<To> &other) const {
- return (To *)_void_ptr >= (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
- }
- #endif // WIN32_VC
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Less-than operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator < (const To *other) const {
- return (To *)_void_ptr < other;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Less-than operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator < (const WeakPointerToBase<To> &other) const {
- return (To *)_void_ptr < (To *)other._void_ptr;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::Less-than operator
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE bool WeakPointerToBase<T>::
- operator < (const PointerToBase<To> &other) const {
- return (To *)_void_ptr < (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
- }
- #endif // CPPPARSER
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::clear
- // Access: Published
- // Description: A convenient way to set the PointerTo object to NULL.
- // (Assignment to a NULL pointer also works, of course.)
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE void WeakPointerToBase<T>::
- clear() {
- reassign((To *)NULL);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::refresh
- // Access: Published
- // Description: Informs the WeakPointerTo object that its pointer is
- // no longer deleted. This may be used after a
- // WeakPointerTo has deleted a deleted pointer, and then
- // a new pointer has been reallocated. It's equivalent
- // to simply reassigning the pointer to its new
- // (i.e. original) value, but has the advantage that it
- // is const, so can be used for WeakPointers used as
- // keys in STL maps and sets.
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE void WeakPointerToBase<T>::
- refresh() const {
- ((WeakPointerToBase<T> *)this)->reassign((To *)_void_ptr);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: WeakPointerToBase::output
- // Access: Published
- // Description: A handy function to output PointerTo's as a hex
- // pointer followed by a reference count.
- ////////////////////////////////////////////////////////////////////
- template<class T>
- INLINE void WeakPointerToBase<T>::
- output(ostream &out) const {
- out << _void_ptr;
- if (was_deleted()) {
- out << ":deleted";
- } else if (_void_ptr != (void *)NULL) {
- out << ":" << ((To *)_void_ptr)->get_ref_count();
- }
- }
|