26 #ifndef CASADI_GENERIC_SHARED_IMPL_HPP
27 #define CASADI_GENERIC_SHARED_IMPL_HPP
29 #ifdef WITH_EXTRA_CHECKS
30 #include "function.hpp"
35 template<
typename Shared,
typename Internal>
37 #ifdef WITH_EXTRA_CHECKS
38 casadi_assert_dev(Function::call_depth_==0);
41 if (node)
static_cast<Internal*
>(node)->count++;
45 template<
typename Shared,
typename Internal>
47 #ifdef WITH_EXTRA_CHECKS
48 casadi_assert_dev(Function::call_depth_==0);
51 if (node->weak_ref_) {
52 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
53 auto mutex = node->weak_ref_->get_mutex();
55 std::lock_guard<std::mutex> lock(*mutex);
59 if (--
static_cast<Internal*
>(node)->count == 0) {
64 if (--
static_cast<Internal*
>(node)->count == 0) {
71 template<
typename Shared,
typename Internal>
78 template<
typename Shared,
typename Internal>
83 template<
typename Shared,
typename Internal>
86 return node->debug_repr(node);
92 template<
typename Shared,
typename Internal>
93 GenericShared<Shared, Internal>&
96 if (node == ref.node)
return *
this;
107 template<
typename Shared,
typename Internal>
112 template<
typename Shared,
typename Internal>
114 return node==
nullptr;
117 template<
typename Shared,
typename Internal>
119 casadi_assert_dev(!is_null());
123 template<
typename Shared,
typename Internal>
130 template<
typename Shared,
typename Internal>
132 return (*this)->getCount();
135 template<
typename Shared,
typename Internal>
137 return (*this)->weak();
140 template<
typename Shared,
typename Internal>
142 return reinterpret_cast<casadi_int
>(get());
145 template<
typename Shared,
typename Internal>
147 casadi_assert_dev(dummy==0);
150 template<
typename Shared,
typename Internal>
152 return !is_null() && (*this)->raw_ !=
nullptr;
155 template<
typename Shared,
typename Internal>
159 ret.own((*this)->raw_);
164 template<
typename Shared,
typename Internal>
166 if (is_null())
return false;
167 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
169 std::lock_guard<std::mutex> lock(*(*this)->mutex_);
172 shared.own((*this)->raw_);
178 template<
typename Shared,
typename Internal>
185 template<
typename Shared,
typename Internal>
192 template<
typename Shared,
typename Internal>
194 this->own(shared.weak()->get());
197 template<
typename Shared,
typename Internal>
199 this->own(
new typename Internal::weak_ref_type(raw));
202 template<
typename Shared,
typename Internal>
203 void GenericWeakRef<Shared, Internal>::kill() {
204 casadi_assert_dev((*this)->raw_);
205 (*this)->raw_ =
nullptr;
208 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
209 template<
typename Shared,
typename Internal>
210 std::shared_ptr<std::mutex> GenericWeakRef<Shared, Internal>::get_mutex()
const {
211 return (*this)->mutex_;
GenericWeakRef< Shared, Internal > * weak()
Get a weak reference to the object.
casadi_int __hash__() const
Returns a number that is unique for a given Node.
std::string debug_repr() const
GenericShared & operator=(const GenericShared &ref)
Assignment operator.
Internal * get() const
Get a const pointer to the node.
void assign(Internal *node)
Assign the node to a node class pointer without reference counting.
void swap(GenericShared &other)
Swap content with another instance.
bool is_null() const
Is a null pointer?
Internal * operator->() const
Access a member function or object.
casadi_int getCount() const
Get the reference count.
bool alive() const
Check if alive.
Shared shared() const
Get a shared (owning) reference.
GenericWeakRefInternal< Shared, Internal > * operator->()
Access functions of the node.
bool shared_if_alive(Shared &shared) const
Thread-safe alternative to alive()/shared()
GenericWeakRef(int dummy=0)
Default constructor.