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

#include <generic_shared.hpp>

Detailed Description

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

Definition at line 141 of file generic_shared.hpp.

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

Public Member Functions

 GenericWeakRef (int dummy=0)
 Default constructor. More...
 
 GenericWeakRef (Shared shared)
 Construct from a shared object (also implicit type conversion) More...
 
Shared shared () const
 Get a shared (owning) reference. More...
 
bool alive () const
 Check if alive. More...
 
bool shared_if_alive (Shared &shared) const
 Thread-safe alternative to alive()/shared() More...
 
GenericWeakRefInternal< Shared, Internal > * operator-> ()
 Access functions of the node. More...
 
const GenericWeakRefInternal< Shared, Internal > * operator-> () const
 Const access functions of the node. 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...
 
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

class GenericSharedInternal< Shared, Internal >
 

Constructor & Destructor Documentation

◆ GenericWeakRef() [1/2]

template<typename Shared , typename Internal >
casadi::GenericWeakRef< Shared, Internal >::GenericWeakRef ( int  dummy = 0)

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

Definition at line 153 of file generic_shared_impl.hpp.

153  {
154  casadi_assert_dev(dummy==0);
155  }

◆ GenericWeakRef() [2/2]

template<typename Shared , typename Internal >
casadi::GenericWeakRef< Shared, Internal >::GenericWeakRef ( Shared  shared)

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

Definition at line 200 of file generic_shared_impl.hpp.

200  {
201  this->own(shared.weak()->get());
202  }
void own(Internal *node)
Shared shared() const
Get a shared (owning) reference.

Member Function Documentation

◆ __hash__()

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

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 148 of file generic_shared_impl.hpp.

148  {
149  return reinterpret_cast<casadi_int>(get());
150  }
Internal * get() const
Get a const pointer to the node.

◆ alive()

template<typename Shared , typename Internal >
bool casadi::GenericWeakRef< Shared, Internal >::alive

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

Definition at line 158 of file generic_shared_impl.hpp.

158  {
159  return !is_null() && (*this)->raw_ != nullptr;
160  }
bool is_null() const
Is a null pointer?

◆ assign()

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

improper use will cause memory leaks!

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

Definition at line 86 of file generic_shared_impl.hpp.

86  {
87  node = node_;
88  }

◆ count_down()

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

Definition at line 46 of file generic_shared_impl.hpp.

46  {
47 #ifdef WITH_EXTRA_CHECKS
48  casadi_assert_dev(Function::call_depth_==0);
49 #endif // WITH_EXTRA_CHECKS
50  if (!node) return;
51 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
52  GenericWeakRef<Shared, Internal>* weak_ref =
53  node->weak_ref_.load(std::memory_order_acquire);
54 #else
55  GenericWeakRef<Shared, Internal>* weak_ref = node->weak_ref_;
56 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
57  if (weak_ref) {
58 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
59  // get_mutex() returns a shared_ptr copy, so the mutex outlives this lock
60  // even if delete node (below) destroys the WeakRefInternal holding it
61  auto mutex = weak_ref->get_mutex();
62  // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
63  std::lock_guard<std::mutex> lock(*mutex);
64 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
65 
66  if (--static_cast<Internal*>(node)->count == 0) {
67  delete node;
68  node = nullptr;
69  }
70  } else {
71  if (--static_cast<Internal*>(node)->count == 0) {
72  delete node;
73  node = nullptr;
74  }
75  }
76  }

◆ count_up()

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

Definition at line 36 of file generic_shared_impl.hpp.

36  {
37 #ifdef WITH_EXTRA_CHECKS
38  casadi_assert_dev(Function::call_depth_==0);
39 #endif // WITH_EXTRA_CHECKS
40 
41  if (node) static_cast<Internal*>(node)->count++;
42 
43  }

◆ debug_repr()

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

Definition at line 91 of file generic_shared_impl.hpp.

91  {
92  if (node) {
93  return node->debug_repr(node);
94  } else {
95  return "NULL";
96  }
97  }

◆ get()

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

Definition at line 115 of file generic_shared_impl.hpp.

115  {
116  return node;
117  }

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

◆ getCount()

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

Definition at line 138 of file generic_shared_impl.hpp.

138  {
139  return (*this)->getCount();
140  }

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

◆ is_null()

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

Definition at line 120 of file generic_shared_impl.hpp.

120  {
121  return node==nullptr;
122  }

Referenced by casadi::FunctionInternal::add_embedded(), casadi::FunctionInternal::alloc(), casadi::Nlpsol::callback(), casadi::FmuFunction::change_option(), casadi::DaeBuilderInternal::clear_cache(), casadi::Switch::codegen_body(), casadi::Conic::Conic(), casadi::conic_debug(), casadi::GraphBuilderInternal::create_function(), casadi::DaeBuilderInternal::export_fmu(), casadi::GraphBuilderInternal::export_onnx(), 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::GurobiInterface::handle_lazy_constraints_callback(), casadi::Integrator::init(), casadi::Nlpsol::init(), casadi::Rootfinder::init(), casadi::Switch::init(), casadi::GurobiInterface::init(), casadi::MosekInterface::init(), casadi::SLEQPInterface::init(), casadi::SnoptInterface::init(), casadi::SundialsInterface::init(), casadi::XpressInterface::init(), casadi::FastNewton::init(), casadi::Newton::init(), casadi::Scpgen::init(), casadi::GurobiInterface::init_mem(), 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::MosekInterface::MosekInterface(), casadi::Function::operator==(), casadi::DaeBuilderInternal::oracle(), casadi::SLEQPInterface::set_work(), casadi::SnoptInterface::set_work(), casadi::OptiNode::solve(), casadi::GurobiInterface::solve(), casadi::KnitroInterface::solve(), casadi::SLEQPInterface::solve(), casadi::WorhpInterface::solve(), casadi::FunctionInternal::sp_forward_block(), casadi::FunctionInternal::sp_reverse(), casadi::Sparsity::uni_coloring(), and casadi::XpressInterface::XpressInterface().

◆ operator->() [1/2]

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

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

Definition at line 194 of file generic_shared_impl.hpp.

194  {
195  return static_cast<GenericWeakRefInternal<Shared, Internal>*>(
197  }
Internal * operator->() const
Access a member function or object.

References casadi::GenericShared< Shared, Internal >::operator->().

◆ operator->() [2/2]

template<typename Shared , typename Internal >
const GenericWeakRefInternal< Shared, Internal > * casadi::GenericWeakRef< Shared, Internal >::operator->

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

Definition at line 187 of file generic_shared_impl.hpp.

187  {
188  return static_cast<const GenericWeakRefInternal<Shared, Internal>*>(
190  }

References casadi::GenericShared< Shared, Internal >::operator->().

◆ own()

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

◆ shared()

template<typename Shared , typename Internal >
Shared casadi::GenericWeakRef< Shared, Internal >::shared

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

Definition at line 163 of file generic_shared_impl.hpp.

163  {
164  Shared ret;
165  if (alive()) {
166  ret.own((*this)->raw_);
167  }
168  return ret;
169  }
bool alive() const
Check if alive.

◆ shared_if_alive()

template<typename Shared , typename Internal >
bool casadi::GenericWeakRef< Shared, Internal >::shared_if_alive ( Shared &  shared) const

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

Definition at line 172 of file generic_shared_impl.hpp.

172  {
173  if (is_null()) return false;
174 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
175  // Safe access to ...
176  std::lock_guard<std::mutex> lock(*(*this)->mutex_);
177 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
178  if (alive()) {
179  shared.own((*this)->raw_);
180  return true;
181  }
182  return false;
183  }

Referenced by casadi::Nlpsol::kkt().

◆ swap()

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

Definition at line 131 of file generic_shared_impl.hpp.

131  {
132  GenericShared<Shared, Internal> temp = *this;
133  *this = other;
134  other = temp;
135  }

◆ weak()

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

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

Definition at line 143 of file generic_shared_impl.hpp.

143  {
144  return (*this)->weak();
145  }

Friends And Related Function Documentation

◆ GenericSharedInternal< Shared, Internal >

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

Definition at line 135 of file generic_shared.hpp.


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