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 >

Definition at line 240 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 288 of file generic_shared.hpp.

288  {
289 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
290  // Safe access to cache_
291  std::lock_guard<std::mutex> lock(mtx_);
292 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
293  keys.clear();
294  entries.clear();
295  // Add all entries that haven't been deleted
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));
301  }
302  }
303  }

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

274  {
275 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
276  // Safe access to cache_
277  casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
278 #endif // 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);
283  return true;
284  } else {
285  return false;
286  }
287  }

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

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

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

265  {
266 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
267  // Safe access to cache_
268  std::lock_guard<std::mutex> lock(mtx_);
269 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
270  if (!incache(key, f, false)) {
271  tocache(key, f, false);
272  }
273  }
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: