List of all members | Public Member Functions
casadi::WeakCache< K, T > Class Template Reference

#include <generic_shared.hpp>

Detailed Description

template<typename K, typename T>
class casadi::WeakCache< K, T >

Key is stored as a regular ref Value is stored as weakref

Definition at line 242 of file generic_shared.hpp.

Public Member Functions

void tocache (const K &key, const T &f, bool needs_lock=true)
 
void tocache_if_missing (const K &key, T &f)
 
bool incache (const K &key, T &f, bool needs_lock=true) const
 
void cache (std::vector< K > &keys, std::vector< T > &entries) const
 

Member Function Documentation

◆ cache()

template<typename K , typename T >
void casadi::WeakCache< K, T >::cache ( std::vector< K > &  keys,
std::vector< T > &  entries 
) const
inline

Definition at line 290 of file generic_shared.hpp.

290  {
291 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
292  // Safe access to cache_
293  std::lock_guard<std::mutex> lock(mtx_);
294 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
295  keys.clear();
296  entries.clear();
297  // Add all entries that haven't been deleted
298  for (auto&& cf : cache_) {
299  typename T::base_type temp;
300  if (cf.second.shared_if_alive(temp)) {
301  keys.push_back(cf.first);
302  entries.push_back(shared_cast<T>(temp));
303  }
304  }
305  }

◆ incache()

template<typename K , typename T >
bool casadi::WeakCache< K, T >::incache ( const K &  key,
T &  f,
bool  needs_lock = true 
) const
inline

Definition at line 276 of file generic_shared.hpp.

276  {
277 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
278  // Safe access to cache_
279  casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
280 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
281  auto it = cache_.find(key);
282  typename T::base_type temp;
283  if (it!=cache_.end() && it->second.shared_if_alive(temp)) {
284  f = shared_cast<T>(temp);
285  return true;
286  } else {
287  return false;
288  }
289  }

Referenced by casadi::CallSX::get_output().

◆ tocache()

template<typename K , typename T >
void casadi::WeakCache< K, T >::tocache ( const K &  key,
const T &  f,
bool  needs_lock = true 
)
inline

Definition at line 244 of file generic_shared.hpp.

244  {
245 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
246  // Safe access to cache_
247  casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
248 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
249  // Add to cache
250  cache_.insert(std::make_pair(key, f));
251  // Remove a lost reference, if any, to prevent uncontrolled growth
252  for (auto it = cache_.begin(); it!=cache_.end(); ++it) {
253  if (!it->second.alive()) {
254  cache_.erase(it);
255  break; // just one dead reference is enough
256  }
257  }
258  }

◆ tocache_if_missing()

template<typename K , typename T >
void casadi::WeakCache< K, T >::tocache_if_missing ( const K &  key,
T &  f 
)
inline

Definition at line 267 of file generic_shared.hpp.

267  {
268 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
269  // Safe access to cache_
270  std::lock_guard<std::mutex> lock(mtx_);
271 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
272  if (!incache(key, f, false)) {
273  tocache(key, f, false);
274  }
275  }
void tocache(const K &key, const T &f, bool needs_lock=true)
bool incache(const K &key, T &f, bool needs_lock=true) const

Referenced by casadi::CallSX::get_output().


The documentation for this class was generated from the following file: