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>
36 void GenericShared<Shared, Internal>::count_up() {
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>
46 void GenericShared<Shared, Internal>::count_down() {
47 #ifdef WITH_EXTRA_CHECKS
48 casadi_assert_dev(Function::call_depth_==0);
51 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
52 GenericWeakRef<Shared, Internal>* weak_ref =
53 node->weak_ref_.load(std::memory_order_acquire);
55 GenericWeakRef<Shared, Internal>* weak_ref = node->weak_ref_;
58 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
61 auto mutex = weak_ref->get_mutex();
63 std::lock_guard<std::mutex> lock(*mutex);
66 if (--
static_cast<Internal*
>(node)->count == 0) {
71 if (--
static_cast<Internal*
>(node)->count == 0) {
78 template<
typename Shared,
typename Internal>
79 void GenericShared<Shared, Internal>::own(Internal* node_) {
85 template<
typename Shared,
typename Internal>
86 void GenericShared<Shared, Internal>::assign(Internal* node_) {
90 template<
typename Shared,
typename Internal>
93 return node->debug_repr(node);
99 template<
typename Shared,
typename Internal>
103 if (node == ref.node)
return *
this;
114 template<
typename Shared,
typename Internal>
119 template<
typename Shared,
typename Internal>
121 return node==
nullptr;
124 template<
typename Shared,
typename Internal>
126 casadi_assert_dev(!is_null());
130 template<
typename Shared,
typename Internal>
137 template<
typename Shared,
typename Internal>
138 casadi_int GenericShared<Shared, Internal>::getCount()
const {
139 return (*this)->getCount();
142 template<
typename Shared,
typename Internal>
143 GenericWeakRef<Shared, Internal>* GenericShared<Shared, Internal>::weak() {
144 return (*this)->weak();
147 template<
typename Shared,
typename Internal>
149 return reinterpret_cast<casadi_int
>(get());
152 template<
typename Shared,
typename Internal>
154 casadi_assert_dev(dummy==0);
157 template<
typename Shared,
typename Internal>
159 return !is_null() && (*this)->raw_ !=
nullptr;
162 template<
typename Shared,
typename Internal>
166 ret.own((*this)->raw_);
171 template<
typename Shared,
typename Internal>
173 if (is_null())
return false;
174 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
176 std::lock_guard<std::mutex> lock(*(*this)->mutex_);
179 shared.own((*this)->raw_);
185 template<
typename Shared,
typename Internal>
186 const GenericWeakRefInternal<Shared, Internal>*
188 return static_cast<const GenericWeakRefInternal<Shared, Internal>*
>(
192 template<
typename Shared,
typename Internal>
193 GenericWeakRefInternal<Shared, Internal>*
195 return static_cast<GenericWeakRefInternal<Shared, Internal>*
>(
199 template<
typename Shared,
typename Internal>
201 this->own(shared.weak()->get());
204 template<
typename Shared,
typename Internal>
206 this->own(
new typename Internal::weak_ref_type(raw));
209 template<
typename Shared,
typename Internal>
210 void GenericWeakRef<Shared, Internal>::kill() {
211 casadi_assert_dev((*this)->raw_);
212 (*this)->raw_ =
nullptr;
215 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
216 template<
typename Shared,
typename Internal>
217 std::shared_ptr<std::mutex> GenericWeakRef<Shared, Internal>::get_mutex()
const {
218 return (*this)->mutex_;
casadi_int __hash__() const
Returns a number that is unique for a given Node.
std::string debug_repr() const
bool is_null() const
Is a null pointer?
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.