diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/tmp/tmpNrc.H index 319754ce77d48f520c3c9b4cb12d826221e4a370..a5bd9c761b976634292b17de997a2e4e355513e9 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrc.H +++ b/src/OpenFOAM/memory/tmp/tmpNrc.H @@ -85,6 +85,26 @@ public: typedef Foam::refCount::zero refCount; + // Factory Methods + + //- Construct tmpNrc of T with forwarding arguments + // \param args list of arguments with which an instance of T + // will be constructed. + // + // \note Similar to std::make_shared, but the overload for + // array types is not disabled. + template<class... Args> + inline static tmpNrc<T> New(Args&&... args); + + //- Construct tmpNrc from derived type with forwarding arguments + // \param args list of arguments with which an instance of U + // will be constructed. + // + // \note Similar to New but for derived types + template<class U, class... Args> + inline static tmpNrc<T> NewFrom(Args&&... args); + + // Constructors //- Construct with no managed pointer. @@ -99,6 +119,9 @@ public: //- Construct for a const reference to an object. inline tmpNrc(const T& obj) noexcept; + //- Move construct + inline tmpNrc(tmpNrc<T>&& t); + //- Copy construct inline tmpNrc(const tmpNrc<T>& t); @@ -160,6 +183,13 @@ public: //- delete object and set pointer to nullptr inline void clear() const; + //- Release ownership of managed temporary object. + // After this call no object is managed. + inline void reset(); + + //- Delete managed temporary object and set to new given pointer + inline void reset(T* p); + //- 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 85e2b3a25c090a9b1c7e063b78fa5065461a4244..8d3f0683de8b6280b1469605ca70be081ea93348 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrcI.H +++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H @@ -26,6 +26,24 @@ License #include "error.H" #include <typeinfo> +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +template<class T> +template<class... Args> +inline Foam::tmpNrc<T> Foam::tmpNrc<T>::New(Args&&... args) +{ + return tmpNrc<T>(new T(std::forward<Args>(args)...)); +} + + +template<class T> +template<class U, class... Args> +inline Foam::tmpNrc<T> Foam::tmpNrc<T>::NewFrom(Args&&... args) +{ + return tmpNrc<T>(new U(std::forward<Args>(args)...)); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class T> @@ -60,6 +78,17 @@ inline Foam::tmpNrc<T>::tmpNrc(const T& obj) noexcept {} +template<class T> +inline Foam::tmpNrc<T>::tmpNrc(tmpNrc<T>&& t) +: + ptr_(t.ptr_), + type_(t.type_) +{ + t.ptr_ = nullptr; + t.type_ = PTR; +} + + template<class T> inline Foam::tmpNrc<T>::tmpNrc(const tmpNrc<T>& t) : @@ -243,6 +272,24 @@ inline void Foam::tmpNrc<T>::clear() const } +template<class T> +inline void Foam::tmpNrc<T>::reset() +{ + clear(); + ptr_ = nullptr; + type_ = PTR; +} + + +template<class T> +inline void Foam::tmpNrc<T>::reset(T* p) +{ + clear(); + ptr_ = p; + type_ = PTR; +} + + template<class T> inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept {