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, bool prune=true)
 
void tocache_if_missing (const K &key, T &f, bool prune=true)
 
bool incache (const K &key, T &f, bool needs_lock=true) const
 
void prune ()
 
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 313 of file generic_shared.hpp.

313  {
314 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
315  // Safe access to cache_
316  std::lock_guard<std::mutex> lock(mtx_);
317 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
318  keys.clear();
319  entries.clear();
320  // Add all entries that haven't been deleted
321  for (auto&& cf : cache_) {
322  typename T::base_type temp;
323  if (cf.second.shared_if_alive(temp)) {
324  keys.push_back(cf.first);
325  entries.push_back(shared_cast<T>(temp));
326  }
327  }
328  }

◆ 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 285 of file generic_shared.hpp.

285  {
286 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
287  // Safe access to cache_
288  casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
289 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
290  auto it = cache_.find(key);
291  typename T::base_type temp;
292  if (it!=cache_.end() && it->second.shared_if_alive(temp)) {
293  f = shared_cast<T>(temp);
294  return true;
295  } else {
296  return false;
297  }
298  }

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

◆ prune()

template<typename K , typename T >
void casadi::WeakCache< K, T >::prune ( )
inline

Definition at line 300 of file generic_shared.hpp.

300  {
301 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
302  // Safe access to cache_
303  std::lock_guard<std::mutex> lock(mtx_);
304 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
305  for (auto it = cache_.begin(); it!=cache_.end();) {
306  if (!it->second.alive()) {
307  it = cache_.erase(it);
308  } else {
309  ++it;
310  }
311  }
312  }

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,
bool  prune = true 
)
inline

Definition at line 244 of file generic_shared.hpp.

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

◆ tocache_if_missing()

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

Definition at line 276 of file generic_shared.hpp.

276  {
277 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
278  // Safe access to cache_
279  std::lock_guard<std::mutex> lock(mtx_);
280 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
281  if (!incache(key, f, false)) {
282  tocache(key, f, false, prune);
283  }
284  }
bool incache(const K &key, T &f, bool needs_lock=true) const
void tocache(const K &key, const T &f, bool needs_lock=true, bool prune=true)

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


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