From 4fc85ba7173f3693a88769036f6862813c3daf3b Mon Sep 17 00:00:00 2001 From: Triston Caton Date: Sat, 4 Oct 2025 17:18:38 -0400 Subject: [PATCH 1/3] addressed some refBase.h warns --- Engine/source/core/util/refBase.h | 147 +++++++++++++++--------------- 1 file changed, 73 insertions(+), 74 deletions(-) diff --git a/Engine/source/core/util/refBase.h b/Engine/source/core/util/refBase.h index 6ef6d9775..ea058e773 100644 --- a/Engine/source/core/util/refBase.h +++ b/Engine/source/core/util/refBase.h @@ -42,21 +42,20 @@ public: { public: - WeakRefBase * get() { return mObject; } - void incRefCount() { mRefCount++; } - void decRefCount() - { + [[nodiscard]] constexpr WeakRefBase* get() const { return mObject; } + [[nodiscard]] constexpr U32 getRefCount() const { return mRefCount; } + + constexpr void incRefCount() { mRefCount++; } + constexpr void decRefCount() { AssertFatal( mRefCount > 0, "WeakReference - decrementing count of zero!" ); if (--mRefCount==0) delete this; } - U32 getRefCount() { return mRefCount; } - private: friend class WeakRefBase; - WeakReference(WeakRefBase *object) { mObject = object; mRefCount = 0; } - ~WeakReference() { AssertFatal(mObject==NULL, "Deleting weak reference which still points at an object."); } + constexpr explicit WeakReference(WeakRefBase *object) { mObject = object; mRefCount = 0; } + ~WeakReference() { AssertFatal(mObject==nullptr, "Deleting weak reference which still points at an object."); } // Object we reference WeakRefBase *mObject; @@ -66,10 +65,10 @@ public: }; public: - WeakRefBase() { mReference = NULL; } + constexpr WeakRefBase() : mReference(nullptr) {} virtual ~WeakRefBase() { clearWeakReferences(); } - WeakReference * getWeakReference(); + WeakReference* getWeakReference(); protected: void clearWeakReferences(); @@ -88,14 +87,15 @@ template< typename T > class SimObjectPtr; template class WeakRefPtr { public: - WeakRefPtr() { mReference = NULL; } - WeakRefPtr(T *ptr) { mReference = NULL; set(ptr); } - WeakRefPtr(const WeakRefPtr & ref) { mReference = NULL; set(ref.mReference); } + constexpr WeakRefPtr() : mReference(nullptr) {} + WeakRefPtr(T *ptr) : mReference(nullptr) { set(ptr); } + WeakRefPtr(const WeakRefPtr & ref) { mReference = nullptr; set(ref.mReference); } - ~WeakRefPtr() { set((WeakRefBase::WeakReference*)NULL); } + ~WeakRefPtr() { set(static_cast(nullptr)); } WeakRefPtr& operator=(const WeakRefPtr& ref) { + if (this == &ref) { return *this; } // handle self assignment ( x = x; ) set(ref.mReference); return *this; } @@ -106,24 +106,24 @@ public: } /// Returns true if the pointer is not set. - bool isNull() const { return mReference == NULL || mReference->get() == NULL; } + [[nodiscard]] constexpr bool isNull() const { return mReference == nullptr || mReference->get() == nullptr; } /// Returns true if the pointer is set. - bool isValid() const { return mReference && mReference->get(); } + [[nodiscard]] constexpr bool isValid() const { return mReference && mReference->get(); } - T* operator->() const { return getPointer(); } - T& operator*() const { return *getPointer(); } - operator T*() const { return getPointer(); } + T* operator->() const { return getPointer(); } + T& operator*() const { return *getPointer(); } + operator T*() const { return getPointer(); } /// Returns the pointer. - T* getPointer() const { return mReference ? ( T* ) mReference->get() : NULL; } + [[nodiscard]] constexpr T* getPointer() const { return mReference ? ( T* ) mReference->get() : nullptr; } protected: - void set(WeakRefBase::WeakReference * ref) + void set(WeakRefBase::WeakReference* ref) { if (mReference) mReference->decRefCount(); - mReference = NULL; + mReference = nullptr; if (ref) { mReference = ref; @@ -131,10 +131,10 @@ protected: } } - void set(T * obj) { set(obj ? obj->getWeakReference() : (WeakRefBase::WeakReference *)NULL); } + void set(T* obj) { set(obj ? obj->getWeakReference() : nullptr); } private: template< typename > friend class SimObjectPtr; - WeakRefBase::WeakReference * mReference; + WeakRefBase::WeakReference * mReference {nullptr}; }; /// Union of an arbitrary type with a WeakRefBase. The exposed type will @@ -146,14 +146,15 @@ class WeakRefUnion typedef WeakRefUnion Union; public: - WeakRefUnion() : mPtr(NULL) {} - WeakRefUnion(const WeakRefPtr & ref, ExposedType * ptr) : mPtr(NULL) { set(ref, ptr); } - WeakRefUnion(const Union & lock) : mPtr(NULL) { *this = lock; } - WeakRefUnion(WeakRefBase * ref) : mPtr(NULL) { set(ref, dynamic_cast(ref)); } - ~WeakRefUnion() { mWeakReference = NULL; } + WeakRefUnion() : mPtr(nullptr) {} + WeakRefUnion(const WeakRefPtr & ref, ExposedType * ptr) : mPtr(nullptr) { set(ref, ptr); } + WeakRefUnion(const Union & lock) : mPtr(nullptr) { *this = lock; } + WeakRefUnion(WeakRefBase * ref) : mPtr(nullptr) { set(ref, dynamic_cast(ref)); } + ~WeakRefUnion() { mWeakReference = nullptr; } Union & operator=(const Union & ptr) { + if (this == *ptr) { return *this; } set(ptr.mWeakReference, ptr.getPointer()); return *this; } @@ -164,18 +165,18 @@ public: mPtr = ptr; } - bool operator == (const ExposedType * t ) const { return getPointer() == t; } - bool operator != (const ExposedType * t ) const { return getPointer() != t; } - bool operator == (const Union &t ) const { return getPointer() == t.getPointer(); } - bool operator != (const Union &t ) const { return getPointer() != t.getPointer(); } - bool isNull() const { return mWeakReference.isNull() || !mPtr; } + [[nodiscard]] constexpr bool operator == (const ExposedType * t ) const { return getPointer() == t; } + [[nodiscard]] constexpr bool operator != (const ExposedType * t ) const { return getPointer() != t; } + [[nodiscard]] constexpr bool operator == (const Union &t ) const { return getPointer() == t.getPointer(); } + [[nodiscard]] constexpr bool operator != (const Union &t ) const { return getPointer() != t.getPointer(); } + [[nodiscard]] constexpr bool isNull() const { return mWeakReference.isNull() || !mPtr; } - ExposedType* getPointer() const { return !mWeakReference.isNull() ? mPtr : NULL; } - ExposedType* operator->() const { return getPointer(); } - ExposedType& operator*() const { return *getPointer(); } - operator ExposedType*() const { return getPointer(); } + [[nodiscard]] constexpr ExposedType* getPointer() const { return !mWeakReference.isNull() ? mPtr : nullptr; } + [[nodiscard]] constexpr ExposedType* operator->() const { return getPointer(); } + [[nodiscard]] constexpr ExposedType& operator*() const { return *getPointer(); } + [[nodiscard]] constexpr operator ExposedType*() const { return getPointer(); } - WeakRefPtr getRef() const { return mWeakReference; } + [[nodiscard]] WeakRefPtr getRef() const { return mWeakReference; } private: WeakRefPtr mWeakReference; @@ -221,11 +222,7 @@ class StrongObjectRef public: /// Constructor, assigns from the object and increments its reference count if it's not NULL - StrongObjectRef(StrongRefBase *object = NULL) : mObject( object ) - { - mObject = object; - incRef(); - } + StrongObjectRef(StrongRefBase *object = nullptr) : mObject( object ) { incRef(); } /// Destructor, dereferences the object, if there is one ~StrongObjectRef() { decRef(); } @@ -245,14 +242,14 @@ protected: StrongRefBase *mObject; ///< the object this RefObjectRef references /// increments the reference count on the referenced object - void incRef() + constexpr void incRef() const { if(mObject) mObject->incRefCount(); } /// decrements the reference count on the referenced object - void decRef() + constexpr void decRef() const { if(mObject) mObject->decRefCount(); @@ -270,7 +267,7 @@ public: StrongRefPtr() : StrongObjectRef() {} StrongRefPtr(T *ptr) : StrongObjectRef(ptr) {} StrongRefPtr(const StrongRefPtr& ref) : StrongObjectRef(ref.mObject) {} - ~StrongRefPtr() {} + ~StrongRefPtr() = default; StrongRefPtr& operator=(const StrongRefPtr& ref) { @@ -283,9 +280,9 @@ public: return *this; } - bool isNull() const { return mObject == 0; } - bool isValid() const { return mObject != 0; } - T* operator->() const { return getPointer(); } + [[nodiscard]] constexpr bool isNull() const { return mObject == nullptr; } + [[nodiscard]] constexpr bool isValid() const { return mObject != nullptr; } + [[nodiscard]] T* operator->() const { return getPointer(); } T& operator*() const { return *getPointer(); } operator T*() const { return getPointer(); } T* getPointer() const { return const_cast(static_cast(mObject)); } @@ -301,13 +298,13 @@ class StrongRefUnion typedef StrongRefUnion Union; public: - StrongRefUnion() : mPtr(NULL) {} + StrongRefUnion() : mPtr(nullptr) {} - StrongRefUnion(const StrongRefPtr & ref, ExposedType * ptr) : mPtr(NULL) { set(ref, ptr); } - StrongRefUnion(const Union & lock) : mPtr(NULL) { *this = lock; } - StrongRefUnion(StrongRefBase * ref) : mPtr(NULL) { set(ref, dynamic_cast(ref)); } + StrongRefUnion(const StrongRefPtr & ref, ExposedType * ptr) : mPtr(nullptr) { set(ref, ptr); } + StrongRefUnion(const Union & lock) : mPtr(nullptr) { *this = lock; } + StrongRefUnion(StrongRefBase * ref) : mPtr(nullptr) { set(ref, dynamic_cast(ref)); } - ~StrongRefUnion() { mReference = NULL; } + ~StrongRefUnion() { mReference = nullptr; } Union & operator=(const Union & ptr) { @@ -321,12 +318,12 @@ public: mPtr = ptr; } - bool operator == (const ExposedType * t ) const { return mPtr == t; } - bool operator != (const ExposedType * t ) const { return mPtr != t; } - bool operator == (const Union &t ) const { return mPtr == t.mPtr; } - bool operator != (const Union &t ) const { return mPtr != t.mPtr; } - bool isNull() const { return mReference.isNull() || !mPtr; } - bool isValid() const { return mReference.isValid() && mPtr; } + [[nodiscard]] constexpr bool operator == (const ExposedType * t ) const { return mPtr == t; } + [[nodiscard]] constexpr bool operator != (const ExposedType * t ) const { return mPtr != t; } + [[nodiscard]] constexpr bool operator == (const Union &t ) const { return mPtr == t.mPtr; } + [[nodiscard]] constexpr bool operator != (const Union &t ) const { return mPtr != t.mPtr; } + [[nodiscard]] constexpr bool isNull() const { return mReference.isNull() || !mPtr; } + [[nodiscard]] constexpr bool isValid() const { return mReference.isValid() && mPtr; } ExposedType* operator->() const { return mPtr; } ExposedType& operator*() const { return *mPtr; } @@ -353,8 +350,8 @@ template< class T > class StrongWeakRefPtr { public: - StrongWeakRefPtr() : mReference( NULL ) {} - StrongWeakRefPtr( T* ptr ) : mReference( NULL ) { _set( ptr ); } + constexpr StrongWeakRefPtr() : mReference( nullptr ) {} + constexpr StrongWeakRefPtr( T* ptr ) : mReference( nullptr ) { _set( ptr ); } ~StrongWeakRefPtr() { if( mReference ) @@ -367,13 +364,15 @@ public: } } - bool isNull() const { return ( _get() == NULL ); } - bool operator ==( T* ptr ) const { return ( _get() == ptr ); } - bool operator !=( T* ptr ) const { return ( _get() != ptr ); } - bool operator !() const { return isNull(); } - T* operator ->() const { return _get(); } - T& operator *() const { return *( _get() ); } - operator T*() const { return _get(); } + [[nodiscard]] constexpr bool isNull() const { return ( _get() == nullptr ); } + [[nodiscard]] constexpr bool operator ==( T* ptr ) const { return ( _get() == ptr ); } + [[nodiscard]] constexpr bool operator !=( T* ptr ) const { return ( _get() != ptr ); } + [[nodiscard]] constexpr bool operator !() const { return isNull(); } + [[nodiscard]] constexpr T* operator ->() const { return _get(); } + [[nodiscard]] constexpr T& operator *() const { return *( _get() ); } + + constexpr operator T*() const { return _get(); } // consider making this explicit + T* getPointer() const { return _get(); } StrongWeakRefPtr& operator =( T* ptr ) @@ -390,7 +389,7 @@ private: if( mReference ) return static_cast< T* >( mReference->get() ); else - return NULL; + return nullptr; } void _set( T* ptr ) { @@ -410,7 +409,7 @@ private: mReference->incRefCount(); } else - mReference = NULL; + mReference = nullptr; } }; @@ -420,9 +419,9 @@ inline void WeakRefBase::clearWeakReferences() { if (mReference) { - mReference->mObject = NULL; + mReference->mObject = nullptr; mReference->decRefCount(); - mReference = NULL; + mReference = nullptr; } } From 8d1e95b3f34d2a3c2c7defa90433c8698298e7e9 Mon Sep 17 00:00:00 2001 From: Triston Caton Date: Sat, 4 Oct 2025 19:38:30 -0400 Subject: [PATCH 2/3] resolved constexpr constructor not mem-initializing member variables --- Engine/source/core/util/refBase.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/source/core/util/refBase.h b/Engine/source/core/util/refBase.h index ea058e773..08cd8dc9a 100644 --- a/Engine/source/core/util/refBase.h +++ b/Engine/source/core/util/refBase.h @@ -54,7 +54,7 @@ public: private: friend class WeakRefBase; - constexpr explicit WeakReference(WeakRefBase *object) { mObject = object; mRefCount = 0; } + constexpr explicit WeakReference(WeakRefBase *object) :mObject(object), mRefCount(0) {} ~WeakReference() { AssertFatal(mObject==nullptr, "Deleting weak reference which still points at an object."); } // Object we reference @@ -469,4 +469,4 @@ inline T& Deref( StrongWeakRefPtr< T >& ref ) return *ref; } -#endif \ No newline at end of file +#endif From c24d0211bd27659a5e3bde7a989fbd85941f6bc3 Mon Sep 17 00:00:00 2001 From: Triston Caton Date: Sun, 5 Oct 2025 13:00:40 -0400 Subject: [PATCH 3/3] more constexprs # Conflicts: # Engine/source/core/util/refBase.h --- Engine/source/core/util/refBase.h | 37 ++++++++++++++++--------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/Engine/source/core/util/refBase.h b/Engine/source/core/util/refBase.h index 08cd8dc9a..7cd18cafb 100644 --- a/Engine/source/core/util/refBase.h +++ b/Engine/source/core/util/refBase.h @@ -43,7 +43,7 @@ public: public: [[nodiscard]] constexpr WeakRefBase* get() const { return mObject; } - [[nodiscard]] constexpr U32 getRefCount() const { return mRefCount; } + [[nodiscard]] constexpr U32 getRefCount() const { return mRefCount; } constexpr void incRefCount() { mRefCount++; } constexpr void decRefCount() { @@ -54,7 +54,8 @@ public: private: friend class WeakRefBase; - constexpr explicit WeakReference(WeakRefBase *object) :mObject(object), mRefCount(0) {} + constexpr explicit WeakReference(WeakRefBase *object) :mObject(object), mRefCount(0) {} + ~WeakReference() { AssertFatal(mObject==nullptr, "Deleting weak reference which still points at an object."); } // Object we reference @@ -66,7 +67,7 @@ public: public: constexpr WeakRefBase() : mReference(nullptr) {} - virtual ~WeakRefBase() { clearWeakReferences(); } + virtual ~WeakRefBase() { clearWeakReferences(); } WeakReference* getWeakReference(); @@ -106,17 +107,17 @@ public: } /// Returns true if the pointer is not set. - [[nodiscard]] constexpr bool isNull() const { return mReference == nullptr || mReference->get() == nullptr; } + [[nodiscard]] constexpr bool isNull() const { return mReference == nullptr || mReference->get() == nullptr; } /// Returns true if the pointer is set. - [[nodiscard]] constexpr bool isValid() const { return mReference && mReference->get(); } + [[nodiscard]] constexpr bool isValid() const { return mReference && mReference->get(); } - T* operator->() const { return getPointer(); } - T& operator*() const { return *getPointer(); } - operator T*() const { return getPointer(); } + [[nodiscard]] constexpr T* operator->() const { return getPointer(); } + [[nodiscard]] constexpr T& operator*() const { return *getPointer(); } + [[nodiscard]] constexpr operator T*() const { return getPointer(); } /// Returns the pointer. - [[nodiscard]] constexpr T* getPointer() const { return mReference ? ( T* ) mReference->get() : nullptr; } + [[nodiscard]] constexpr T* getPointer() const { return mReference ? (T*)mReference->get() : nullptr; } protected: void set(WeakRefBase::WeakReference* ref) @@ -146,10 +147,10 @@ class WeakRefUnion typedef WeakRefUnion Union; public: - WeakRefUnion() : mPtr(nullptr) {} - WeakRefUnion(const WeakRefPtr & ref, ExposedType * ptr) : mPtr(nullptr) { set(ref, ptr); } - WeakRefUnion(const Union & lock) : mPtr(nullptr) { *this = lock; } - WeakRefUnion(WeakRefBase * ref) : mPtr(nullptr) { set(ref, dynamic_cast(ref)); } + constexpr WeakRefUnion() : mPtr(nullptr) {} + constexpr WeakRefUnion(const WeakRefPtr & ref, ExposedType * ptr) : mPtr(nullptr) { set(ref, ptr); } + constexpr WeakRefUnion(const Union & lock) : mPtr(nullptr) { *this = lock; } + constexpr WeakRefUnion(WeakRefBase * ref) : mPtr(nullptr) { set(ref, dynamic_cast(ref)); } ~WeakRefUnion() { mWeakReference = nullptr; } Union & operator=(const Union & ptr) @@ -425,7 +426,7 @@ inline void WeakRefBase::clearWeakReferences() } } -inline WeakRefBase::WeakReference * WeakRefBase::getWeakReference() +inline WeakRefBase::WeakReference* WeakRefBase::getWeakReference() { if (!mReference) { @@ -454,19 +455,19 @@ struct TypeTraits< StrongWeakRefPtr< T > > : public _TypeTraits< StrongWeakRefPt }; template< typename T > -inline T& Deref( WeakRefPtr< T >& ref ) +[[nodiscard]] constexpr T& Deref( WeakRefPtr< T >& ref ) { return *ref; } template< typename T > -inline T& Deref( StrongRefPtr< T >& ref ) +[[nodiscard]] constexpr T& Deref( StrongRefPtr< T >& ref ) { return *ref; } template< typename T > -inline T& Deref( StrongWeakRefPtr< T >& ref ) +[[nodiscard]] constexpr T& Deref( StrongWeakRefPtr< T >& ref ) { return *ref; } -#endif +#endif \ No newline at end of file