[Scummvm-git-logs] scummvm master -> 6bdb93bcce6a6e79a7e486397cc5ae8336c33156

dreammaster noreply at scummvm.org
Tue May 3 04:04:01 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
6bdb93bcce COMMON: Add cast methods and to SharedPtr and WeakPtr, and add owner_before to WeakPtr


Commit: 6bdb93bcce6a6e79a7e486397cc5ae8336c33156
    https://github.com/scummvm/scummvm/commit/6bdb93bcce6a6e79a7e486397cc5ae8336c33156
Author: elasota (ejlasota at gmail.com)
Date: 2022-05-02T21:03:55-07:00

Commit Message:
COMMON: Add cast methods and to SharedPtr and WeakPtr, and add owner_before to WeakPtr

Changed paths:
    common/ptr.h


diff --git a/common/ptr.h b/common/ptr.h
index 45353da3518..c52667a82cc 100644
--- a/common/ptr.h
+++ b/common/ptr.h
@@ -321,7 +321,44 @@ public:
 		_tracker = new BasePtrTrackerImpl<T>(ptr);
 	}
 
+	/**
+	 * Performs the equivalent of static_cast to a new pointer type
+	 */
+	template<class T2>
+	SharedPtr<T2> staticCast() const {
+		return SharedPtr<T2>(static_cast<T2 *>(_pointer), _tracker);
+	}
+
+	/**
+	 * Performs the equivalent of dynamic_cast to a new pointer type
+	 */
+	template<class T2>
+	SharedPtr<T2> dynamicCast() const {
+		return SharedPtr<T2>(dynamic_cast<T2 *>(_pointer), _tracker);
+	}
+
+	/**
+	 * Performs the equivalent of const_cast to a new pointer type
+	 */
+	template<class T2>
+	SharedPtr<T2> constCast() const {
+		return SharedPtr<T2>(const_cast<T2 *>(_pointer), _tracker);
+	}
+
+	/**
+	 * Performs the equivalent of const_cast to a new pointer type
+	 */
+	template<class T2>
+	SharedPtr<T2> reinterpretCast() const {
+		return SharedPtr<T2>(reinterpret_cast<T2 *>(_pointer), _tracker);
+	}
+
 private:
+	SharedPtr(T *pointer, BasePtrTrackerInternal *tracker) : _pointer(pointer), _tracker(tracker) {
+		if (tracker)
+			tracker->incStrong();
+	}
+
 	T *_pointer;
 	BasePtrTrackerInternal *_tracker;
 };
@@ -367,6 +404,38 @@ public:
 			_tracker->incWeak();
 	}
 
+	/**
+	 * Performs the equivalent of static_cast to a new pointer type
+	 */
+	template<class T2>
+	WeakPtr<T2> staticCast() const {
+		return WeakPtr<T2>(expired() ? nullptr : static_cast<T2 *>(_pointer), _tracker);
+	}
+
+	/**
+	 * Performs the equivalent of dynamic_cast to a new pointer type
+	 */
+	template<class T2>
+	WeakPtr<T2> dynamicCast() const {
+		return WeakPtr<T2>(expired() ? nullptr : dynamic_cast<T2 *>(_pointer), _tracker);
+	}
+
+	/**
+	 * Performs the equivalent of const_cast to a new pointer type
+	 */
+	template<class T2>
+	WeakPtr<T2> constCast() const {
+		return WeakPtr<T2>(expired() ? nullptr : const_cast<T2 *>(_pointer), _tracker);
+	}
+
+	/**
+	 * Performs the equivalent of const_cast to a new pointer type
+	 */
+	template<class T2>
+	WeakPtr<T2> reinterpretCast() const {
+		return WeakPtr<T2>(expired() ? nullptr : reinterpret_cast<T2 *>(_pointer), _tracker);
+	}
+
 	/**
 	 * Creates a SharedPtr that manages the referenced object
 	 */
@@ -390,6 +459,22 @@ public:
 		return _tracker == nullptr || _tracker->getStrongCount() == 0;
 	}
 
+	/**
+	 * Returns whether this precedes another weak pointer in owner-based order
+	 */
+	template<class T2>
+	bool owner_before(const WeakPtr<T2>& other) const {
+		return _tracker < other._tracker;
+	}
+
+	/**
+	 * Returns whether this precedes a shared pointer in owner-based order
+	 */
+	template<class T2>
+	bool owner_before(const SharedPtr<T2> &other) const {
+		return _tracker < other._tracker;
+	}
+
 	WeakPtr<T> &operator=(const WeakPtr<T> &r) {
 		reset(r);
 		return *this;
@@ -450,6 +535,11 @@ public:
 	}
 
 private:
+	WeakPtr(T *pointer, BasePtrTrackerInternal *tracker) : _pointer(pointer), _tracker(tracker) {
+		if (tracker)
+			tracker->incWeak();
+	}
+
 	T *_pointer;
 	BasePtrTrackerInternal *_tracker;
 };




More information about the Scummvm-git-logs mailing list