retdec
Public Member Functions | Protected Member Functions | Private Types | Private Attributes | List of all members
retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc > Class Template Reference

A mixin for enabling caching of computed results. More...

#include <caching.h>

Collaboration diagram for retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >:
Collaboration graph
[legend]

Public Member Functions

 Caching (bool enableCaching)
 
void enableCaching ()
 Enables caching. More...
 
void disableCaching ()
 Disables caching. More...
 
void clearCache ()
 Clears the cache of the already cached results. More...
 
void removeFromCache (const CachedKey &key)
 Removes the value corresponding to the given key from the cache. More...
 
bool isCachingEnabled () const
 Returns true if caching is enabled, false otherwise. More...
 

Protected Member Functions

void addToCache (const CachedKey &key, const CachedValue &value)
 If caching is enabled, associates the given value with key. More...
 
bool getCachedResult (const CachedKey &key, CachedValue &value) const
 If caching is enabled, stores the value associated with key into value. More...
 

Private Types

using Cache = std::unordered_map< CachedKey, CachedValue, HashFunc >
 Container for storing cached results. More...
 

Private Attributes

bool cachingEnabled
 Is caching enabled? More...
 
Cache cache
 Cache for storing cached results. More...
 

Detailed Description

template<typename CachedKey, typename CachedValue, typename HashFunc = std::hash<CachedKey>>
class retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >

A mixin for enabling caching of computed results.

Template Parameters
CachedKeyKey with which a value is associated.
CachedValueValue that is associated with a key.
HashFuncHashing function for CachedKey. The default is std::hash<CachedKey>.

Usage example (see Analysis/UsedVarsVisitor):

class UsedVarsVisitor: public Caching<ShPtr<Value>, ShPtr<UsedVars>,
HashFuncShPtr<Value>> {
// ...
};
ShPtr<UsedVars> UsedVarsVisitor::getUsedVars(ShPtr<Value> value) {
// Caching.
ShPtr<UsedVars> usedVars;
if (getCachedResult(value, usedVars)) {
return usedVars;
}
// The result is not cached, so compute it.
// Caching.
addToCache(value, usedVars);
return usedVars;
}
void enableCaching()
Enables caching.
Definition: caching.h:60
Caching(bool enableCaching)
Definition: caching.h:53
UsedVarsVisitor(bool visitSuccessors=true, bool visitNestedStmts=true, bool enableCaching=false)
Constructs a new visitor.
Definition: used_vars_visitor.cpp:180

Member Typedef Documentation

◆ Cache

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
using retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::Cache = std::unordered_map<CachedKey, CachedValue, HashFunc>
private

Container for storing cached results.

Constructor & Destructor Documentation

◆ Caching()

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::Caching ( bool  enableCaching)
inlineexplicit

Member Function Documentation

◆ addToCache()

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
void retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::addToCache ( const CachedKey &  key,
const CachedValue &  value 
)
inlineprotected

If caching is enabled, associates the given value with key.

◆ clearCache()

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
void retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::clearCache ( )
inline

Clears the cache of the already cached results.

◆ disableCaching()

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
void retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::disableCaching ( )
inline

Disables caching.

It also clears the cache of the already cached results.

◆ enableCaching()

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
void retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::enableCaching ( )
inline

Enables caching.

It also clears the cache of the already cached results.

◆ getCachedResult()

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
bool retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::getCachedResult ( const CachedKey &  key,
CachedValue &  value 
) const
inlineprotected

If caching is enabled, stores the value associated with key into value.

Returns
true if there is a value associated to key, false otherwise.

If there is a value associated with key, the value of value is left unchanged.

◆ isCachingEnabled()

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
bool retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::isCachingEnabled ( ) const
inline

Returns true if caching is enabled, false otherwise.

◆ removeFromCache()

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
void retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::removeFromCache ( const CachedKey &  key)
inline

Removes the value corresponding to the given key from the cache.

The key is removed as well. If there is no value corresponding to key, this function does nothing.

Member Data Documentation

◆ cache

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
Cache retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::cache
private

Cache for storing cached results.

◆ cachingEnabled

template<typename CachedKey , typename CachedValue , typename HashFunc = std::hash<CachedKey>>
bool retdec::llvmir2hll::Caching< CachedKey, CachedValue, HashFunc >::cachingEnabled
private

Is caching enabled?


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