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

156  {
157  static_cast<Internal*>(this)->count = 0;
158  weak_ref_ = nullptr;
159  }

◆ GenericSharedInternal() [2/2]

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

Definition at line 141 of file generic_shared_internal.hpp.

142  {
143  static_cast<Internal*>(this)->count = 0; // reference counter is _not_ copied
144  weak_ref_ = nullptr; // nor will they have the same weak references
145  }

◆ ~GenericSharedInternal()

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

Definition at line 168 of file generic_shared_internal.hpp.

168  {
169  #ifdef WITH_REFCOUNT_WARNINGS
170  if (static_cast<Internal*>(this)->count!=0) {
171  // Note that casadi_assert_warning cannot be used in destructors
172  std::cerr << "Reference counting failure." <<
173  "Possible cause: Circular dependency in user code." << std::endl;
174  }
175  #endif // WITH_REFCOUNT_WARNINGS
176  if (weak_ref_!=nullptr) {
177  // Assumption: no other GenericSharedInternal instances
178  // point to the same WeakRefInternal through weak_ref_
179  weak_ref_->kill();
180  delete weak_ref_;
181  weak_ref_ = nullptr;
182  }
183  }

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

162  {
163  // Note: i != this because of something something multiple inheritance
164  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
165  }
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 186 of file generic_shared_internal.hpp.

186  {
187  return static_cast<const Internal*>(this)->count;
188  }

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

150  {
151  // do _not_ copy the reference counter
152  return *this;
153  }

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

191  {
192  if (weak_ref_==nullptr) {
193  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
194  }
195  return weak_ref_;
196  }

Friends And Related Function Documentation

◆ GenericShared< Shared, Internal >

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

Definition at line 208 of file generic_shared_internal.hpp.


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