/** * PANDA 3D SOFTWARE * Copyright (c) Carnegie Mellon University. All rights reserved. * * All use of this software is subject to the terms of the revised BSD * license. You should have received a copy of this license along * with this source code in a file named "LICENSE." * * @file weakPointerTo.h * @author drose * @date 2004-09-27 */ #ifndef WEAKPOINTERTO_H #define WEAKPOINTERTO_H #include "pandabase.h" #include "weakPointerToBase.h" #include "pointerTo.h" /** * WeakPointerTo is similar to PointerTo, except that it does not actually * prevent the referenced pointer from deleting. Instead, the referenced * pointer is allowed to delete, but if this happens then was_deleted() will * return true, and it will be an assertion error to dereference the pointer * thereafter. */ template class WeakPointerTo : public WeakPointerToBase { public: typedef typename WeakPointerToBase::To To; PUBLISHED: constexpr WeakPointerTo() noexcept = default; INLINE WeakPointerTo(To *ptr); INLINE WeakPointerTo(const PointerTo ©); INLINE WeakPointerTo(const WeakPointerTo ©); public: INLINE WeakPointerTo(WeakPointerTo &&from) noexcept; template ALWAYS_INLINE WeakPointerTo(const WeakPointerTo &r) noexcept; template ALWAYS_INLINE WeakPointerTo(const PointerTo &r) noexcept; template ALWAYS_INLINE WeakPointerTo(WeakPointerTo &&r) noexcept; INLINE To &operator *() const; INLINE To *operator -> () const; // MSVC.NET 2005 insists that we use T *, and not To *, here. INLINE explicit operator T *() const; PUBLISHED: INLINE PointerTo lock() const; INLINE To *p() const; INLINE To *get_orig() const; INLINE WeakPointerTo &operator = (To *ptr); INLINE WeakPointerTo &operator = (const PointerTo ©); INLINE WeakPointerTo &operator = (const WeakPointerTo ©); public: INLINE WeakPointerTo &operator = (WeakPointerTo &&from) noexcept; template ALWAYS_INLINE WeakPointerTo &operator = (const WeakPointerTo &r) noexcept; template ALWAYS_INLINE WeakPointerTo &operator = (const PointerTo &r) noexcept; template ALWAYS_INLINE WeakPointerTo &operator = (WeakPointerTo &&r) noexcept; PUBLISHED: // This function normally wouldn't need to be redefined here, but we do so // anyway just to help out interrogate (which doesn't seem to want to // automatically export the WeakPointerToBase class). When this works again // in interrogate, we can remove this. INLINE void clear() { WeakPointerToBase::clear(); } }; /** * A WeakConstPointerTo is similar to a WeakPointerTo, except it keeps a const * pointer to the thing, that will be cleared to NULL when the thing deleted. */ template class WeakConstPointerTo : public WeakPointerToBase { public: typedef typename WeakPointerToBase::To To; PUBLISHED: constexpr WeakConstPointerTo() noexcept = default; INLINE WeakConstPointerTo(const To *ptr); INLINE WeakConstPointerTo(const PointerTo ©); INLINE WeakConstPointerTo(const ConstPointerTo ©); INLINE WeakConstPointerTo(const WeakPointerTo ©); INLINE WeakConstPointerTo(const WeakConstPointerTo ©); public: INLINE WeakConstPointerTo(WeakPointerTo &&from) noexcept; INLINE WeakConstPointerTo(WeakConstPointerTo &&from) noexcept; template ALWAYS_INLINE WeakConstPointerTo(const WeakPointerTo &r) noexcept; template ALWAYS_INLINE WeakConstPointerTo(const WeakConstPointerTo &r) noexcept; template ALWAYS_INLINE WeakConstPointerTo(const PointerTo &r) noexcept; template ALWAYS_INLINE WeakConstPointerTo(const ConstPointerTo &r) noexcept; template ALWAYS_INLINE WeakConstPointerTo(WeakPointerTo &&r) noexcept; template ALWAYS_INLINE WeakConstPointerTo(WeakConstPointerTo &&r) noexcept; INLINE const To &operator *() const; INLINE const To *operator -> () const; INLINE explicit operator const T *() const; PUBLISHED: INLINE ConstPointerTo lock() const; INLINE const To *p() const; INLINE const To *get_orig() const; INLINE WeakConstPointerTo &operator = (const To *ptr); INLINE WeakConstPointerTo &operator = (const PointerTo ©); INLINE WeakConstPointerTo &operator = (const ConstPointerTo ©); INLINE WeakConstPointerTo &operator = (const WeakPointerTo ©); INLINE WeakConstPointerTo &operator = (const WeakConstPointerTo ©); public: INLINE WeakConstPointerTo &operator = (WeakPointerTo &&from) noexcept; INLINE WeakConstPointerTo &operator = (WeakConstPointerTo &&from) noexcept; template ALWAYS_INLINE WeakConstPointerTo &operator = (const WeakPointerTo &r) noexcept; template ALWAYS_INLINE WeakConstPointerTo &operator = (const WeakConstPointerTo &r) noexcept; template ALWAYS_INLINE WeakConstPointerTo &operator = (const PointerTo &r) noexcept; template ALWAYS_INLINE WeakConstPointerTo &operator = (const ConstPointerTo &r) noexcept; template ALWAYS_INLINE WeakConstPointerTo &operator = (WeakPointerTo &&r) noexcept; template ALWAYS_INLINE WeakConstPointerTo &operator = (WeakConstPointerTo &&r) noexcept; PUBLISHED: // These functions normally wouldn't need to be redefined here, but we do so // anyway just to help out interrogate (which doesn't seem to want to // automatically export the WeakPointerToBase class). When this works again // in interrogate, we can remove these. INLINE bool is_null() const { return WeakPointerToBase::is_null(); } INLINE void clear() { WeakPointerToBase::clear(); } }; // Provide specializations of std::owner_less, for using a WPT as a map key. namespace std { template struct owner_less > { bool operator () (const WeakPointerTo &lhs, const WeakPointerTo &rhs) const noexcept { return lhs.owner_before(rhs); } }; template struct owner_less > { bool operator () (const WeakConstPointerTo &lhs, const WeakConstPointerTo &rhs) const noexcept { return lhs.owner_before(rhs); } }; } #define WPT(type) WeakPointerTo< type > #define WCPT(type) WeakConstPointerTo< type > #include "weakPointerTo.I" #endif