diff --git a/src/OpenFOAM/memory/UautoPtr/UautoPtr.H b/src/OpenFOAM/memory/UautoPtr/UautoPtr.H new file mode 100644 index 000000000..98f738703 --- /dev/null +++ b/src/OpenFOAM/memory/UautoPtr/UautoPtr.H @@ -0,0 +1,143 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::UautoPtr + +Description + An auto-pointer similar to the STL auto_ptr but with automatic casting + to a reference to the type and with pointer allocation checking on access. + +SourceFiles + UautoPtrI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef UautoPtr_H +#define UautoPtr_H + +#include + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class UautoPtr Declaration +\*---------------------------------------------------------------------------*/ + +template +class UautoPtr +{ + // Public data + + //- Pointer to object + mutable T* ptr_; + + +public: + + typedef T Type; + + + // Constructors + + //- Store object pointer + inline explicit UautoPtr(T* = nullptr); + + //- Construct as copy + inline UautoPtr(const UautoPtr&); + + + // Member Functions + + // Check + + //- Return true if the UautoPtr is empty (ie, no pointer set) + inline bool empty() const; + + //- Return true if the UautoPtr valid (ie, the pointer is set) + inline bool valid() const; + + + // Edit + + //- Return object pointer for reuse + inline T* ptr(); + + //- Set pointer to that given + // If object pointer already set issue a FatalError + inline void set(T*); + + //- Set pointer to that given + inline void reset(T* = nullptr); + + //- Set pointer to nullptr + inline void clear(); + + + // Member operators + + //- Return reference to the object data + inline T& operator()(); + + //- Return const reference to the object data + inline const T& operator()() const; + + //- Return reference to the object data + inline T& operator*(); + + //- Return const reference to the object data + inline const T& operator*() const; + + //- Const cast to the underlying type reference + inline operator const T&() const; + + //- Return object pointer + inline T* operator->(); + + //- Return const object pointer + inline const T* operator->() const; + + //- Take over the object pointer from parameter + inline void operator=(T*); + + //- Take over the object pointer from parameter + inline void operator=(const UautoPtr&); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "UautoPtrI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/memory/UautoPtr/UautoPtrI.H b/src/OpenFOAM/memory/UautoPtr/UautoPtrI.H new file mode 100644 index 000000000..81ef22a16 --- /dev/null +++ b/src/OpenFOAM/memory/UautoPtr/UautoPtrI.H @@ -0,0 +1,205 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "error.H" +#include + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template +inline Foam::UautoPtr::UautoPtr(T* p) +: + ptr_(p) +{} + + +template +inline Foam::UautoPtr::UautoPtr(const UautoPtr& ap) +: + ptr_(ap.ptr_) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +inline bool Foam::UautoPtr::empty() const +{ + return !ptr_; +} + + +template +inline bool Foam::UautoPtr::valid() const +{ + return ptr_; +} + + +template +inline T* Foam::UautoPtr::ptr() +{ + return ptr_; +} + + +template +inline void Foam::UautoPtr::set(T* p) +{ + if (ptr_) + { + FatalErrorInFunction + << "object of type " << typeid(T).name() + << " already allocated" + << abort(FatalError); + } + + ptr_ = p; +} + + +template +inline void Foam::UautoPtr::reset(T* p) +{ + ptr_ = p; +} + + +template +inline void Foam::UautoPtr::clear() +{ + reset(nullptr); +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +template +inline T& Foam::UautoPtr::operator()() +{ + if (!ptr_) + { + FatalErrorInFunction + << "object of type " << typeid(T).name() + << " is not allocated" + << abort(FatalError); + } + + return *ptr_; +} + + +template +inline const T& Foam::UautoPtr::operator()() const +{ + if (!ptr_) + { + FatalErrorInFunction + << "object of type " << typeid(T).name() + << " is not allocated" + << abort(FatalError); + } + + return *ptr_; +} + + +template +inline T& Foam::UautoPtr::operator*() +{ + if (!ptr_) + { + FatalErrorInFunction + << "object of type " << typeid(T).name() + << " is not allocated" + << abort(FatalError); + } + + return *ptr_; +} + + +template +inline const T& Foam::UautoPtr::operator*() const +{ + if (!ptr_) + { + FatalErrorInFunction + << "object of type " << typeid(T).name() + << " is not allocated" + << abort(FatalError); + } + + return *ptr_; +} + + +template +inline Foam::UautoPtr::operator const T&() const +{ + return operator()(); +} + + +template +inline T* Foam::UautoPtr::operator->() +{ + if (!ptr_) + { + FatalErrorInFunction + << "object of type " << typeid(T).name() + << " is not allocated" + << abort(FatalError); + } + + return ptr_; +} + + +template +inline const T* Foam::UautoPtr::operator->() const +{ + return const_cast&>(*this).operator->(); +} + + +template +inline void Foam::UautoPtr::operator=(T* p) +{ + reset(p); +} + + +template +inline void Foam::UautoPtr::operator=(const UautoPtr& ap) +{ + if (this != &ap) + { + reset(const_cast&>(ap).ptr()); + } +} + + +// ************************************************************************* //