UautoPtr: Prototype unallocated version of autoPtr
This is a variant of autoPtr which hold a pointer to a object the storage of which is managed elsewhere rather than by UautoPtr.
This commit is contained in:
143
src/OpenFOAM/memory/UautoPtr/UautoPtr.H
Normal file
143
src/OpenFOAM/memory/UautoPtr/UautoPtr.H
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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 <cstddef>
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class UautoPtr Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
template<class T>
|
||||
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<T>&);
|
||||
|
||||
|
||||
// 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<T>&);
|
||||
};
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
} // End namespace Foam
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#include "UautoPtrI.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#endif
|
||||
|
||||
// ************************************************************************* //
|
||||
205
src/OpenFOAM/memory/UautoPtr/UautoPtrI.H
Normal file
205
src/OpenFOAM/memory/UautoPtr/UautoPtrI.H
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "error.H"
|
||||
#include <typeinfo>
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
template<class T>
|
||||
inline Foam::UautoPtr<T>::UautoPtr(T* p)
|
||||
:
|
||||
ptr_(p)
|
||||
{}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline Foam::UautoPtr<T>::UautoPtr(const UautoPtr<T>& ap)
|
||||
:
|
||||
ptr_(ap.ptr_)
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class T>
|
||||
inline bool Foam::UautoPtr<T>::empty() const
|
||||
{
|
||||
return !ptr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline bool Foam::UautoPtr<T>::valid() const
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline T* Foam::UautoPtr<T>::ptr()
|
||||
{
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::UautoPtr<T>::set(T* p)
|
||||
{
|
||||
if (ptr_)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "object of type " << typeid(T).name()
|
||||
<< " already allocated"
|
||||
<< abort(FatalError);
|
||||
}
|
||||
|
||||
ptr_ = p;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::UautoPtr<T>::reset(T* p)
|
||||
{
|
||||
ptr_ = p;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::UautoPtr<T>::clear()
|
||||
{
|
||||
reset(nullptr);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||
|
||||
template<class T>
|
||||
inline T& Foam::UautoPtr<T>::operator()()
|
||||
{
|
||||
if (!ptr_)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "object of type " << typeid(T).name()
|
||||
<< " is not allocated"
|
||||
<< abort(FatalError);
|
||||
}
|
||||
|
||||
return *ptr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline const T& Foam::UautoPtr<T>::operator()() const
|
||||
{
|
||||
if (!ptr_)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "object of type " << typeid(T).name()
|
||||
<< " is not allocated"
|
||||
<< abort(FatalError);
|
||||
}
|
||||
|
||||
return *ptr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline T& Foam::UautoPtr<T>::operator*()
|
||||
{
|
||||
if (!ptr_)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "object of type " << typeid(T).name()
|
||||
<< " is not allocated"
|
||||
<< abort(FatalError);
|
||||
}
|
||||
|
||||
return *ptr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline const T& Foam::UautoPtr<T>::operator*() const
|
||||
{
|
||||
if (!ptr_)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "object of type " << typeid(T).name()
|
||||
<< " is not allocated"
|
||||
<< abort(FatalError);
|
||||
}
|
||||
|
||||
return *ptr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline Foam::UautoPtr<T>::operator const T&() const
|
||||
{
|
||||
return operator()();
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline T* Foam::UautoPtr<T>::operator->()
|
||||
{
|
||||
if (!ptr_)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "object of type " << typeid(T).name()
|
||||
<< " is not allocated"
|
||||
<< abort(FatalError);
|
||||
}
|
||||
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline const T* Foam::UautoPtr<T>::operator->() const
|
||||
{
|
||||
return const_cast<UautoPtr<T>&>(*this).operator->();
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::UautoPtr<T>::operator=(T* p)
|
||||
{
|
||||
reset(p);
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::UautoPtr<T>::operator=(const UautoPtr<T>& ap)
|
||||
{
|
||||
if (this != &ap)
|
||||
{
|
||||
reset(const_cast<UautoPtr<T>&>(ap).ptr());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user