AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
SharedPtr.hpp
1#pragma once
2
3#include <memory>
4#include <type_traits>
5#include <utility>
6
7#include <dune/common/shared_ptr.hh>
8
9#include <amdis/common/TypeTraits.hpp>
10
11namespace AMDiS
12{
21 template <class T>
22 std::shared_ptr<T> wrap_or_share(T& t)
23 {
24 return Dune::stackobject_to_shared_ptr(t);
25 }
26
27 template<class T>
28 std::shared_ptr<T> wrap_or_share(T&& t)
29 {
30 return std::make_shared<T>(FWD(t));
31 }
32
33 template <class T>
34 std::shared_ptr<T> wrap_or_share(T* t)
35 {
36 static_assert(not std::is_pointer_v<T*>,
37 "Raw pointers must be wrapped into smart pointers or references to clarify ownership");
38 }
39
40 template <class T>
41 std::shared_ptr<T> wrap_or_share(std::shared_ptr<T> t)
42 {
43 return t;
44 }
45
46 template <class T>
47 std::shared_ptr<T> wrap_or_share(std::unique_ptr<T> t)
48 {
49 return std::shared_ptr<T>(std::move(t));
50 }
51
52} // end namespace AMDiS