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...
 
GenericSharedInternaloperator= (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 >
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 165 of file generic_shared_internal.hpp.

165  {
166  static_cast<Internal*>(this)->count = 0;
167 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
168  weak_ref_.store(nullptr, std::memory_order_relaxed);
169 #else
170  weak_ref_ = nullptr;
171 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
172  }

◆ GenericSharedInternal() [2/2]

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

Definition at line 146 of file generic_shared_internal.hpp.

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

◆ ~GenericSharedInternal()

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

Definition at line 181 of file generic_shared_internal.hpp.

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

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 175 of file generic_shared_internal.hpp.

175  {
176  // Note: i != this because of something something multiple inheritance
177  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
178  }
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 205 of file generic_shared_internal.hpp.

205  {
206  return static_cast<const Internal*>(this)->count;
207  }

◆ 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 158 of file generic_shared_internal.hpp.

159  {
160  // do _not_ copy the reference counter
161  return *this;
162  }

◆ 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 210 of file generic_shared_internal.hpp.

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

Friends And Related Function Documentation

◆ GenericShared< Shared, Internal >

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

Definition at line 243 of file generic_shared_internal.hpp.


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