Geant4  10.01.p01
G4memory.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 #ifndef G4SHARED_PTR_HH_
27 #define G4SHARED_PTR_HH_
28 
29 #include "CLHEP/Utility/memory.h"
30 
31 #define G4shared_ptr G4::shared_ptr
32 #define G4weak_ptr G4::weak_ptr
33 #define G4static_pointer_cast G4::static_pointer_cast
34 #define G4const_pointer_cast G4::const_pointer_cast
35 #define G4dynamic_pointer_cast G4::dynamic_pointer_cast
36 #define G4enable_shared_from_this G4::enable_shared_from_this
37 #define G4enable_shared_from_this2 G4::enable_shared_from_this2
38 
39 namespace G4
40 {
41 using CLHEP::shared_ptr;
42 using CLHEP::weak_ptr;
43 using CLHEP::static_pointer_cast;
44 using CLHEP::const_pointer_cast;
45 using CLHEP::dynamic_pointer_cast;
46 }
47 
48 namespace CLHEP
49 {
50 
51 template< typename T >
53 {
54 protected:
56 { }
57 
59  { }
60 
62  { }
63 
66  {
67  return *this;
68  }
69 
70 public:
71  shared_ptr<T>
73  {
74  shared_ptr<T> p( weak_this_ );
75  assert( p.get() == this );
76  return p;
77  }
78 
79  shared_ptr<T const>
81  {
82  shared_ptr<T const> p( weak_this_ );
83  assert( p.get() == this );
84  return p;
85  }
86 
87 public: // actually private, but avoids compiler template friendship issues
88 
89  // Note: invoked automatically by shared_ptr; do not call
90  template< typename X, typename Y >
91  void
92  _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const
93  {
94  if( weak_this_.expired() )
95  weak_this_ = shared_ptr<T>( *ppx, py );
96  }
97 
98 private:
99  mutable weak_ptr<T> weak_this_;
100 }; // enable_shared_from_this<>
101 
102 
103 // enable_shared_from_this2.hpp
104 
105 namespace detail
106 {
107 
109 {
110 private:
111  shared_ptr<void> deleter_;
112 
113 public:
115 
116  template< typename T >
117  void
118  set_deleter( shared_ptr<T> const & deleter )
119  {
120  deleter_ = deleter;
121  }
122 
123  template< typename T >
124  void
126  {
127  assert( deleter_.use_count() <= 1 );
128  deleter_.reset();
129  }
130 };
131 
132 } // namespace detail
133 
134 template< typename T >
136 {
137 protected:
138 
140 
142  { }
143 
145  {
146  return *this;
147  }
148 
150  {
151  assert( shared_this_.use_count() <= 1 ); // ensure no dangling shared_ptrs
152  }
153 
154 private:
155  mutable weak_ptr<T> weak_this_;
156  mutable shared_ptr<T> shared_this_;
157 
158 public:
159 
160  shared_ptr<T>
162  {
163  init_weak_once();
164  return shared_ptr<T>( weak_this_ );
165  }
166 
167  shared_ptr<T const>
169  {
170  init_weak_once();
171  return shared_ptr<T>( weak_this_ );
172  }
173 
174 private:
175 
176  void init_weak_once() const
177  {
178  if( weak_this_._empty() )
179  {
180  shared_this_.reset( static_cast< T* >( 0 )
182  );
183  weak_this_ = shared_this_;
184  }
185  }
186 
187 public: // actually private, but avoids compiler template friendship issues
188 
189 // Note: invoked automatically by shared_ptr; do not call
190  template< typename X, typename Y >
191  void
192  _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const
193  {
194  assert( ppx != 0 );
195 
196  if( weak_this_.use_count() == 0 )
197  weak_this_ = shared_ptr<T>( *ppx, py );
198  else if( shared_this_.use_count() != 0 )
199  {
200  assert( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
201 
203  = //boost::
204  get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
205  assert( pd != 0 );
206 
207  pd->set_deleter( *ppx );
208 
209  ppx->reset( shared_this_, ppx->get() );
210  shared_this_.reset();
211  }
212  }
213 }; // enable_shared_from_this2<>
214 }
215 
216 namespace G4
217 {
220 }
221 
222 #endif /* G4SHARED_PTR_HH_ */
void set_deleter(shared_ptr< T > const &deleter)
Definition: G4memory.hh:118
enable_shared_from_this2 & operator=(enable_shared_from_this2 const &)
Definition: G4memory.hh:144
enable_shared_from_this & operator=(enable_shared_from_this const &)
Definition: G4memory.hh:65
Definition: G4memory.hh:39
shared_ptr< T const > shared_from_this() const
Definition: G4memory.hh:168
enable_shared_from_this2(enable_shared_from_this2 const &)
Definition: G4memory.hh:141
shared_ptr< T const > shared_from_this() const
Definition: G4memory.hh:80
shared_ptr< T > shared_from_this()
Definition: G4memory.hh:72
shared_ptr< T > shared_from_this()
Definition: G4memory.hh:161
enable_shared_from_this(enable_shared_from_this const &)
Definition: G4memory.hh:61
void _internal_accept_owner(shared_ptr< X > *ppx, Y *py) const
Definition: G4memory.hh:192
void _internal_accept_owner(shared_ptr< X > const *ppx, Y *py) const
Definition: G4memory.hh:92
shared_ptr< T > shared_this_
Definition: G4memory.hh:156