diff --git a/applications/test/tmp/Test-tmp.C b/applications/test/tmp/Test-tmp.C index 73ba236063422591b9a99bfc26e6fe8c6a740d47..80691b3393ee1adebd3508b58f55c21271060f1a 100644 --- a/applications/test/tmp/Test-tmp.C +++ b/applications/test/tmp/Test-tmp.C @@ -56,16 +56,18 @@ int main() } { - tmp<scalarField> tfld1 = tmp<scalarField>::New(20, Zero); + auto tfld1 = tmp<scalarField>::New(20, Zero); Info<< "tmp refCount = " << tfld1->count() << nl; if (tfld1.valid()) { Info<<"tmp: " << tfld1() << nl; } - } - { + Info<<"tmp addr: " << long(tfld1.get()) << nl; + + // Hold on to the old content for a bit + tmp<scalarField> tfld2 = tmp<scalarField>::NewFrom<myScalarField>(20, Zero); @@ -74,6 +76,12 @@ int main() { Info<<"tmp: " << tfld2() << nl; } + + Info<<"tmp addr: " << long(tfld2.get()) << nl; + + tfld2.clear(); + + Info<<"after clear: " << long(tfld2.get()) << nl; } Info<< "\nEnd" << endl; diff --git a/src/OpenFOAM/memory/refCount/refCount.H b/src/OpenFOAM/memory/refCount/refCount.H index ba4a362a921d0a5f115f88370cf172bfa3b6203d..a5e1c97924e5b1dd28d1f159dcce2f3545236902 100644 --- a/src/OpenFOAM/memory/refCount/refCount.H +++ b/src/OpenFOAM/memory/refCount/refCount.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -84,25 +84,25 @@ public: // Member Operators //- Increment the reference count - void operator++() + void operator++() noexcept { ++count_; } //- Increment the reference count - void operator++(int) + void operator++(int) noexcept { ++count_; } //- Decrement the reference count - void operator--() + void operator--() noexcept { --count_; } //- Decrement the reference count - void operator--(int) + void operator--(int) noexcept { --count_; } diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index b315ed34efc8c1e3cfa020e011e51565eeccca1a..00a26a8a81128d7979aac904a9e3f3c0a2d31b32 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -157,17 +157,17 @@ public: // Query //- True if this is a managed pointer (not a const reference) - inline bool isTmp() const; + inline bool isTmp() const noexcept; //- True if this is a non-null managed pointer - inline bool empty() const; + inline bool empty() const noexcept; //- True if this is a non-null managed pointer, //- or is a const object reference - inline bool valid() const; + inline bool valid() const noexcept; //- True if this is a non-null managed pointer with a unique ref-count - inline bool movable() const; + inline bool movable() const noexcept; //- Return type-name of the tmp, constructed from type-name of T inline word typeName() const; @@ -175,6 +175,12 @@ public: // Access + //- Return pointer without nullptr checking. + inline T* get() noexcept; + + //- Return const pointer without nullptr checking. + inline const T* get() const noexcept; + //- Return the const object reference or a const reference to the //- contents of a non-null managed pointer. // Fatal for a null managed pointer @@ -200,14 +206,14 @@ public: //- If object pointer points to valid object: //- delete object and set pointer to nullptr - inline void clear() const; + inline void clear() const noexcept; //- Release ownership of managed temporary object. // After this call no object is managed. - inline void reset(); + inline void reset() noexcept; //- Delete managed temporary object and set to new given pointer - inline void reset(T* p); + inline void reset(T* p) noexcept; //- Swaps the managed object with other tmp. inline void swap(tmp<T>& other) noexcept; diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index c2729bc1c7c143ad386dffdd5359b2d6f9460e2e..63dd3668a062c11ad42de6ccdeff8f4ed8820bfc 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -186,28 +186,28 @@ inline Foam::tmp<T>::~tmp() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T> -inline bool Foam::tmp<T>::isTmp() const +inline bool Foam::tmp<T>::isTmp() const noexcept { return type_ == PTR; } template<class T> -inline bool Foam::tmp<T>::empty() const +inline bool Foam::tmp<T>::empty() const noexcept { return (!ptr_ && isTmp()); } template<class T> -inline bool Foam::tmp<T>::valid() const +inline bool Foam::tmp<T>::valid() const noexcept { return (ptr_ || type_ == CREF); } template<class T> -inline bool Foam::tmp<T>::movable() const +inline bool Foam::tmp<T>::movable() const noexcept { return (type_ == PTR && ptr_ && ptr_->unique()); } @@ -220,6 +220,20 @@ inline Foam::word Foam::tmp<T>::typeName() const } +template<class T> +inline T* Foam::tmp<T>::get() noexcept +{ + return ptr_; // non-const pointer +} + + +template<class T> +inline const T* Foam::tmp<T>::get() const noexcept +{ + return ptr_; // const pointer +} + + template<class T> inline const T& Foam::tmp<T>::cref() const { @@ -305,7 +319,7 @@ inline T* Foam::tmp<T>::ptr() const template<class T> -inline void Foam::tmp<T>::clear() const +inline void Foam::tmp<T>::clear() const noexcept { if (isTmp() && ptr_) { @@ -323,7 +337,7 @@ inline void Foam::tmp<T>::clear() const template<class T> -inline void Foam::tmp<T>::reset() +inline void Foam::tmp<T>::reset() noexcept { clear(); ptr_ = nullptr; @@ -332,7 +346,7 @@ inline void Foam::tmp<T>::reset() template<class T> -inline void Foam::tmp<T>::reset(T* p) +inline void Foam::tmp<T>::reset(T* p) noexcept { clear(); ptr_ = p; diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/tmp/tmpNrc.H index a5bd9c761b976634292b17de997a2e4e355513e9..b5b4dd36e3167e3241002cdb86c9b184019df515 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrc.H +++ b/src/OpenFOAM/memory/tmp/tmpNrc.H @@ -138,17 +138,17 @@ public: // Query //- True if this is a managed pointer (not a const reference) - inline bool isTmp() const; + inline bool isTmp() const noexcept; //- True if this is a non-null managed pointer - inline bool empty() const; + inline bool empty() const noexcept; //- True if this is a non-null managed pointer, //- or is a const object reference - inline bool valid() const; + inline bool valid() const noexcept; //- True if this is a non-null managed pointer with a unique ref-count - inline bool movable() const; + inline bool movable() const noexcept; //- Return type-name of the tmp, constructed from type-name of T inline word typeName() const; @@ -156,6 +156,12 @@ public: // Access + //- Return pointer without nullptr checking. + inline T* get() noexcept; + + //- Return const pointer without nullptr checking. + inline const T* get() const noexcept; + //- Return the const object reference or a const reference to the //- contents of a non-null managed pointer. // Fatal for a null managed pointer @@ -181,14 +187,14 @@ public: //- If object pointer points to valid object: //- delete object and set pointer to nullptr - inline void clear() const; + inline void clear() const noexcept; //- Release ownership of managed temporary object. // After this call no object is managed. - inline void reset(); + inline void reset() noexcept; //- Delete managed temporary object and set to new given pointer - inline void reset(T* p); + inline void reset(T* p) noexcept; //- Swaps the managed object with other tmpNrc. inline void swap(tmpNrc<T>& other) noexcept; diff --git a/src/OpenFOAM/memory/tmp/tmpNrcI.H b/src/OpenFOAM/memory/tmp/tmpNrcI.H index 8d3f0683de8b6280b1469605ca70be081ea93348..0a86958b2949ec6b868530b912182af12f08b9b0 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrcI.H +++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H @@ -150,28 +150,28 @@ inline Foam::tmpNrc<T>::~tmpNrc() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T> -inline bool Foam::tmpNrc<T>::isTmp() const +inline bool Foam::tmpNrc<T>::isTmp() const noexcept { return type_ == PTR; } template<class T> -inline bool Foam::tmpNrc<T>::empty() const +inline bool Foam::tmpNrc<T>::empty() const noexcept { return (!ptr_ && isTmp()); } template<class T> -inline bool Foam::tmpNrc<T>::valid() const +inline bool Foam::tmpNrc<T>::valid() const noexcept { return (ptr_ || type_ == CREF); } template<class T> -inline bool Foam::tmpNrc<T>::movable() const +inline bool Foam::tmpNrc<T>::movable() const noexcept { return (type_ == PTR && ptr_); } @@ -184,6 +184,20 @@ inline Foam::word Foam::tmpNrc<T>::typeName() const } +template<class T> +inline T* Foam::tmpNrc<T>::get() noexcept +{ + return ptr_; // non-const pointer +} + + +template<class T> +inline const T* Foam::tmpNrc<T>::get() const noexcept +{ + return ptr_; // const pointer +} + + template<class T> inline const T& Foam::tmpNrc<T>::cref() const { @@ -262,7 +276,7 @@ inline T* Foam::tmpNrc<T>::ptr() const template<class T> -inline void Foam::tmpNrc<T>::clear() const +inline void Foam::tmpNrc<T>::clear() const noexcept { if (isTmp() && ptr_) { @@ -273,7 +287,7 @@ inline void Foam::tmpNrc<T>::clear() const template<class T> -inline void Foam::tmpNrc<T>::reset() +inline void Foam::tmpNrc<T>::reset() noexcept { clear(); ptr_ = nullptr; @@ -282,7 +296,7 @@ inline void Foam::tmpNrc<T>::reset() template<class T> -inline void Foam::tmpNrc<T>::reset(T* p) +inline void Foam::tmpNrc<T>::reset(T* p) noexcept { clear(); ptr_ = p;