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 328 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 365 of file generic_shared.hpp.

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

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

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

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

356  {
357 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
358  // Safe access to cache_
359  std::lock_guard<std::mutex> lock(mtx_);
360 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
361  if (!incache(key, f, false)) {
362  tocache(key, f, false);
363  }
364  }
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: