00001 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 00002 // vi: set et ts=4 sw=2 sts=2: 00003 00004 #ifndef DUNE_SHARED_PTR_HH 00005 #define DUNE_SHARED_PTR_HH 00006 00007 #include <memory> 00008 00009 #include <dune/common/typetraits.hh> 00016 namespace Dune 00017 { 00018 // pull in default implementations 00019 using std::shared_ptr; 00020 using std::make_shared; 00021 00050 template<class T> 00051 struct null_deleter 00052 { 00053 void operator() (T*) const {} 00054 }; 00055 00074 template<typename T> 00075 inline shared_ptr<T> stackobject_to_shared_ptr(T & t) 00076 { 00077 return shared_ptr<T>(&t, null_deleter<T>()); 00078 } 00079 00103 template<typename T, typename T2> 00104 inline shared_ptr<T2> stackobject_to_shared_ptr(T & t) 00105 { 00106 return shared_ptr<T2>(dynamic_cast<T2*>(&t), null_deleter<T2>()); 00107 } 00108 00109 00127 template<class T> 00128 auto wrap_or_move(T&& t) 00129 { 00130 return std::make_shared<std::decay_t<T>>(std::forward<T>(t)); 00131 } 00132 00150 template<class T> 00151 auto wrap_or_move(T& t) 00152 { 00153 return stackobject_to_shared_ptr(t); 00154 } 00155 00156 } 00157 #endif