26 #ifndef CASADI_GENERIC_SHARED_IMPL_HPP
27 #define CASADI_GENERIC_SHARED_IMPL_HPP
31 template<
typename Shared,
typename Internal>
32 void GenericShared<Shared, Internal>::count_up() {
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>
42 void GenericShared<Shared, Internal>::count_down() {
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>
68 void GenericShared<Shared, Internal>::own(Internal* node_) {
74 template<
typename Shared,
typename Internal>
75 void GenericShared<Shared, Internal>::assign(Internal* node_) {
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>
104 Internal* GenericShared<Shared, Internal>::get()
const {
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>
132 GenericWeakRef<Shared, Internal>* GenericShared<Shared, Internal>::weak() {
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>
175 const GenericWeakRefInternal<Shared, Internal>*
177 return static_cast<const GenericWeakRefInternal<Shared, Internal>*
>(
181 template<
typename Shared,
typename Internal>
182 GenericWeakRefInternal<Shared, Internal>*
184 return static_cast<GenericWeakRefInternal<Shared, 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_;
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.