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

#include <generic_shared.hpp>

Detailed Description

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

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

Definition at line 327 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
 

Member Function Documentation

◆ incache()

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

Definition at line 364 of file generic_shared.hpp.

364  {
365 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
366  // Safe access to cache_
367  casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
368 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
369  const void* k = key.get();
370  auto it = pre_cache_.find(k);
371  K temp;
372  if (it!=pre_cache_.end() && it->second.shared_if_alive(temp)) {
373  auto it2 = cache_.find(k);
374  f = it2->second;
375  return true;
376  } else {
377  return false;
378  }
379  }

◆ tocache()

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

Definition at line 329 of file generic_shared.hpp.

329  {
330 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
331  // Safe access to cache_
332  casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
333 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
334  // Add to cache
335  const void* k = key.get();
336  pre_cache_.insert(std::make_pair(k, key));
337  cache_.insert(std::make_pair(k, f));
338  // Remove a lost reference, if any, to prevent uncontrolled growth
339  for (auto it = pre_cache_.begin(); it!=pre_cache_.end(); ++it) {
340  if (!it->second.alive()) {
341  pre_cache_.erase(it);
342  cache_.erase(it->first);
343  break; // just one dead reference is enough
344  }
345  }
346  }

◆ tocache_if_missing()

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

Definition at line 355 of file generic_shared.hpp.

355  {
356 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
357  // Safe access to cache_
358  std::lock_guard<std::mutex> lock(mtx_);
359 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
360  if (!incache(key, f, false)) {
361  tocache(key, f, false);
362  }
363  }
void tocache(const K &key, const T &f, bool needs_lock=true)
bool incache(const K &key, T &f, bool needs_lock=true) const

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