mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add tmp/refPtr support for setting cref/ref from pointer
- makes it easier to use for local or alternative storage.
Eg,
```
tmp<volScalarField> tfld;
tfld.cref(obj.cfindObject<volScalarField>("name"));
if (!tfld)
{
tfld = volScalarField::New("name", ...);
}
```
This commit is contained in:
@ -85,6 +85,12 @@ int main()
|
|||||||
ptr.reset(new scalarField(5, scalar(15)));
|
ptr.reset(new scalarField(5, scalar(15)));
|
||||||
tfld3.reset(std::move(ptr));
|
tfld3.reset(std::move(ptr));
|
||||||
printInfo(tfld3, true);
|
printInfo(tfld3, true);
|
||||||
|
|
||||||
|
|
||||||
|
ptr.reset(new scalarField(2, scalar(1)));
|
||||||
|
Info<< nl << "const-ref from pointer: " << name(ptr.get()) << nl;
|
||||||
|
tfld3.cref(ptr.get());
|
||||||
|
printInfo(tfld3, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Info<< "\nEnd" << endl;
|
Info<< "\nEnd" << endl;
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
\\ / A nd | www.openfoam.com
|
\\ / A nd | www.openfoam.com
|
||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Copyright (C) 2018-2020 OpenCFD Ltd.
|
Copyright (C) 2018-2021 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
|
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
|
||||||
@ -110,6 +110,12 @@ int main()
|
|||||||
Info<< "Reset to some other tmp content : ";
|
Info<< "Reset to some other tmp content : ";
|
||||||
printInfo(tfld2);
|
printInfo(tfld2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<scalarField> ptr(new scalarField(2, scalar(1)));
|
||||||
|
|
||||||
|
Info<< nl << "const-ref from pointer: " << name(ptr.get()) << nl;
|
||||||
|
tfld2.cref(ptr.get());
|
||||||
|
printInfo(tfld2);
|
||||||
}
|
}
|
||||||
|
|
||||||
Info<< "\nEnd" << endl;
|
Info<< "\nEnd" << endl;
|
||||||
|
|||||||
@ -215,12 +215,20 @@ public:
|
|||||||
//- Clear existing and transfer ownership.
|
//- Clear existing and transfer ownership.
|
||||||
inline void reset(refPtr<T>&& other) noexcept;
|
inline void reset(refPtr<T>&& other) noexcept;
|
||||||
|
|
||||||
//- Delete managed temporary object and set to (const) reference
|
//- Clear existing and set (const) reference
|
||||||
inline void cref(const T& obj) noexcept;
|
inline void cref(const T& obj) noexcept;
|
||||||
|
|
||||||
//- Delete managed temporary object and set to (non-const) reference
|
//- Clear existing and set (const) reference to pointer content.
|
||||||
|
// A null pointer is permitted (treated as a managed pointer).
|
||||||
|
inline void cref(const T* p) noexcept;
|
||||||
|
|
||||||
|
//- 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.
|
||||||
|
// A null pointer is permitted (treated as a managed pointer).
|
||||||
|
inline void ref(T* p) noexcept;
|
||||||
|
|
||||||
//- Swaps the managed object with other.
|
//- Swaps the managed object with other.
|
||||||
inline void swap(refPtr<T>& other) noexcept;
|
inline void swap(refPtr<T>& other) noexcept;
|
||||||
|
|
||||||
|
|||||||
@ -300,6 +300,15 @@ inline void Foam::refPtr<T>::cref(const T& obj) noexcept
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
inline void Foam::refPtr<T>::cref(const T* p) noexcept
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
ptr_ = const_cast<T*>(p);
|
||||||
|
type_ = (ptr_ ? CREF : PTR);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
inline void Foam::refPtr<T>::ref(T& obj) noexcept
|
inline void Foam::refPtr<T>::ref(T& obj) noexcept
|
||||||
{
|
{
|
||||||
@ -309,6 +318,15 @@ inline void Foam::refPtr<T>::ref(T& obj) noexcept
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
inline void Foam::refPtr<T>::ref(T* p) noexcept
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
ptr_ = p;
|
||||||
|
type_ = (ptr_ ? REF : PTR);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
inline void Foam::refPtr<T>::swap(refPtr<T>& other) noexcept
|
inline void Foam::refPtr<T>::swap(refPtr<T>& other) noexcept
|
||||||
{
|
{
|
||||||
|
|||||||
@ -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.
|
||||||
@ -224,12 +224,20 @@ public:
|
|||||||
//- 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 (const) reference
|
//- Clear existing and set (const) reference
|
||||||
inline void cref(const T& obj) noexcept;
|
inline void cref(const T& obj) noexcept;
|
||||||
|
|
||||||
//- Delete managed temporary object and set to (non-const) reference
|
//- Clear existing and set (const) reference to pointer content.
|
||||||
|
// A null pointer is permitted (treated as a managed pointer).
|
||||||
|
inline void cref(const T* p) noexcept;
|
||||||
|
|
||||||
|
//- 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.
|
||||||
|
// A null pointer is permitted (treated as a managed pointer).
|
||||||
|
inline void ref(T* p) noexcept;
|
||||||
|
|
||||||
//- Swaps the managed object with other.
|
//- Swaps the managed object with other.
|
||||||
inline void swap(tmp<T>& other) noexcept;
|
inline void swap(tmp<T>& other) noexcept;
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Copyright (C) 2011-2017 OpenFOAM Foundation
|
Copyright (C) 2011-2017 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.
|
||||||
@ -340,6 +340,15 @@ inline void Foam::tmp<T>::cref(const T& obj) noexcept
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
inline void Foam::tmp<T>::cref(const T* p) noexcept
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
ptr_ = const_cast<T*>(p);
|
||||||
|
type_ = (ptr_ ? CREF : PTR);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
inline void Foam::tmp<T>::ref(T& obj) noexcept
|
inline void Foam::tmp<T>::ref(T& obj) noexcept
|
||||||
{
|
{
|
||||||
@ -349,6 +358,15 @@ inline void Foam::tmp<T>::ref(T& obj) noexcept
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
inline void Foam::tmp<T>::ref(T* p) noexcept
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
ptr_ = p;
|
||||||
|
type_ = (ptr_ ? REF : PTR);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
|
inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user