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

#include <generic_shared.hpp>

Detailed Description

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

Definition at line 61 of file generic_shared.hpp.

Inheritance diagram for casadi::GenericShared< Shared, Internal >:
Inheritance graph
[legend]

Public Member Functions

 GenericShared ()
 Default constructor. More...
 
 GenericShared (const GenericShared &ref)
 Copy constructor (shallow copy) More...
 
 ~GenericShared ()
 Destructor. More...
 
GenericSharedoperator= (const GenericShared &ref)
 Assignment operator. More...
 
void own (Internal *node)
 
void assign (Internal *node)
 Assign the node to a node class pointer without reference counting. More...
 
Internal * get () const
 Get a const pointer to the node. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
void swap (GenericShared &other)
 Swap content with another instance. More...
 
Internal * operator-> () const
 Access a member function or object. More...
 
std::string debug_repr () const
 
bool is_null () const
 Is a null pointer? More...
 
casadi_int __hash__ () const
 Returns a number that is unique for a given Node. More...
 
GenericWeakRef< Shared, Internal > * weak ()
 Get a weak reference to the object. More...
 

Protected Member Functions

void count_up ()
 
void count_down ()
 

Friends

template<class B , class S >
shared_cast (S &A)
 Typecast a shared object to a base class to a shared object to a derived class,. More...
 
template<class B , class S >
const B shared_cast (const S &A)
 Typecast a shared object to a base class to a shared object to a derived class,. More...
 

Constructor & Destructor Documentation

◆ GenericShared() [1/2]

template<typename Shared , typename Internal >
casadi::GenericShared< Shared, Internal >::GenericShared ( )
inline

Definition at line 70 of file generic_shared.hpp.

70  {
71  node = nullptr;
72  }

◆ GenericShared() [2/2]

template<typename Shared , typename Internal >
casadi::GenericShared< Shared, Internal >::GenericShared ( const GenericShared< Shared, Internal > &  ref)
inline

Definition at line 75 of file generic_shared.hpp.

75  {
76  node = ref.node;
77  count_up();
78  }

◆ ~GenericShared()

template<typename Shared , typename Internal >
casadi::GenericShared< Shared, Internal >::~GenericShared ( )
inline

Definition at line 81 of file generic_shared.hpp.

81  {
82  count_down();
83  }

Member Function Documentation

◆ __hash__()

template<typename Shared , typename Internal >
casadi_int casadi::GenericShared< Shared, Internal >::__hash__

If the Object does not point to any node, "0" is returned.

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

Definition at line 137 of file generic_shared_impl.hpp.

137  {
138  return reinterpret_cast<casadi_int>(get());
139  }
Internal * get() const
Get a const pointer to the node.

◆ assign()

template<typename Shared , typename Internal >
void casadi::GenericShared< Shared, Internal >::assign ( Internal *  node)

improper use will cause memory leaks!

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

Definition at line 75 of file generic_shared_impl.hpp.

75  {
76  node = node_;
77  }

◆ count_down()

template<typename Shared , typename Internal >
void casadi::GenericShared< Shared, Internal >::count_down
protected

Definition at line 42 of file generic_shared_impl.hpp.

42  {
43 #ifdef WITH_EXTRA_CHECKS
44  casadi_assert_dev(Function::call_depth_==0);
45 #endif // WITH_EXTRA_CHECKS
46  if (!node) return;
47  if (node->weak_ref_) {
48 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
49  auto mutex = node->weak_ref_->get_mutex();
50  // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
51  std::lock_guard<std::mutex> lock(*mutex);
52  // Could it be that this mutex is destroyed when the lock goes out of scope?
53 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
54 
55  if (--static_cast<Internal*>(node)->count == 0) {
56  delete node;
57  node = nullptr;
58  }
59  } else {
60  if (--static_cast<Internal*>(node)->count == 0) {
61  delete node;
62  node = nullptr;
63  }
64  }
65  }

◆ count_up()

template<typename Shared , typename Internal >
void casadi::GenericShared< Shared, Internal >::count_up
protected

Definition at line 32 of file generic_shared_impl.hpp.

32  {
33 #ifdef WITH_EXTRA_CHECKS
34  casadi_assert_dev(Function::call_depth_==0);
35 #endif // WITH_EXTRA_CHECKS
36 
37  if (node) static_cast<Internal*>(node)->count++;
38 
39  }

◆ debug_repr()

template<typename Shared , typename Internal >
std::string casadi::GenericShared< Shared, Internal >::debug_repr

Definition at line 80 of file generic_shared_impl.hpp.

80  {
81  if (node) {
82  return node->debug_repr(node);
83  } else {
84  return "NULL";
85  }
86  }

◆ get()

template<typename Shared , typename Internal >
Internal * casadi::GenericShared< Shared, Internal >::get

Definition at line 104 of file generic_shared_impl.hpp.

104  {
105  return node;
106  }

Referenced by casadi::CallSX::get_output().

◆ getCount()

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

Definition at line 127 of file generic_shared_impl.hpp.

127  {
128  return (*this)->getCount();
129  }

Referenced by casadi::MXNode::~MXNode().

◆ is_null()

template<typename Shared , typename Internal >
bool casadi::GenericShared< Shared, Internal >::is_null

Definition at line 109 of file generic_shared_impl.hpp.

109  {
110  return node==nullptr;
111  }

Referenced by casadi::FunctionInternal::add_embedded(), casadi::FunctionInternal::alloc(), casadi::Nlpsol::callback(), casadi::DaeBuilderInternal::clear_cache(), casadi::Switch::codegen_body(), casadi::Conic::Conic(), casadi::conic_debug(), casadi::DaeBuilderInternal::export_fmu(), casadi::FunctionInternal::finalize(), casadi::Switch::find(), casadi::FunctionInternal::FunctionInternal(), casadi::FunctionInternal::get_n_in(), casadi::Switch::get_n_in(), casadi::FunctionInternal::get_n_out(), casadi::Switch::get_n_out(), casadi::FunctionInternal::get_name_in(), casadi::FunctionInternal::get_name_out(), casadi::FunctionInternal::get_partition(), casadi::FunctionInternal::get_sparsity_in(), casadi::Switch::get_sparsity_in(), casadi::FunctionInternal::get_sparsity_out(), casadi::Switch::get_sparsity_out(), casadi::MXFunction::get_stats(), casadi::Integrator::init(), casadi::Nlpsol::init(), casadi::Rootfinder::init(), casadi::Switch::init(), casadi::SLEQPInterface::init(), casadi::SnoptInterface::init(), casadi::SundialsInterface::init(), casadi::FastNewton::init(), casadi::Newton::init(), casadi::Scpgen::init(), casadi::BonminInterface::intermediate_callback(), casadi::IpoptInterface::intermediate_callback(), casadi::XFunction< DerivedType, MatType, NodeType >::jac(), casadi::FunctionInternal::jac_is_symm(), casadi::FunctionInternal::jac_sparsity(), casadi::OracleFunction::jit_dependencies(), casadi::SparsityInternal::maxtrans(), casadi::BlazingSplineFunction::merge(), casadi::Function::operator==(), casadi::DaeBuilderInternal::oracle(), casadi::SLEQPInterface::set_work(), casadi::SnoptInterface::set_work(), casadi::OptiNode::solve(), casadi::KnitroInterface::solve(), casadi::SLEQPInterface::solve(), casadi::WorhpInterface::solve(), casadi::FunctionInternal::sp_forward_block(), casadi::FunctionInternal::sp_reverse(), and casadi::Sparsity::uni_coloring().

◆ operator->()

template<typename Shared , typename Internal >
Internal * casadi::GenericShared< Shared, Internal >::operator->

Definition at line 114 of file generic_shared_impl.hpp.

114  {
115  casadi_assert_dev(!is_null());
116  return node;
117  }
bool is_null() const
Is a null pointer?

Referenced by casadi::GenericWeakRef< Shared, Internal >::operator->().

◆ operator=()

template<typename Shared , typename Internal >
GenericShared< Shared, Internal > & casadi::GenericShared< Shared, Internal >::operator= ( const GenericShared< Shared, Internal > &  ref)

Definition at line 90 of file generic_shared_impl.hpp.

90  {
91  // quick return if the old and new pointers point to the same object
92  if (node == ref.node) return *this;
93 
94  // decrease the counter and delete if this was the last pointer
95  count_down();
96 
97  // save the new pointer
98  node = ref.node;
99  count_up();
100  return *this;
101  }

◆ own()

template<typename Shared , typename Internal >
void casadi::GenericShared< Shared, Internal >::own ( Internal *  node)

◆ swap()

template<typename Shared , typename Internal >
void casadi::GenericShared< Shared, Internal >::swap ( GenericShared< Shared, Internal > &  other)

Definition at line 120 of file generic_shared_impl.hpp.

120  {
121  GenericShared<Shared, Internal> temp = *this;
122  *this = other;
123  other = temp;
124  }

◆ weak()

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

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

Definition at line 132 of file generic_shared_impl.hpp.

132  {
133  return (*this)->weak();
134  }

Friends And Related Function Documentation

◆ shared_cast [1/2]

template<typename Shared , typename Internal >
template<class B , class S >
const B shared_cast ( const S &  A)
friend

cf. dynamic_cast (const)

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

Definition at line 231 of file generic_shared.hpp.

231  {
232  S A_copy = A;
233  return shared_cast<B, S>(A_copy);
234  }

◆ shared_cast [2/2]

template<typename Shared , typename Internal >
template<class B , class S >
B shared_cast ( S &  A)
friend

cf. dynamic_cast

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

Get a pointer to the node

Create a return object

Quick return if not allowed

Assign node of B and return

Definition at line 209 of file generic_shared.hpp.

209  {
210 
212  typename S::internal_base_type* ptr = A.get();
213 
215  B ret;
216 
218  if (!B::test_cast(ptr)) return ret;
219 
221  ret.own(ptr);
222  return ret;
223  }

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