26 #ifndef CASADI_GENERIC_SHARED_IMPL_HPP
27 #define CASADI_GENERIC_SHARED_IMPL_HPP
31 template<
typename Shared,
typename Internal>
33 #ifdef WITH_EXTRA_CHECKS
34 casadi_assert_dev(Function::call_depth_==0);
37 if (node)
static_cast<Internal*
>(node)->count++;
41 template<
typename Shared,
typename Internal>
43 #ifdef WITH_EXTRA_CHECKS
44 casadi_assert_dev(Function::call_depth_==0);
47 if (node->weak_ref_) {
48 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
49 auto mutex = node->weak_ref_->get_mutex();
51 std::lock_guard<std::mutex> lock(*mutex);
55 if (--
static_cast<Internal*
>(node)->count == 0) {
60 if (--
static_cast<Internal*
>(node)->count == 0) {
67 template<
typename Shared,
typename Internal>
74 template<
typename Shared,
typename Internal>
79 template<
typename Shared,
typename Internal>
82 return node->debug_repr(node);
88 template<
typename Shared,
typename Internal>
92 if (node == ref.node)
return *
this;
103 template<
typename Shared,
typename Internal>
108 template<
typename Shared,
typename Internal>
110 return node==
nullptr;
113 template<
typename Shared,
typename Internal>
115 casadi_assert_dev(!is_null());
119 template<
typename Shared,
typename Internal>
126 template<
typename Shared,
typename Internal>
128 return (*this)->getCount();
131 template<
typename Shared,
typename Internal>
133 return (*this)->weak();
136 template<
typename Shared,
typename Internal>
138 return reinterpret_cast<casadi_int
>(get());
141 template<
typename Shared,
typename Internal>
143 casadi_assert_dev(dummy==0);
146 template<
typename Shared,
typename Internal>
148 return !is_null() && (*this)->raw_ !=
nullptr;
151 template<
typename Shared,
typename Internal>
155 ret.own((*this)->raw_);
160 template<
typename Shared,
typename Internal>
162 if (is_null())
return false;
163 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
165 std::lock_guard<std::mutex> lock(*(*this)->mutex_);
168 shared.own((*this)->raw_);
174 template<
typename Shared,
typename Internal>
181 template<
typename Shared,
typename Internal>
188 template<
typename Shared,
typename Internal>
190 this->own(shared.weak()->get());
193 template<
typename Shared,
typename Internal>
195 this->own(
new typename Internal::weak_ref_type(raw));
198 template<
typename Shared,
typename Internal>
199 void GenericWeakRef<Shared, Internal>::kill() {
200 casadi_assert_dev((*this)->raw_);
201 (*this)->raw_ =
nullptr;
204 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
205 template<
typename Shared,
typename Internal>
206 std::shared_ptr<std::mutex> GenericWeakRef<Shared, Internal>::get_mutex()
const {
207 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.