ENH: add refPtr release() method

- releases ownership of the pointer. A no-op (and returns nullptr)
  for references.

  Naming consistent with unique_ptr and autoPtr.

DOC: adjust wording for memory-related classes

- add is_const() method for tmp, refPtr.

  Drop (ununsed and confusing looking) isTmp method from refPtr
  in favour of is_pointer() or movable() checks

ENH: noexcept for some pTraits methods, remove redundant 'inline'

- test for const first for tmp/refPtr (simpler logic)
This commit is contained in:
Mark Olesen
2021-11-15 19:34:01 +01:00
parent 9c9d6c64b5
commit effd69a005
27 changed files with 241 additions and 196 deletions

View File

@ -39,6 +39,7 @@ Description
#include "argList.H" #include "argList.H"
#include "Time.H" #include "Time.H"
#include "IOobject.H" #include "IOobject.H"
#include "scalarField.H"
namespace Foam namespace Foam
{ {
@ -139,6 +140,8 @@ int main(int argc, char *argv[])
cout<<"PstreamBuffers:" << sizeof(Foam::PstreamBuffers) << nl; cout<<"PstreamBuffers:" << sizeof(Foam::PstreamBuffers) << nl;
cout<<"Time:" << sizeof(Foam::Time) << nl; cout<<"Time:" << sizeof(Foam::Time) << nl;
cout<<"tmp<>:" << sizeof(tmp<scalarField>) << nl;
Info << "---\nEnd\n" << endl; Info << "---\nEnd\n" << endl;
return 0; return 0;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd. Copyright (C) 2018-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -98,12 +98,12 @@ inline Type& Foam::regIOobject::store(refPtr<Type>& ptr)
if (ptr.is_pointer()) if (ptr.is_pointer())
{ {
// Acquire ownership, pass management to objectRegistry // Acquire ownership, pass management to objectRegistry
p = ptr.ptr(); p = ptr.release();
store(p); store(p);
// Change parameter to access the stored reference // Change parameter to access the stored reference
ptr.cref(*p); ptr.cref(p);
} }
else else
{ {
@ -144,7 +144,7 @@ inline Type& Foam::regIOobject::store(tmp<Type>& ptr)
store(p); store(p);
// Change parameter to access the stored reference // Change parameter to access the stored reference
ptr.cref(*p); ptr.cref(p);
} }
else else
{ {

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -65,7 +65,7 @@ namespace Foam
template<class T> template<class T>
class autoPtr class autoPtr
{ {
//- Pointer to managed object //- The managed pointer
T* ptr_; T* ptr_;
@ -103,10 +103,10 @@ public:
// Constructors // Constructors
//- Construct with no managed object. //- Construct with no managed pointer.
inline constexpr autoPtr() noexcept; inline constexpr autoPtr() noexcept;
//- Construct with no managed object (literal nullptr). //- Construct with no managed pointer (literal nullptr).
inline constexpr autoPtr(std::nullptr_t) noexcept; inline constexpr autoPtr(std::nullptr_t) noexcept;
//- Construct, taking ownership of the pointer. //- Construct, taking ownership of the pointer.
@ -134,7 +134,7 @@ public:
#endif #endif
//- Deletes the managed object if such is present //- Deletes the managed pointer
inline ~autoPtr() noexcept; inline ~autoPtr() noexcept;
@ -142,14 +142,8 @@ public:
// Query // Query
//- Deprecated(2020-07) True if the managed pointer is null
//
// \deprecated(2020-07) - use bool operator
FOAM_DEPRECATED_FOR(2020-07, "bool operator")
bool empty() const noexcept { return !ptr_; }
//- True if the managed pointer is non-null //- True if the managed pointer is non-null
bool valid() const noexcept { return ptr_; } bool valid() const noexcept { return bool(ptr_); }
// Access // Access
@ -170,7 +164,7 @@ public:
// Edit // Edit
//- Return pointer to the managed object and release ownership. //- Release ownership and return the pointer.
inline T* release() noexcept; inline T* release() noexcept;
//- Same as \c release(). //- Same as \c release().
@ -182,11 +176,11 @@ public:
void clear() noexcept { reset(nullptr); } void clear() noexcept { reset(nullptr); }
//- Delete managed object and set to new given pointer //- Delete managed object and set to new given pointer
inline void reset(T* p = nullptr) noexcept; // \remark Same as move assign, but better for code documentation
inline void reset(autoPtr<T>&& other) noexcept;
//- Delete managed object and set to new given pointer //- Delete managed object and set to new given pointer
// \remark Same as move assign, but better for code documentation inline void reset(T* p = nullptr) noexcept;
inline void reset(autoPtr<T>&& ap) noexcept;
//- Swaps the managed object with other autoPtr. //- Swaps the managed object with other autoPtr.
inline void swap(autoPtr<T>& other) noexcept; inline void swap(autoPtr<T>& other) noexcept;
@ -230,7 +224,7 @@ public:
// Casting // Casting
//- True if the managed pointer is non-null //- True if pointer/reference is non-null. Same as valid()
explicit operator bool() const noexcept { return bool(ptr_); } explicit operator bool() const noexcept { return bool(ptr_); }
//- Cast to pointer type //- Cast to pointer type
@ -253,6 +247,9 @@ public:
// Assignment // Assignment
//- No copy assignment from plain pointer (uncontrolled access)
void operator=(T* p) = delete;
//- Transfer object ownership from parameter //- Transfer object ownership from parameter
inline void operator=(autoPtr<T>&& ap) noexcept; inline void operator=(autoPtr<T>&& ap) noexcept;
@ -270,6 +267,12 @@ public:
// Housekeeping // Housekeeping
//- Deprecated(2020-07) True if the managed pointer is null
//
// \deprecated(2020-07) - use bool operator
FOAM_DEPRECATED_FOR(2020-07, "bool operator")
bool empty() const noexcept { return !ptr_; }
//- Deprecated(2018-02) Identical to reset(). //- Deprecated(2018-02) Identical to reset().
// \note Provided for backward compatibility - the older version // \note Provided for backward compatibility - the older version
// enforced a run-time check (Fatal if pointer was already set) // enforced a run-time check (Fatal if pointer was already set)
@ -279,10 +282,6 @@ public:
FOAM_DEPRECATED_FOR(2018-02, "reset() - same behaviour") FOAM_DEPRECATED_FOR(2018-02, "reset() - same behaviour")
#endif #endif
void set(T* p) noexcept { reset(p); } void set(T* p) noexcept { reset(p); }
//- Deprecated(2018-02) No copy assignment from plain pointer
// \deprecated(2018-02) Convenient, but uncontrolled access
void operator=(T* p) = delete;
}; };

View File

@ -114,9 +114,13 @@ inline void Foam::autoPtr<T>::reset(T* p) noexcept
template<class T> template<class T>
inline void Foam::autoPtr<T>::reset(autoPtr<T>&& ap) noexcept inline void Foam::autoPtr<T>::reset(autoPtr<T>&& other) noexcept
{ {
reset(ap.release()); if (&other != this)
{
// Ignore self-assignment
reset(other.release());
}
} }

View File

@ -59,12 +59,12 @@ class refPtr
{ {
// Private Data // Private Data
//- Object types //- The stored reference type
enum refType enum refType
{ {
PTR, //!< Managing a pointer (not ref-counted) PTR, //!< A managed pointer (not ref-counted)
CREF, //!< Using (const) reference to an object CREF, //!< A const reference to an object
REF //!< Using (non-const) reference to an object REF //!< A non-const reference to an object
}; };
//- The managed pointer or address of the object (reference) //- The managed pointer or address of the object (reference)
@ -111,10 +111,10 @@ public:
// Constructors // Constructors
//- Default construct, no managed pointer. //- Construct with no managed pointer.
inline constexpr refPtr() noexcept; inline constexpr refPtr() noexcept;
//- Construct with no managed pointer. //- Construct with no managed pointer (literal nullptr).
inline constexpr refPtr(std::nullptr_t) noexcept; inline constexpr refPtr(std::nullptr_t) noexcept;
//- Construct, taking ownership of the pointer. //- Construct, taking ownership of the pointer.
@ -151,21 +151,15 @@ public:
// Query // Query
//- Deprecated(2020-07) True if a null managed pointer
//
// \deprecated(2020-07) - use bool operator
FOAM_DEPRECATED_FOR(2020-07, "bool operator")
bool empty() const noexcept { return !ptr_; }
//- True for non-null pointer/reference //- True for non-null pointer/reference
bool valid() const noexcept { return ptr_; } bool valid() const noexcept { return bool(ptr_); }
//- If the stored/referenced content is const
bool is_const() const noexcept { return type_ == CREF; }
//- True if this is a managed pointer (not a reference) //- True if this is a managed pointer (not a reference)
bool is_pointer() const noexcept { return type_ == PTR; } bool is_pointer() const noexcept { return type_ == PTR; }
//- Identical to is_pointer()
bool isTmp() const noexcept { return type_ == PTR; }
//- True if this is a non-null managed pointer //- True if this is a non-null managed pointer
inline bool movable() const noexcept; inline bool movable() const noexcept;
@ -196,6 +190,10 @@ public:
// Edit // Edit
//- Release ownership and return the pointer.
//- A no-op for reference objects (returns nullptr).
inline T* release() noexcept;
//- Return managed pointer for reuse, or clone() the object reference. //- Return managed pointer for reuse, or clone() the object reference.
inline T* ptr() const; inline T* ptr() const;
@ -203,7 +201,10 @@ public:
//- delete object and set pointer to nullptr //- delete object and set pointer to nullptr
inline void clear() const noexcept; inline void clear() const noexcept;
//- Delete managed temporary object and set to new given pointer //- Clear existing and transfer ownership.
inline void reset(refPtr<T>&& other) noexcept;
//- Delete managed pointer and set to new given pointer
inline void reset(T* p = nullptr) noexcept; inline void reset(T* p = nullptr) noexcept;
//- Clear existing and transfer ownership from autoPtr. //- Clear existing and transfer ownership from autoPtr.
@ -212,21 +213,18 @@ public:
//- Clear existing and transfer ownership from unique_ptr //- Clear existing and transfer ownership from unique_ptr
void reset(std::unique_ptr<T>&& other) { reset(other.release()); } void reset(std::unique_ptr<T>&& other) { reset(other.release()); }
//- Clear existing and transfer ownership.
inline void reset(refPtr<T>&& other) noexcept;
//- Clear existing and set (const) reference //- Clear existing and set (const) reference
inline void cref(const T& obj) noexcept; inline void cref(const T& obj) noexcept;
//- Clear existing and set (const) reference to pointer content. //- Clear existing and set (const) reference to pointer content.
// A null pointer is permitted (treated as a managed pointer). // The pointer can be null, which is handled like a clear().
inline void cref(const T* p) noexcept; inline void cref(const T* p) noexcept;
//- Clear existing and set (non-const) reference //- Clear existing and set (non-const) reference
inline void ref(T& obj) noexcept; inline void ref(T& obj) noexcept;
//- Clear existing and set (non-const) reference to pointer content. //- Clear existing and set (non-const) reference to pointer content.
// A null pointer is permitted (treated as a managed pointer). // The pointer can be null, which is handled like a clear().
inline void ref(T* p) noexcept; inline void ref(T* p) noexcept;
//- Swaps the managed object with other. //- Swaps the managed object with other.
@ -257,8 +255,8 @@ public:
//- Cast to underlying data type, using the cref() method. //- Cast to underlying data type, using the cref() method.
operator const T&() const { return cref(); } operator const T&() const { return cref(); }
//- Non-null pointer/reference : valid() //- True if pointer/reference is non-null. Same as valid()
explicit operator bool() const noexcept { return ptr_; } explicit operator bool() const noexcept { return bool(ptr_); }
//- Transfer ownership of the managed pointer. //- Transfer ownership of the managed pointer.
// Fatal for a null managed pointer or if the object is const. // Fatal for a null managed pointer or if the object is const.
@ -276,6 +274,15 @@ public:
//- Conversion to tmp, releases pointer or shallow-copies reference //- Conversion to tmp, releases pointer or shallow-copies reference
inline operator tmp<T>(); inline operator tmp<T>();
// Housekeeping
//- Deprecated(2020-07) True if a null managed pointer
//
// \deprecated(2020-07) - use bool operator
FOAM_DEPRECATED_FOR(2020-07, "bool operator")
bool empty() const noexcept { return !ptr_; }
}; };

View File

@ -186,41 +186,35 @@ inline bool Foam::refPtr<T>::movable() const noexcept
template<class T> template<class T>
inline const T& Foam::refPtr<T>::cref() const inline const T& Foam::refPtr<T>::cref() const
{ {
if (type_ == PTR) if (type_ == PTR && !ptr_)
{ {
if (!ptr_) FatalErrorInFunction
{ << this->typeName() << " deallocated"
FatalErrorInFunction << abort(FatalError);
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
} }
return *ptr_; // const reference return *ptr_; // const reference
} }
template<class T> template<class T>
inline T& Foam::refPtr<T>::ref() const inline T& Foam::refPtr<T>::ref() const
{ {
if (type_ == PTR) if (type_ == CREF)
{
if (!ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
}
else if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempted non-const reference to const object from a " << "Attempted non-const reference to const object: "
<< this->typeName() << this->typeName()
<< abort(FatalError); << abort(FatalError);
} }
else if (type_ == PTR && !ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
return *ptr_; // non-const reference return *ptr_; // non-const reference
} }
@ -231,6 +225,20 @@ inline T& Foam::refPtr<T>::constCast() const
} }
template<class T>
inline T* Foam::refPtr<T>::release() noexcept
{
if (type_ == PTR)
{
T* p = ptr_;
ptr_ = nullptr;
return p;
}
return nullptr;
}
template<class T> template<class T>
inline T* Foam::refPtr<T>::ptr() const inline T* Foam::refPtr<T>::ptr() const
{ {
@ -363,7 +371,7 @@ inline T& Foam::refPtr<T>::operator*()
if (type_ == CREF) if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempt to cast const object to non-const for a " << "Attempt to cast const object to non-const: "
<< this->typeName() << this->typeName()
<< abort(FatalError); << abort(FatalError);
} }
@ -398,7 +406,7 @@ inline T* Foam::refPtr<T>::operator->()
if (type_ == CREF) if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempt to cast const object to non-const for a " << "Attempt to cast const object to non-const: "
<< this->typeName() << this->typeName()
<< abort(FatalError); << abort(FatalError);
} }

View File

@ -66,12 +66,12 @@ class tmp
{ {
// Private Data // Private Data
//- Object types //- The stored reference type
enum refType enum refType
{ {
PTR, //!< Managing a pointer (ref-counted) PTR, //!< A managed pointer (ref-counted)
CREF, //!< Using (const) reference to an object CREF, //!< A const reference to an object
REF //!< Using (non-const) reference to an object REF //!< A non-const reference to an object
}; };
//- The managed pointer or address of the object (reference) //- The managed pointer or address of the object (reference)
@ -125,10 +125,10 @@ public:
// Constructors // Constructors
//- Default construct, no managed pointer. //- Construct with no managed pointer.
inline constexpr tmp() noexcept; inline constexpr tmp() noexcept;
//- Construct with no managed pointer. //- Construct with no managed pointer (literal nullptr).
inline constexpr tmp(std::nullptr_t) noexcept; inline constexpr tmp(std::nullptr_t) noexcept;
//- Construct, taking ownership of the pointer. //- Construct, taking ownership of the pointer.
@ -166,14 +166,11 @@ public:
// Query // Query
//- Deprecated(2020-07) True if a null managed pointer //- True if pointer/reference is non-null
// bool valid() const noexcept { return bool(ptr_); }
// \deprecated(2020-07) - use bool operator
FOAM_DEPRECATED_FOR(2020-07, "bool operator")
bool empty() const noexcept { return !ptr_; }
//- True for non-null pointer/reference //- If the stored/referenced content is const
bool valid() const noexcept { return ptr_; } bool is_const() const noexcept { return type_ == CREF; }
//- True if this is a managed pointer (not a reference) //- True if this is a managed pointer (not a reference)
bool is_pointer() const noexcept { return type_ == PTR; } bool is_pointer() const noexcept { return type_ == PTR; }
@ -218,24 +215,24 @@ public:
//- delete object and set pointer to nullptr //- delete object and set pointer to nullptr
inline void clear() const noexcept; inline void clear() const noexcept;
//- Delete managed temporary object and set to new given pointer
inline void reset(T* p = nullptr) noexcept;
//- Clear existing and transfer ownership. //- Clear existing and transfer ownership.
inline void reset(tmp<T>&& other) noexcept; inline void reset(tmp<T>&& other) noexcept;
//- Delete managed temporary object and set to new given pointer
inline void reset(T* p = nullptr) noexcept;
//- Clear existing and set (const) reference //- Clear existing and set (const) reference
inline void cref(const T& obj) noexcept; inline void cref(const T& obj) noexcept;
//- Clear existing and set (const) reference to pointer content. //- Clear existing and set (const) reference to pointer content.
// A null pointer is permitted (treated as a managed pointer). // The pointer can be null, which is handled like a clear().
inline void cref(const T* p) noexcept; inline void cref(const T* p) noexcept;
//- Clear existing and set to (non-const) reference //- Clear existing and set to (non-const) reference
inline void ref(T& obj) noexcept; inline void ref(T& obj) noexcept;
//- Clear existing and set (non-const) reference to pointer content. //- Clear existing and set (non-const) reference to pointer content.
// A null pointer is permitted (treated as a managed pointer). // The pointer can be null, which is handled like a clear().
inline void ref(T* p) noexcept; inline void ref(T* p) noexcept;
//- Swaps the managed object with other. //- Swaps the managed object with other.
@ -258,8 +255,8 @@ public:
//- Cast to underlying data type, using the cref() method. //- Cast to underlying data type, using the cref() method.
operator const T&() const { return cref(); } operator const T&() const { return cref(); }
//- Non-null pointer/reference : valid() //- True if pointer/reference is non-null. Same as valid()
explicit operator bool() const noexcept { return ptr_; } explicit operator bool() const noexcept { return bool(ptr_); }
//- Transfer ownership of the managed pointer. //- Transfer ownership of the managed pointer.
// Fatal for a null managed pointer or if the object is const. // Fatal for a null managed pointer or if the object is const.
@ -274,6 +271,15 @@ public:
//- Reset via assignment from literal nullptr //- Reset via assignment from literal nullptr
inline void operator=(std::nullptr_t) noexcept; inline void operator=(std::nullptr_t) noexcept;
// Housekeeping
//- Deprecated(2020-07) True if a null managed pointer
//
// \deprecated(2020-07) - use bool operator
FOAM_DEPRECATED_FOR(2020-07, "bool operator")
bool empty() const noexcept { return !ptr_; }
}; };

View File

@ -210,41 +210,35 @@ inline bool Foam::tmp<T>::movable() const noexcept
template<class T> template<class T>
inline const T& Foam::tmp<T>::cref() const inline const T& Foam::tmp<T>::cref() const
{ {
if (type_ == PTR) if (type_ == PTR && !ptr_)
{ {
if (!ptr_) FatalErrorInFunction
{ << this->typeName() << " deallocated"
FatalErrorInFunction << abort(FatalError);
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
} }
return *ptr_; // const reference return *ptr_; // const reference
} }
template<class T> template<class T>
inline T& Foam::tmp<T>::ref() const inline T& Foam::tmp<T>::ref() const
{ {
if (type_ == PTR) if (type_ == CREF)
{
if (!ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
}
else if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempted non-const reference to const object from a " << "Attempted non-const reference to const object: "
<< this->typeName() << this->typeName()
<< abort(FatalError); << abort(FatalError);
} }
else if (type_ == PTR && !ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
return *ptr_; // non-const reference return *ptr_; // non-const reference
} }
@ -387,14 +381,11 @@ inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
template<class T> template<class T>
inline const T* Foam::tmp<T>::operator->() const inline const T* Foam::tmp<T>::operator->() const
{ {
if (type_ == PTR) if (type_ == PTR && !ptr_)
{ {
if (!ptr_) FatalErrorInFunction
{ << this->typeName() << " deallocated"
FatalErrorInFunction << abort(FatalError);
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
} }
return ptr_; return ptr_;
@ -404,22 +395,19 @@ inline const T* Foam::tmp<T>::operator->() const
template<class T> template<class T>
inline T* Foam::tmp<T>::operator->() inline T* Foam::tmp<T>::operator->()
{ {
if (type_ == PTR) if (type_ == CREF)
{
if (!ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
}
else if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempt to cast const object to non-const for a " << "Attempt to cast const object to non-const: "
<< this->typeName() << this->typeName()
<< abort(FatalError); << abort(FatalError);
} }
else if (type_ == PTR && !ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
return ptr_; return ptr_;
} }

View File

@ -31,7 +31,7 @@ License
namespace Foam namespace Foam
{ {
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const pTraits<Scalar>::typeName = "scalar"; const char* const pTraits<Scalar>::typeName = "scalar";
const char* const pTraits<Scalar>::componentNames[] = { "" }; const char* const pTraits<Scalar>::componentNames[] = { "" };
@ -45,7 +45,9 @@ const Scalar pTraits<Scalar>::rootMax = ScalarROOTVGREAT;
const Scalar pTraits<Scalar>::vsmall = ScalarVSMALL; const Scalar pTraits<Scalar>::vsmall = ScalarVSMALL;
pTraits<Scalar>::pTraits(const Scalar& val) // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
pTraits<Scalar>::pTraits(const Scalar& val) noexcept
: :
p_(val) p_(val)
{} {}

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -110,7 +110,7 @@ public:
// Constructors // Constructors
//- Copy construct from primitive //- Copy construct from primitive
explicit pTraits(const Scalar& val); explicit pTraits(const Scalar& val) noexcept;
//- Read construct from Istream //- Read construct from Istream
explicit pTraits(Istream& is); explicit pTraits(Istream& is);
@ -119,13 +119,13 @@ public:
// Member Functions // Member Functions
//- Access to the value //- Access to the value
operator Scalar() const operator Scalar() const noexcept
{ {
return p_; return p_;
} }
//- Access to the value //- Access to the value
operator Scalar&() operator Scalar&() noexcept
{ {
return p_; return p_;
} }
@ -143,7 +143,7 @@ word name(const Scalar val);
template<> template<>
struct nameOp<Scalar> struct nameOp<Scalar>
{ {
inline word operator()(const Scalar val) const word operator()(const Scalar val) const
{ {
return Foam::name(val); return Foam::name(val);
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd. Copyright (C) 2018-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -31,7 +31,7 @@ License
#include "error.H" #include "error.H"
#include "IOstreams.H" #include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::pTraits<bool>::typeName = "bool"; const char* const Foam::pTraits<bool>::typeName = "bool";
const char* const Foam::pTraits<bool>::componentNames[] = { "" }; const char* const Foam::pTraits<bool>::componentNames[] = { "" };
@ -40,7 +40,9 @@ const bool Foam::pTraits<bool>::zero = false;
const bool Foam::pTraits<bool>::one = true; const bool Foam::pTraits<bool>::one = true;
Foam::pTraits<bool>::pTraits(const bool& p) // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::pTraits<bool>::pTraits(const bool& p) noexcept
: :
p_(p) p_(p)
{} {}
@ -52,6 +54,8 @@ Foam::pTraits<bool>::pTraits(Istream& is)
} }
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, bool& b) Foam::Istream& Foam::operator>>(Istream& is, bool& b)
{ {
b = static_cast<bool>(Switch(is)); b = static_cast<bool>(Switch(is));

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd. Copyright (C) 2019-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -109,7 +109,7 @@ public:
// Constructors // Constructors
//- Copy construct from primitive //- Copy construct from primitive
explicit pTraits(const bool& p); explicit pTraits(const bool& p) noexcept;
//- Read construct from Istream //- Read construct from Istream
explicit pTraits(Istream& is); explicit pTraits(Istream& is);
@ -118,13 +118,13 @@ public:
// Member Functions // Member Functions
//- Access to the value //- Access to the value
operator bool() const operator bool() const noexcept
{ {
return p_; return p_;
} }
//- Access to the value //- Access to the value
operator bool&() operator bool&() noexcept
{ {
return p_; return p_;
} }

View File

@ -29,10 +29,13 @@ License
#include "char.H" #include "char.H"
#include "IOstreams.H" #include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::pTraits<char>::typeName = "char"; const char* const Foam::pTraits<char>::typeName = "char";
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::pTraits<char>::pTraits(const char p) noexcept Foam::pTraits<char>::pTraits(const char p) noexcept
: :
p_(p) p_(p)
@ -45,6 +48,8 @@ Foam::pTraits<char>::pTraits(Istream& is)
} }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
char Foam::readChar(Istream& is) char Foam::readChar(Istream& is)
{ {
char c; char c;

View File

@ -61,7 +61,7 @@ inline word name(const int16_t val)
template<> template<>
struct nameOp<int16_t> struct nameOp<int16_t>
{ {
inline word operator()(const int16_t val) const word operator()(const int16_t val) const
{ {
return word(std::to_string(int(val)), false); // Needs no stripping return word(std::to_string(int(val)), false); // Needs no stripping
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2017-2018 OpenCFD Ltd. Copyright (C) 2017-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,7 +28,7 @@ License
#include "int32.H" #include "int32.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::pTraits<int32_t>::componentNames[] = { "" }; const char* const Foam::pTraits<int32_t>::componentNames[] = { "" };
@ -39,7 +39,10 @@ const int32_t Foam::pTraits<int32_t>::max = INT32_MAX;
const int32_t Foam::pTraits<int32_t>::rootMin = pTraits<int32_t>::min; const int32_t Foam::pTraits<int32_t>::rootMin = pTraits<int32_t>::min;
const int32_t Foam::pTraits<int32_t>::rootMax = pTraits<int32_t>::max; const int32_t Foam::pTraits<int32_t>::rootMax = pTraits<int32_t>::max;
Foam::pTraits<int32_t>::pTraits(const int32_t& val)
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::pTraits<int32_t>::pTraits(const int32_t& val) noexcept
: :
p_(val) p_(val)
{} {}

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -69,7 +69,7 @@ inline word name(const int32_t val)
template<> template<>
struct nameOp<int32_t> struct nameOp<int32_t>
{ {
inline word operator()(const int32_t val) const word operator()(const int32_t val) const
{ {
return word(std::to_string(val), false); // Needs no stripping return word(std::to_string(val), false); // Needs no stripping
} }
@ -175,7 +175,7 @@ public:
// Constructors // Constructors
//- Copy construct from primitive //- Copy construct from primitive
explicit pTraits(const int32_t& val); explicit pTraits(const int32_t& val) noexcept;
//- Read construct from Istream //- Read construct from Istream
explicit pTraits(Istream& is); explicit pTraits(Istream& is);
@ -184,13 +184,13 @@ public:
// Member Functions // Member Functions
//- Access to the value //- Access to the value
operator int32_t() const operator int32_t() const noexcept
{ {
return p_; return p_;
} }
//- Access to the value //- Access to the value
operator int32_t&() operator int32_t&() noexcept
{ {
return p_; return p_;
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2017-2018 OpenCFD Ltd. Copyright (C) 2017-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,7 +28,7 @@ License
#include "int64.H" #include "int64.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::pTraits<int64_t>::componentNames[] = { "" }; const char* const Foam::pTraits<int64_t>::componentNames[] = { "" };
@ -39,11 +39,15 @@ const int64_t Foam::pTraits<int64_t>::max = INT64_MAX;
const int64_t Foam::pTraits<int64_t>::rootMin = pTraits<int64_t>::min; const int64_t Foam::pTraits<int64_t>::rootMin = pTraits<int64_t>::min;
const int64_t Foam::pTraits<int64_t>::rootMax = pTraits<int64_t>::max; const int64_t Foam::pTraits<int64_t>::rootMax = pTraits<int64_t>::max;
Foam::pTraits<int64_t>::pTraits(const int64_t& val)
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::pTraits<int64_t>::pTraits(const int64_t& val) noexcept
: :
p_(val) p_(val)
{} {}
Foam::pTraits<int64_t>::pTraits(Istream& is) Foam::pTraits<int64_t>::pTraits(Istream& is)
{ {
is >> p_; is >> p_;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -69,7 +69,7 @@ inline word name(const int64_t val)
template<> template<>
struct nameOp<int64_t> struct nameOp<int64_t>
{ {
inline word operator()(const int64_t val) const word operator()(const int64_t val) const
{ {
return word(std::to_string(val), false); // Needs no stripping return word(std::to_string(val), false); // Needs no stripping
} }
@ -174,7 +174,7 @@ public:
// Constructors // Constructors
//- Copy construct from primitive //- Copy construct from primitive
explicit pTraits(const int64_t& val); explicit pTraits(const int64_t& val) noexcept;
//- Read construct from Istream //- Read construct from Istream
explicit pTraits(Istream& is); explicit pTraits(Istream& is);
@ -183,13 +183,13 @@ public:
// Member Functions // Member Functions
//- Access to the value //- Access to the value
operator int64_t() const operator int64_t() const noexcept
{ {
return p_; return p_;
} }
//- Access to the value //- Access to the value
operator int64_t&() operator int64_t&() noexcept
{ {
return p_; return p_;
} }

View File

@ -62,7 +62,7 @@ inline word name(const uint16_t val)
template<> template<>
struct nameOp<uint16_t> struct nameOp<uint16_t>
{ {
inline word operator()(const uint16_t val) const word operator()(const uint16_t val) const
{ {
return word(std::to_string(int(val)), false); // Needs no stripping return word(std::to_string(int(val)), false); // Needs no stripping
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2017 OpenCFD Ltd. Copyright (C) 2017-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,7 +28,7 @@ License
#include "uint32.H" #include "uint32.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::pTraits<uint32_t>::componentNames[] = { "" }; const char* const Foam::pTraits<uint32_t>::componentNames[] = { "" };
@ -39,11 +39,15 @@ const uint32_t Foam::pTraits<uint32_t>::max = UINT32_MAX;
const uint32_t Foam::pTraits<uint32_t>::rootMin = 0; const uint32_t Foam::pTraits<uint32_t>::rootMin = 0;
const uint32_t Foam::pTraits<uint32_t>::rootMax = pTraits<uint32_t>::max; const uint32_t Foam::pTraits<uint32_t>::rootMax = pTraits<uint32_t>::max;
Foam::pTraits<uint32_t>::pTraits(const uint32_t& val)
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::pTraits<uint32_t>::pTraits(const uint32_t& val) noexcept
: :
p_(val) p_(val)
{} {}
Foam::pTraits<uint32_t>::pTraits(Istream& is) Foam::pTraits<uint32_t>::pTraits(Istream& is)
{ {
is >> p_; is >> p_;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -68,7 +68,7 @@ inline word name(const uint32_t val)
template<> template<>
struct nameOp<uint32_t> struct nameOp<uint32_t>
{ {
inline word operator()(const uint32_t val) const word operator()(const uint32_t val) const
{ {
return word(std::to_string(val), false); // Needs no stripping return word(std::to_string(val), false); // Needs no stripping
} }
@ -161,7 +161,7 @@ public:
// Constructors // Constructors
//- Copy construct from primitive //- Copy construct from primitive
explicit pTraits(const uint32_t& val); explicit pTraits(const uint32_t& val) noexcept;
//- Read construct from Istream //- Read construct from Istream
explicit pTraits(Istream& is); explicit pTraits(Istream& is);
@ -170,13 +170,13 @@ public:
// Member Functions // Member Functions
//- Access to the value //- Access to the value
operator uint32_t() const operator uint32_t() const noexcept
{ {
return p_; return p_;
} }
//- Access to the value //- Access to the value
operator uint32_t&() operator uint32_t&() noexcept
{ {
return p_; return p_;
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2017 OpenCFD Ltd. Copyright (C) 2017-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,7 +28,7 @@ License
#include "uint64.H" #include "uint64.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::pTraits<uint64_t>::componentNames[] = { "" }; const char* const Foam::pTraits<uint64_t>::componentNames[] = { "" };
@ -39,7 +39,10 @@ const uint64_t Foam::pTraits<uint64_t>::max = UINT64_MAX;
const uint64_t Foam::pTraits<uint64_t>::rootMin = 0; const uint64_t Foam::pTraits<uint64_t>::rootMin = 0;
const uint64_t Foam::pTraits<uint64_t>::rootMax = pTraits<uint64_t>::max; const uint64_t Foam::pTraits<uint64_t>::rootMax = pTraits<uint64_t>::max;
Foam::pTraits<uint64_t>::pTraits(const uint64_t& val)
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::pTraits<uint64_t>::pTraits(const uint64_t& val) noexcept
: :
p_(val) p_(val)
{} {}

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -69,7 +69,7 @@ inline word name(const uint64_t val)
template<> template<>
struct nameOp<uint64_t> struct nameOp<uint64_t>
{ {
inline word operator()(const uint64_t val) const word operator()(const uint64_t val) const
{ {
return word(std::to_string(val), false); // Needs no stripping return word(std::to_string(val), false); // Needs no stripping
} }
@ -170,7 +170,7 @@ public:
// Constructors // Constructors
//- Copy construct from primitive //- Copy construct from primitive
explicit pTraits(const uint64_t& val); explicit pTraits(const uint64_t& val) noexcept;
//- Read construct from Istream //- Read construct from Istream
explicit pTraits(Istream& is); explicit pTraits(Istream& is);
@ -179,13 +179,13 @@ public:
// Member Functions // Member Functions
//- Access to the value //- Access to the value
operator uint64_t() const operator uint64_t() const noexcept
{ {
return p_; return p_;
} }
//- Access to the value //- Access to the value
operator uint64_t&() operator uint64_t&() noexcept
{ {
return p_; return p_;
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,7 +27,7 @@ License
#include "uint8.H" #include "uint8.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::pTraits<uint8_t>::typeName = "uint8"; const char* const Foam::pTraits<uint8_t>::typeName = "uint8";
const char* const Foam::pTraits<uint8_t>::componentNames[] = { "" }; const char* const Foam::pTraits<uint8_t>::componentNames[] = { "" };
@ -40,11 +40,14 @@ const uint8_t Foam::pTraits<uint8_t>::rootMin = 0;
const uint8_t Foam::pTraits<uint8_t>::rootMax = UINT8_MAX; const uint8_t Foam::pTraits<uint8_t>::rootMax = UINT8_MAX;
Foam::pTraits<uint8_t>::pTraits(const uint8_t& val) // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::pTraits<uint8_t>::pTraits(const uint8_t& val) noexcept
: :
p_(val) p_(val)
{} {}
Foam::pTraits<uint8_t>::pTraits(Istream& is) Foam::pTraits<uint8_t>::pTraits(Istream& is)
{ {
is >> p_; is >> p_;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -68,7 +68,7 @@ inline word name(const uint8_t val)
template<> template<>
struct nameOp<uint8_t> struct nameOp<uint8_t>
{ {
inline word operator()(const uint8_t val) const word operator()(const uint8_t val) const
{ {
return word(std::to_string(int(val)), false); // Needs no stripping return word(std::to_string(int(val)), false); // Needs no stripping
} }
@ -126,7 +126,7 @@ public:
// Constructors // Constructors
//- Copy construct from primitive //- Copy construct from primitive
explicit pTraits(const uint8_t& val); explicit pTraits(const uint8_t& val) noexcept;
//- Read construct from Istream //- Read construct from Istream
explicit pTraits(Istream& is); explicit pTraits(Istream& is);

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,7 +28,7 @@ Class
Foam::pTraits Foam::pTraits
Description Description
Traits class for primitives. A traits class, which is primarily used for primitives.
All primitives need a specialised version of this class. The All primitives need a specialised version of this class. The
specialised version will normally also require a conversion specialised version will normally also require a conversion
@ -51,25 +51,27 @@ class Istream;
Class pTraits Declaration Class pTraits Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
template<class PrimitiveType> // The base implementation is a pass-through to the base class.
// Accordingly it inherits all static methods (eg, typeName etc).
template<class Base>
class pTraits class pTraits
: :
public PrimitiveType public Base
{ {
public: public:
// Constructors // Constructors
//- Copy construct from primitive //- Copy construct from base class
explicit pTraits(const PrimitiveType& p) explicit pTraits(const Base& obj)
: :
PrimitiveType(p) Base(obj)
{} {}
//- Construct from Istream //- Construct from Istream
explicit pTraits(Istream& is) explicit pTraits(Istream& is)
: :
PrimitiveType(is) Base(is)
{} {}
}; };

View File

@ -231,7 +231,7 @@ word name(const void* ptr);
template<class T> template<class T>
struct nameOp struct nameOp
{ {
inline word operator()(const T& obj) const word operator()(const T& obj) const
{ {
return obj.name(); return obj.name();
} }
@ -242,7 +242,7 @@ struct nameOp
template<class T> template<class T>
struct typeOp struct typeOp
{ {
inline word operator()(const T& obj) const word operator()(const T& obj) const
{ {
return obj.type(); return obj.type();
} }