List of all members | Public Member Functions | Protected Member Functions | Friends
casadi::GenericSharedInternal< Shared, Internal > Class Template Referenceabstract

#include <generic_shared_internal.hpp>

Detailed Description

template<typename Shared, typename Internal>
class casadi::GenericSharedInternal< Shared, Internal >

Internal class for the reference counting framework, see comments on the public class.

Definition at line 43 of file generic_shared_internal.hpp.

Public Member Functions

 GenericSharedInternal ()
 Default constructor. More...
 
 GenericSharedInternal (const GenericSharedInternal &node)
 Copy constructor. More...
 
GenericSharedInternal & operator= (const GenericSharedInternal &node)
 Assignment operator. More...
 
virtual ~GenericSharedInternal ()=0
 Destructor. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
std::string debug_repr (const Internal *) const
 
GenericWeakRef< Shared, Internal > * weak ()
 Get a weak reference to the object. More...
 

Protected Member Functions

void initSingleton ()
 
void destroySingleton ()
 
template<class B >
B shared_from_this ()
 Get a shared object from the current internal object. More...
 
template<class B >
const B shared_from_this () const
 Get a shared object from the current internal object. More...
 

Friends

class GenericShared< Shared, Internal >
 

Constructor & Destructor Documentation

◆ GenericSharedInternal() [1/2]

template<typename Shared , typename Internal >
casadi::GenericSharedInternal< Shared, Internal >::GenericSharedInternal

Definition at line 170 of file generic_shared_internal.hpp.

170  {
171  static_cast<Internal*>(this)->count = 0;
172 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
173  weak_ref_.store(nullptr, std::memory_order_relaxed);
174 #else
175  weak_ref_ = nullptr;
176 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
177  }

◆ GenericSharedInternal() [2/2]

template<typename Shared , typename Internal >
casadi::GenericSharedInternal< Shared, Internal >::GenericSharedInternal ( const GenericSharedInternal< Shared, Internal > &  node)

Definition at line 151 of file generic_shared_internal.hpp.

152  {
153  static_cast<Internal*>(this)->count = 0; // reference counter is _not_ copied
154 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
155  weak_ref_.store(nullptr, std::memory_order_relaxed); // nor same weak references
156 #else
157  weak_ref_ = nullptr; // nor will they have the same weak references
158 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
159  }

◆ ~GenericSharedInternal()

template<typename Shared , typename Internal >
casadi::GenericSharedInternal< Shared, Internal >::~GenericSharedInternal
pure virtual

Definition at line 186 of file generic_shared_internal.hpp.

186  {
187  #ifdef WITH_REFCOUNT_WARNINGS
188  if (static_cast<Internal*>(this)->count!=0) {
189  // Note that casadi_assert_warning cannot be used in destructors
190  std::cerr << "Reference counting failure." <<
191  "Possible cause: Circular dependency in user code." << std::endl;
192  }
193  #endif // WITH_REFCOUNT_WARNINGS
194 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
195  GenericWeakRef<Shared, Internal>* weak_ref =
196  weak_ref_.exchange(nullptr, std::memory_order_acq_rel);
197 #else
198  GenericWeakRef<Shared, Internal>* weak_ref = weak_ref_;
199  weak_ref_ = nullptr;
200 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
201  if (weak_ref != nullptr) {
202  // Assumption: no other GenericSharedInternal instances
203  // point to the same WeakRefInternal through weak_ref
204  weak_ref->kill();
205  delete weak_ref;
206  }
207  }

Member Function Documentation

◆ debug_repr()

template<typename Shared , typename Internal >
std::string casadi::GenericSharedInternal< Shared, Internal >::debug_repr ( const Internal *  i) const

Definition at line 180 of file generic_shared_internal.hpp.

180  {
181  // Note: i != this because of something something multiple inheritance
182  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
183  }
std::string str(const T &v)
String representation, any type.

References casadi::str().

◆ destroySingleton()

template<typename Shared , typename Internal >
void casadi::GenericSharedInternal< Shared, Internal >::destroySingleton ( )
inlineprotected

Called in the destructor of singletons

Definition at line 77 of file generic_shared_internal.hpp.

77  {
78  static_cast<Internal*>(this)->count--;
79  }

◆ getCount()

template<typename Shared , typename Internal >
casadi_int casadi::GenericSharedInternal< Shared, Internal >::getCount

Definition at line 210 of file generic_shared_internal.hpp.

210  {
211  return static_cast<const Internal*>(this)->count;
212  }

◆ initSingleton()

template<typename Shared , typename Internal >
void casadi::GenericSharedInternal< Shared, Internal >::initSingleton ( )
inlineprotected

Called in the constructor of singletons to avoid that the counter reaches zero

Definition at line 71 of file generic_shared_internal.hpp.

71  {
72  casadi_assert_dev(static_cast<Internal*>(this)->count==0);
73  static_cast<Internal*>(this)->count++;
74  }

◆ operator=()

template<typename Shared , typename Internal >
GenericSharedInternal< Shared, Internal > & casadi::GenericSharedInternal< Shared, Internal >::operator= ( const GenericSharedInternal< Shared, Internal > &  node)

Definition at line 163 of file generic_shared_internal.hpp.

164  {
165  // do _not_ copy the reference counter
166  return *this;
167  }

◆ shared_from_this() [1/2]

template<typename Shared , typename Internal >
template<class B >
B casadi::GenericSharedInternal< Shared, Internal >::shared_from_this ( )
inlineprotected

Definition at line 83 of file generic_shared_internal.hpp.

83  {
84  casadi_assert_dev(B::test_cast(static_cast<Internal*>(this)));
85  B ret;
86  ret.own(static_cast<Internal*>(this));
87  return ret;
88  }

◆ shared_from_this() [2/2]

template<typename Shared , typename Internal >
template<class B >
const B casadi::GenericSharedInternal< Shared, Internal >::shared_from_this ( ) const
inlineprotected

Definition at line 92 of file generic_shared_internal.hpp.

92  {
93  casadi_assert_dev(B::test_cast(static_cast<const Internal*>(this)));
94  B ret;
95  ret.own(const_cast<Internal*>(static_cast<const Internal*>(this)));
96  return ret;
97  }

◆ weak()

template<typename Shared , typename Internal >
GenericWeakRef< Shared, Internal > * casadi::GenericSharedInternal< Shared, Internal >::weak

Extra doc: https://github.com/casadi/casadi/wiki/L_1ai

Definition at line 215 of file generic_shared_internal.hpp.

215  {
216 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
217  auto* w = weak_ref_.load(std::memory_order_acquire);
218  if (!w) {
219  auto* nw = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
220  GenericWeakRef<Shared, Internal>* expected = nullptr;
221  if (weak_ref_.compare_exchange_strong(
222  expected, nw, std::memory_order_release, std::memory_order_acquire)) {
223  w = nw;
224  } else {
225  delete nw; // lost the race; another thread published first
226  w = expected;
227  }
228  }
229  return w;
230 #else
231  if (weak_ref_==nullptr) {
232  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
233  }
234  return weak_ref_;
235 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
236  }

Friends And Related Function Documentation

◆ GenericShared< Shared, Internal >

template<typename Shared , typename Internal >
friend class GenericShared< Shared, Internal >
friend

Definition at line 248 of file generic_shared_internal.hpp.


The documentation for this class was generated from the following files: