From f727bc94432905d79af23cf0aef14854965da2cd Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 4 Feb 2022 09:46:55 -0500 Subject: PointerHolder: add get() and use_count() for forward compatibility PointerHolder will be replaced with shared_ptr, so let people start moving. --- include/qpdf/PointerHolder.hh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'include') diff --git a/include/qpdf/PointerHolder.hh b/include/qpdf/PointerHolder.hh index a85df8bb..bc60f7f0 100644 --- a/include/qpdf/PointerHolder.hh +++ b/include/qpdf/PointerHolder.hh @@ -22,6 +22,14 @@ #ifndef POINTERHOLDER_HH #define POINTERHOLDER_HH +// In qpdf 11, PointerHolder will be derived from std::shared_ptr and +// will also include a fix to incorrect semantics of const +// PointerHolder objects. PointerHolder only allows a const +// PointerHolder to return a const pointer. This is wrong. Use a +// PointerHolder for that. A const PointerHolder should just +// not allow you to change what it points to. This is consistent with +// how regular pointers and standard library shared pointers work. + // This class is basically std::shared_ptr but predates that by // several years. @@ -119,6 +127,12 @@ class PointerHolder return this->data->pointer < rhs.data->pointer; } + // get() is for interface compatibility with std::shared_ptr + T* get() const + { + return this->data->pointer; + } + // NOTE: The pointer returned by getPointer turns into a pumpkin // when the last PointerHolder that contains it disappears. T* getPointer() @@ -134,6 +148,12 @@ class PointerHolder return this->data->refcount; } + // use_count() is for compatibility with std::shared_ptr + long use_count() + { + return static_cast(this->data->refcount); + } + T const& operator*() const { return *this->data->pointer; -- cgit v1.2.3-54-g00ecf