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 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>
72 void GenericShared<Shared, Internal>::own(Internal* node_) {
78 template<
typename Shared,
typename Internal>
79 void GenericShared<Shared, Internal>::assign(Internal* node_) {
83 template<
typename Shared,
typename Internal>
86 return node->debug_repr(node);
92 template<
typename Shared,
typename Internal>
96 if (node == ref.node)
return *
this;
107 template<
typename Shared,
typename Internal>
108 Internal* GenericShared<Shared, Internal>::get()
const {
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>
136 GenericWeakRef<Shared, Internal>* GenericShared<Shared, Internal>::weak() {
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>
179 const GenericWeakRefInternal<Shared, Internal>*
181 return static_cast<const GenericWeakRefInternal<Shared, Internal>*
>(
185 template<
typename Shared,
typename Internal>
186 GenericWeakRefInternal<Shared, Internal>*
188 return static_cast<GenericWeakRefInternal<Shared, 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_;
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.