Skip to content
Snippets Groups Projects
Commit e66aba4c authored by Simon Praetorius's avatar Simon Praetorius
Browse files

Simplify ToUniquePtr deprecation

parent f01a1041
Branches
Tags
1 merge request!912Deprecate ToUniquePtr.
Pipeline #35352 passed
......@@ -392,10 +392,6 @@ dune_add_test(SOURCES transposetest.cc
LINK_LIBRARIES dunecommon
LABELS quick)
dune_add_test(SOURCES to_unique_ptrtest.cc
LINK_LIBRARIES dunecommon
LABELS quick)
dune_add_test(SOURCES tupleutilitytest.cc
LABELS quick)
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <dune/common/deprecated.hh>
#include <dune/common/to_unique_ptr.hh>
#include <dune/common/test/testsuite.hh>
int* f_old() { return new int(0); }
Dune::ToUniquePtr<int> f1() { return new int(1); }
auto f2() { return Dune::makeToUnique<int>(1); }
struct A { double x = 1.0; };
Dune::ToUniquePtr<A> g() { return new A{}; }
int main()
{
using namespace Dune;
TestSuite t;
DUNE_NO_DEPRECATED_BEGIN
{
int* ptr = new int(1);
// test constructor
ToUniquePtr<int> w1( ptr );
// test assignment from makeToUnique
ToUniquePtr<int> w2 = makeToUnique<int>(2);
// test conversion to pointer
int* p1 = f1();
A* p2 = g();
delete p1;
delete p2;
// test conversion to unique_ptr
{
std::unique_ptr<int> u1 = f1();
std::unique_ptr<int> u2 = f2();
std::unique_ptr<A> u3 = g();
}
// test conversion to shared_ptr
{
std::shared_ptr<int> s1 = f1();
std::shared_ptr<A> s2 = g();
std::shared_ptr<int> s3 = w2;
t.check(!bool(w2)) << "w2 should be invalidated";
}
// test move assignment
{
ToUniquePtr<int> w3( new int(3) );
w1 = std::move(w3);
t.check(!bool(w3)) << "w3 should be invalidated after move assignment";
}
t.check(bool(w1)) << "w1 should not be invalidated";
// test move construction
{
ToUniquePtr<int> w4( std::move(w1) );
t.check(!bool(w1)) << "w1 should be invalidated after move construction";
}
// test management of ownership in ToUniquePtr
{
auto w5 = makeToUnique<int>(5); // should free at the end of scope
}
// test unique_ptr-like interface of ToUniquePtr
auto w6 = makeToUnique<int>(6);
t.check(*w6 == 6) << "Access to value of ToUniquePtr";
w6.reset(new int(7));
t.check(*w6 == 7) << "Access to value of ToUniquePtr after reset";
w6.reset();
t.check(!bool(w6)) << "w6 should be invalidated";
}
std::unique_ptr<int> x0{ f_old() };
std::unique_ptr<int> x1{ f1() };
std::unique_ptr<int> x2 = f1();
DUNE_NO_DEPRECATED_END
return t.exit();
}
......@@ -4,101 +4,23 @@
#ifndef DUNE_TO_UNIQUE_PTR_HH
#define DUNE_TO_UNIQUE_PTR_HH
#warning to_unique_ptr.hh and ToUniquePtr is deprecated. Use std::unique_ptr or std::shared_ptr instead.
#warning to_unique_ptr.hh and ToUniquePtr are deprecated. Use std::unique_ptr or std::shared_ptr instead.
#include <memory>
namespace Dune
{
/// \brief An owning pointer wrapper that can be assigned to (smart) pointers. Cannot be copied.
/// Transfers ownership by cast to any (smart) pointer type. Releases the stored pointer on transfer.
/// NOTE: This is an intermediate solution to switch to std::unique_ptr in later releases smoothly.
/**
* \deprecated ToUniquePtr is deprecated and will be removed after Dune 2.8.
* Use std::unique_ptr or std::shared_ptr instead.
*
* Example of usage:
* ```
* ToUniquePtr<int> f() { return new int(1); }
* auto g() { return makeToUnique<int>(2); }
*
* int* p1 = f(); // p1 gets ownership, must delete explicitly
* delete p1;
*
* std::unique_ptr<int> p2 = f();
* std::shared_ptr<int> p3 = f();
*
* auto p4 = f(); // ToUniquePtr has itself pointer semantic
* std::cout << *p4 << '\n';
*
* std::unique_ptr<int> p5( g() );
* ```
**/
/// \brief Alias for `std::unique_ptr` introduced as transition wrapper.
/// \deprecated
template <class T>
class
[[deprecated("Will be removed after Dune 2.8. Use std::unique_ptr or std::shared_ptr instead.")]]
ToUniquePtr
: public std::unique_ptr<T>
{
using Super = std::unique_ptr<T>;
public:
// Member types:
//@{
using pointer = typename Super::pointer;
//@}
public:
// Constructors:
//@{
/// Constructor, stores the pointer.
ToUniquePtr(pointer ptr = pointer()) noexcept
: Super(ptr)
{}
/// Constructor, creates a `nullptr`
ToUniquePtr(std::nullptr_t) noexcept
: Super(nullptr)
{}
//@}
public:
// Conversion operators:
//@{
/// Convert to underlying pointer, releases the stored pointer.
/// \deprecated Cast to raw pointer is deprecated. Use std::unique_ptr or std::shared_ptr instead.
/// Will be removed after Dune 2.8
[[deprecated("Cast to raw pointer is deprecated. Use std::unique_ptr or std::shared_ptr instead.")]]
operator pointer() noexcept { return Super::release(); }
/// Convert to unique_ptr, invalidates the stored pointer
operator std::unique_ptr<T>() noexcept { return std::move(static_cast<Super&>(*this)); }
/// Convert to shared_ptr, invalidates the stored pointer
operator std::shared_ptr<T>() noexcept { return std::move(static_cast<Super&>(*this)); }
/// Checks whether *this owns an object
explicit operator bool() noexcept { return bool(static_cast<Super&>(*this)); }
/// Checks whether *this owns an object
explicit operator bool() const noexcept { return bool(static_cast<Super const&>(*this)); }
//@}
};
using ToUniquePtr [[deprecated]] = std::unique_ptr<T>;
/// Constructs an object of type T and wraps it in a ToUniquePtr, \relates ToUniquePtr
/// \brief Alias for `std::make_unique` introduced as transition wrapper.
/// \deprecated
template <class T, class... Args>
ToUniquePtr<T> makeToUnique(Args&&... args)
[[deprecated]] std::unique_ptr<T> makeToUnique (Args&&... args)
{
return {new T(std::forward<Args>(args)...)};
return std::make_unique(std::forward<Args>(args)...);
}
} // end namespace Dune
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment