mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add get() accessor to tmp classes
- similar to autoPtr and unique_ptr. Returns the pointer value without
any checks. This provides a simple way for use to use either
an autoPtr or a tmp for local memory management without accidentally
stealing the pointer.
Eg,
volVectorField* ptr;
tmp<volVectorField> tempField;
if (someField.valid())
{
ptr = someField.get();
}
else
{
tempField.reset(new volVectorField(....));
ptr = tmpField.get();
}
const volVectorField& withField = *ptr;
STYLE: make more tmp methods noexcept
This commit is contained in:
@ -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;
|
||||
|
||||
@ -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_;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user