26 #ifndef CASADI_GENERIC_SHARED_HPP
27 #define CASADI_GENERIC_SHARED_HPP
29 #include "casadi_common.hpp"
30 #include "exception.hpp"
31 #include <unordered_map>
33 #ifdef CASADI_WITH_THREAD
34 #ifdef CASADI_WITH_THREAD_MINGW
35 #include <mingw.mutex.h>
41 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
48 template<
typename Shared,
typename Internal>
53 template<
typename Shared,
typename Internal>
54 class GenericSharedInternal;
56 template<
typename Shared,
typename Internal>
57 class GenericWeakRefInternal;
60 template<
typename Shared,
typename Internal>
64 template<
class B,
class S>
friend const B
shared_cast(
const S& A);
90 void own(Internal* node);
97 void assign(Internal* node);
100 Internal* get()
const;
103 casadi_int getCount()
const;
109 Internal* operator->()
const;
113 std::string debug_repr()
const;
117 bool is_null()
const;
124 casadi_int __hash__()
const;
141 template<
typename Shared,
typename Internal>
161 Shared shared()
const;
171 bool shared_if_alive(Shared& shared)
const;
183 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
184 std::shared_ptr<std::mutex> get_mutex()
const;
208 template<
class B,
class S>
212 typename S::internal_base_type* ptr = A.get();
218 if (!B::test_cast(ptr))
return ret;
230 template<
class B,
class S>
233 return shared_cast<B, S>(A_copy);
239 template<
typename K,
typename T>
242 void tocache(
const K& key,
const T& f,
bool needs_lock=
true) {
243 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
248 cache_.insert(std::make_pair(key, f));
250 for (
auto it = cache_.begin(); it!=cache_.end(); ++it) {
251 if (!it->second.alive()) {
266 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
268 std::lock_guard<std::mutex> lock(mtx_);
270 if (!incache(key, f,
false)) {
271 tocache(key, f,
false);
274 bool incache(
const K& key, T& f,
bool needs_lock=
true)
const {
275 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
279 auto it = cache_.find(key);
280 typename T::base_type temp;
281 if (it!=cache_.end() && it->second.shared_if_alive(temp)) {
282 f = shared_cast<T>(temp);
288 void cache(std::vector<K>& keys, std::vector<T>& entries)
const {
289 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
291 std::lock_guard<std::mutex> lock(mtx_);
296 for (
auto&& cf : cache_) {
297 typename T::base_type temp;
298 if (cf.second.shared_if_alive(temp)) {
299 keys.push_back(cf.first);
300 entries.push_back(shared_cast<T>(temp));
305 std::unordered_map<K,
308 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
309 mutable std::mutex mtx_;
friend const B shared_cast(const S &A)
Typecast a shared object to a base class to a shared object to a derived class,.
GenericShared(const GenericShared &ref)
Copy constructor (shallow copy)
GenericShared()
Default constructor.
~GenericShared()
Destructor.
friend B shared_cast(S &A)
Typecast a shared object to a base class to a shared object to a derived class,.
void tocache(const K &key, const T &f, bool needs_lock=true)
bool incache(const K &key, T &f, bool needs_lock=true) const
void cache(std::vector< K > &keys, std::vector< T > &entries) const
void tocache_if_missing(const K &key, T &f)
B shared_cast(S &A)
Typecast a shared object to a base class to a shared object to a derived class,.