DUNE-FEM (unstable)

memory.hh
1#ifndef DUNE_FEM_COMMON_MEMORY_HH
2#define DUNE_FEM_COMMON_MEMORY_HH
3
4#include <type_traits>
5#include <memory>
6
8
9namespace Dune
10{
11
12 namespace Fem
13 {
14
15 // referenceToSharedPtr
16 // --------------------
17
18 template< class T, std::enable_if_t< !std::is_base_of< std::enable_shared_from_this< std::decay_t< T > >, std::decay_t< T > >::value, int > = 0 >
19 inline static std::shared_ptr< T > referenceToSharedPtr ( T &t )
20 {
21 return std::shared_ptr< T >( &t, Dune::null_deleter< T >() );
22 }
23
24 template< class T, std::enable_if_t< std::is_base_of< std::enable_shared_from_this< std::decay_t< T > >, std::decay_t< T > >::value, int > = 0 >
25 inline static std::shared_ptr< T > referenceToSharedPtr ( T &t )
26 try
27 {
28 // obtain internal weak pointer of enable_shared_from_this class.
29 // if the obtained internal weak_ptr of the enable_shared_from_this object is empty
30 // then no previously created shared_ptr exists and we create a phony one using the null_deleter
31 if( t.weak_from_this().use_count() == 0 )
32 {
33 return std::shared_ptr< T >( &t, Dune::null_deleter< T >() );
34 }
35 else
36 {
37 // return a shared pointer with the same count from the previously
38 // created shared_ptr
39 return t.shared_from_this();
40 }
41 }
42 // std::bad_weak_ptr is for example thrown when a shared_ptr
43 // is created from and empty weak_ptr
44 catch( std::bad_weak_ptr& )
45 {
46 return std::shared_ptr< T >( &t, Dune::null_deleter< T >() );
47 }
48
49 } // namespace Fem
50
51} // namespace Dune
52
53#endif // #ifndef DUNE_FEM_COMMON_MEMORY_HH
Dune namespace.
Definition: alignedallocator.hh:13
This file implements several utilities related to std::shared_ptr.
implements the Deleter concept of shared_ptr without deleting anything
Definition: shared_ptr.hh:49
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)