Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 //#if __cplusplus >= 201103L
30 #include <memory>
31 
32 #define G4shared_ptr std::shared_ptr
33 #define G4weak_ptr std::weak_ptr
34 #define G4static_pointer_cast std::static_pointer_cast
35 #define G4const_pointer_cast std::const_pointer_cast
36 #define G4dynamic_pointer_cast std::dynamic_pointer_cast
37 #define G4enable_shared_from_this std::enable_shared_from_this
38 #define G4enable_shared_from_this2 std::enable_shared_from_this
39 /*
40 #else
41 #include "CLHEP/Utility/memory.h"
42 
43 #define G4shared_ptr G4::shared_ptr
44 #define G4weak_ptr G4::weak_ptr
45 #define G4static_pointer_cast G4::static_pointer_cast
46 #define G4const_pointer_cast G4::const_pointer_cast
47 #define G4dynamic_pointer_cast G4::dynamic_pointer_cast
48 #define G4enable_shared_from_this G4::enable_shared_from_this
49 #define G4enable_shared_from_this2 G4::enable_shared_from_this2
50 
51 namespace G4
52 {
53 using CLHEP::shared_ptr;
54 using CLHEP::weak_ptr;
55 using CLHEP::static_pointer_cast;
56 using CLHEP::const_pointer_cast;
57 using CLHEP::dynamic_pointer_cast;
58 }
59 
60 namespace CLHEP
61 {
62 
63 template< typename T >
64 class enable_shared_from_this
65 {
66 protected:
67  enable_shared_from_this()
68 { }
69 
70  ~enable_shared_from_this()
71  { }
72 
73  enable_shared_from_this( enable_shared_from_this const & )
74  { }
75 
76  enable_shared_from_this &
77  operator = ( enable_shared_from_this const & )
78  {
79  return *this;
80  }
81 
82 public:
83  shared_ptr<T>
84  shared_from_this()
85  {
86  shared_ptr<T> p( weak_this_ );
87  // assert( p.get() == this );
88  return p;
89  }
90 
91  shared_ptr<T const>
92  shared_from_this() const
93  {
94  shared_ptr<T const> p( weak_this_ );
95  // assert( p.get() == this );
96  return p;
97  }
98 
99 public: // actually private, but avoids compiler template friendship issues
100 
101  // Note: invoked automatically by shared_ptr; do not call
102  template< typename X, typename Y >
103  void
104  _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const
105  {
106  if( weak_this_.expired() )
107  weak_this_ = shared_ptr<T>( *ppx, py );
108  }
109 
110 private:
111  mutable weak_ptr<T> weak_this_;
112 }; // enable_shared_from_this<>
113 
114 
115 // enable_shared_from_this2.hpp
116 
117 namespace detail
118 {
119 
120 class esft2_deleter_wrapper
121 {
122 private:
123  shared_ptr<void> deleter_;
124 
125 public:
126  esft2_deleter_wrapper(){ }
127 
128  template< typename T >
129  void
130  set_deleter( shared_ptr<T> const & deleter )
131  {
132  deleter_ = deleter;
133  }
134 
135  template< typename T >
136  void
137  operator () ( T* )
138  {
139  assert( deleter_.use_count() <= 1 );
140  deleter_.reset();
141  }
142 };
143 
144 } // namespace detail
145 
146 template< typename T >
147 class enable_shared_from_this2
148 {
149 protected:
150 
151  enable_shared_from_this2(){ }
152 
153  enable_shared_from_this2( enable_shared_from_this2 const & )
154  { }
155 
156  enable_shared_from_this2 & operator = ( enable_shared_from_this2 const & )
157  {
158  return *this;
159  }
160 
161  ~enable_shared_from_this2()
162  {
163  assert( shared_this_.use_count() <= 1 ); // ensure no dangling shared_ptrs
164  }
165 
166 private:
167  mutable weak_ptr<T> weak_this_;
168  mutable shared_ptr<T> shared_this_;
169 
170 public:
171 
172  shared_ptr<T>
173  shared_from_this()
174  {
175  init_weak_once();
176  return shared_ptr<T>( weak_this_ );
177  }
178 
179  shared_ptr<T const>
180  shared_from_this() const
181  {
182  init_weak_once();
183  return shared_ptr<T>( weak_this_ );
184  }
185 
186 private:
187 
188  void init_weak_once() const
189  {
190  if( weak_this_._empty() )
191  {
192  shared_this_.reset( static_cast< T* >( 0 )
193  , detail::esft2_deleter_wrapper()
194  );
195  weak_this_ = shared_this_;
196  }
197  }
198 
199 public: // actually private, but avoids compiler template friendship issues
200 
201 // Note: invoked automatically by shared_ptr; do not call
202  template< typename X, typename Y >
203  void
204  _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const
205  {
206  assert( ppx != 0 );
207 
208  if( weak_this_.use_count() == 0 )
209  weak_this_ = shared_ptr<T>( *ppx, py );
210  else if( shared_this_.use_count() != 0 )
211  {
212  assert( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
213 
214  detail::esft2_deleter_wrapper * pd
215  = //boost::
216  get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
217  assert( pd != 0 );
218 
219  pd->set_deleter( *ppx );
220 
221  ppx->reset( shared_this_, ppx->get() );
222  shared_this_.reset();
223  }
224  }
225 }; // enable_shared_from_this2<>
226 }
227 
228 namespace G4
229 {
230  using CLHEP::enable_shared_from_this;
231  using CLHEP::enable_shared_from_this2;
232 }
233 #endif
234 */
235 
236 #endif /* G4SHARED_PTR_HH_ */