List of all members | Public Member Functions | Protected Member Functions
casadi::WeakRef Class Reference

Weak reference type. More...

#include <shared_object.hpp>

Detailed Description

A weak reference to a GenericShared

Author
Joel Andersson
Date
2013

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

Definition at line 114 of file shared_object.hpp.

Inheritance diagram for casadi::WeakRef:
Inheritance graph
[legend]
Collaboration diagram for casadi::WeakRef:
Collaboration graph
[legend]

Public Member Functions

 WeakRef (int dummy=0)
 
 WeakRef (SharedObject shared)
 
SharedObject shared () const
 Get a shared (owning) reference. More...
 
bool alive () const
 Check if alive. More...
 
bool shared_if_alive (SharedObject &shared) const
 Thread-safe alternative to alive()/shared() More...
 
GenericWeakRefInternal< SharedObject, SharedObjectInternal > * operator-> ()
 Access functions of the node. More...
 
const GenericWeakRefInternal< SharedObject, SharedObjectInternal > * operator-> () const
 Const access functions of the node. More...
 
void own (SharedObjectInternal *node)
 
void assign (SharedObjectInternal *node)
 Assign the node to a node class pointer without reference counting. More...
 
SharedObjectInternalget () 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< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 

Protected Member Functions

void count_up ()
 
void count_down ()
 

Constructor & Destructor Documentation

◆ WeakRef() [1/2]

casadi::WeakRef::WeakRef ( int  dummy = 0)
inline

Definition at line 117 of file shared_object.hpp.

118  }
template class CASADI_EXPORT GenericWeakRef< SharedObject, SharedObjectInternal >

◆ WeakRef() [2/2]

casadi::WeakRef::WeakRef ( SharedObject  shared)
inline

Definition at line 119 of file shared_object.hpp.

Member Function Documentation

◆ __hash__()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::__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 123 of file generic_shared_impl.hpp.

141  {
142  return reinterpret_cast<casadi_int>(get());
143  }

◆ alive()

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

Definition at line 165 of file generic_shared_impl.hpp.

151  {
152  return !is_null() && (*this)->raw_ != nullptr;
153  }

◆ assign()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::assign ( Internal *  node)
inherited

improper use will cause memory leaks!

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

Definition at line 96 of file generic_shared_impl.hpp.

79  {
80  node = node_;
81  }

◆ count_down()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_down
protectedinherited

Definition at line 133 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  if (node->weak_ref_) {
52 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
53  auto mutex = node->weak_ref_->get_mutex();
54  // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
55  std::lock_guard<std::mutex> lock(*mutex);
56  // Could it be that this mutex is destroyed when the lock goes out of scope?
57 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
58 
59  if (--static_cast<Internal*>(node)->count == 0) {
60  delete node;
61  node = nullptr;
62  }
63  } else {
64  if (--static_cast<Internal*>(node)->count == 0) {
65  delete node;
66  node = nullptr;
67  }
68  }
69  }

◆ count_up()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_up
protectedinherited

Definition at line 132 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()

std::string casadi::GenericShared< SharedObject , SharedObjectInternal >::debug_repr
inherited

Definition at line 112 of file generic_shared_impl.hpp.

84  {
85  if (node) {
86  return node->debug_repr(node);
87  } else {
88  return "NULL";
89  }
90  }
std::string debug_repr(const Internal *) const

◆ get()

Definition at line 99 of file generic_shared_impl.hpp.

108  {
109  return node;
110  }

◆ getCount()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::getCount
inherited

Definition at line 102 of file generic_shared_impl.hpp.

131  {
132  return (*this)->getCount();
133  }

◆ is_null()

Definition at line 116 of file generic_shared_impl.hpp.

113  {
114  return node==nullptr;
115  }

◆ operator->() [1/2]

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

Definition at line 175 of file generic_shared_impl.hpp.

187  {
188  return static_cast<GenericWeakRefInternal<Shared, Internal>*>(
189  GenericShared<Shared, Internal>::operator->());
190  }

◆ operator->() [2/2]

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

Definition at line 180 of file generic_shared_impl.hpp.

180  {
181  return static_cast<const GenericWeakRefInternal<Shared, Internal>*>(
182  GenericShared<Shared, Internal>::operator->());
183  }

◆ own()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::own ( Internal *  node)
inherited

Assign the node to a node class pointer (or null)

Definition at line 89 of file generic_shared_impl.hpp.

◆ shared()

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

Definition at line 160 of file generic_shared_impl.hpp.

156  {
157  Shared ret;
158  if (alive()) {
159  ret.own((*this)->raw_);
160  }
161  return ret;
162  }

◆ shared_if_alive()

bool casadi::GenericWeakRef< SharedObject , SharedObjectInternal >::shared_if_alive ( SharedObject shared) const
inherited

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

Definition at line 170 of file generic_shared_impl.hpp.

165  {
166  if (is_null()) return false;
167 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
168  // Safe access to ...
169  std::lock_guard<std::mutex> lock(*(*this)->mutex_);
170 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
171  if (alive()) {
172  shared.own((*this)->raw_);
173  return true;
174  }
175  return false;
176  }
void own(Internal *node)

◆ swap()

Definition at line 105 of file generic_shared_impl.hpp.

124  {
125  GenericShared<Shared, Internal> temp = *this;
126  *this = other;
127  other = temp;
128  }

◆ weak()

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

Definition at line 130 of file generic_shared_impl.hpp.

136  {
137  return (*this)->weak();
138  }

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