Add a CopyablePtr class

Adds a template class that wraps a std::unique_ptr and makes it
copyable. See pull #714 in opm-models for a use case.
This commit is contained in:
Håkon Hægland 2022-09-20 01:30:16 +02:00
parent 9e143482cf
commit 5086d5d74e
3 changed files with 139 additions and 0 deletions

View File

@ -365,6 +365,7 @@ list (APPEND TEST_SOURCE_FILES
if(ENABLE_ECL_INPUT)
list(APPEND TEST_SOURCE_FILES
tests/rst_test.cpp
tests/test_CopyablePtr.cpp
tests/test_ERsm.cpp
tests/test_GuideRate.cpp
tests/test_RestartFileView.cpp
@ -988,6 +989,7 @@ if(ENABLE_ECL_OUTPUT)
opm/output/eclipse/WriteRPT.hpp
opm/output/eclipse/WriteRestartHelpers.hpp
opm/output/OutputWriter.hpp
opm/utility/CopyablePtr.hpp
opm/utility/EModel.hpp
)
endif()

View File

@ -0,0 +1,71 @@
/*
Copyright 2022 Equinor ASA.
This file is part of the Open Porous Media project (OPM).
OPM 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.
OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_COPYABLE_PTR_HPP
#define OPM_COPYABLE_PTR_HPP
namespace Opm {
namespace Utility {
// Wraps std::unique_ptr and makes it copyable.
//
// WARNING: This template should not be used with polymorphic classes.
// That would require a virtual clone() method to be implemented.
// It will only ever copy the static class type of the pointed to class.
template <class T>
class CopyablePtr {
public:
CopyablePtr() : ptr_(nullptr) {}
CopyablePtr(const CopyablePtr& other) {
if (other) { // other does not contain a nullptr
ptr_ = std::make_unique<T>(*other.get());
}
else {
ptr_ = nullptr;
}
}
// assignment operator
CopyablePtr<T>& operator=(const CopyablePtr<T>& other) {
if (other) {
ptr_ = std::make_unique<T>(*other.get());
}
else {
ptr_ = nullptr;
}
return *this;
}
// assign directly from a unique_ptr
CopyablePtr<T>& operator=(std::unique_ptr<T>&& uptr) {
ptr_ = std::move(uptr);
return *this;
}
// member access operator
T* operator->() const {return ptr_.get(); }
// boolean context operator
explicit operator bool() const noexcept {
return ptr_ ? true : false;
}
// get a pointer to the stored value
T* get() const {return ptr_.get();}
T* release() const {return ptr_.release();}
private:
std::unique_ptr<T> ptr_;
};
} // namespace Utility
} // namespace Opm
#endif

View File

@ -0,0 +1,66 @@
/*
Copyright 2022 Equinor ASA.
This file is part of the Open Porous Media project (OPM).
OPM 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.
OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#define BOOST_TEST_MODULE CopyablePtrTest
#include <boost/test/unit_test.hpp>
#include <opm/utility/CopyablePtr.hpp>
#include <memory>
namespace {
template <class T>
class A {
using CopyablePtr = Opm::Utility::CopyablePtr<T>;
public:
A() : aptr_{} {}
void assign(T value) {
aptr_ = std::make_unique<T>(value);
}
const CopyablePtr& get_aptr() const {return aptr_;}
private:
CopyablePtr aptr_;
};
struct B {
double a;
int b;
int getb() {return b;}
};
} // namespace
BOOST_AUTO_TEST_SUITE ()
BOOST_AUTO_TEST_CASE (copyable)
{
A<B> a1 {};
a1.assign(B{1.1,2});
BOOST_CHECK_MESSAGE(a1.get_aptr().get()->a == 1.1, "Able to assign new value to pointer");
A<B> a2 = a1;
BOOST_CHECK_MESSAGE(a2.get_aptr().get()->a == 1.1, "Able to copy construct a new pointer");
a1.assign(B{1.3,3});
BOOST_CHECK_MESSAGE(a1.get_aptr().get()->a == 1.3, "Able to access new value to pointer");
BOOST_CHECK_MESSAGE(a2.get_aptr().get()->b == 2, "Copied value not affected by parent modification");
BOOST_CHECK_MESSAGE(a1.get_aptr().get()->getb() == 3, "Member access operator works");
BOOST_CHECK_MESSAGE(a1.get_aptr(), "Boolean context operator works");
}
BOOST_AUTO_TEST_SUITE_END()