10 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_CACHE_H
11 #define INCLUDE_MOLASSEMBLER_TEMPLE_CACHE_H
13 #include <boost/any.hpp>
14 #include <boost/optional.hpp>
23 namespace Molassembler {
27 template<
typename KeyType,
typename ValueType>
30 using key_type = KeyType;
31 using value_type = ValueType;
36 void add(
const KeyType key,
const ValueType value) {
50 if(
cache_.count(key) == 1) {
59 boost::optional<const ValueType&>
getOption(
const KeyType& key)
const {
60 if(
cache_.count(key) == 1) {
61 return boost::optional<const ValueType&>(
70 bool has(
const KeyType& key)
const {
71 return cache_.count(key) > 0;
75 const ValueType&
get(
const KeyType& key)
const {
76 if(
cache_.count(key) == 0) {
77 throw "Fetching member in Cache whose key does not exist!";
98 template<
typename KeyType>
105 const std::initializer_list<
115 for(
const auto& pair: initList) {
126 void add(
const KeyType& key,
const T& value) {
142 if(
cache_.count(key) == 1) {
143 return boost::any_cast<T>(
153 return boost::any_cast<T>(
167 > modifyingUnaryFunction
172 if(
cache_.count(key) == 0) {
180 modifyingUnaryFunction(
197 if(
cache_.count(key) == 1) {
207 boost::optional<T>
getOption(
const KeyType& key)
const {
208 if(
cache_.count(key) == 1) {
209 return boost::optional<T>(
220 bool has(
const KeyType& key)
const {
221 return (
cache_.count(key) > 0);
void invalidate(const KeyType &key)
Selectively invalidates cache entries.
Definition: Cache.h:196
void invalidate()
Invalidates the entire cache, removing all stored data.
Definition: Cache.h:44
std::map< KeyType, ValueType > cache_
Cache data.
Definition: Cache.h:87
std::map< KeyType, boost::any > cache_
Cache data.
Definition: Cache.h:228
void changeGeneratable(const KeyType &key, std::function< > modifyingUnaryFunction)
Permits the modification of a generatable cache item via a raw pointer.
Definition: Cache.h:163
constexpr Get< 1 > second
Calls std::get<1> on any argument it is invoked with.
Definition: Functor.h:125
std::map< KeyType, std::function< boost::any()> > generationMap_
Map of key values to generating functions.
Definition: Cache.h:231
void invalidate(const KeyType &key)
Selectively invalidates cache entries.
Definition: Cache.h:49
void add(const KeyType key, const ValueType value)
Adds a data value for a key value into the cache.
Definition: Cache.h:36
T getGeneratable(const KeyType &key)
Definition: Cache.h:138
A minimal wrapper around a map class with cache semantics.
Definition: Cache.h:28
bool has(const KeyType &key) const
Tests whether the cache contains an entry for a key.
Definition: Cache.h:220
boost::optional< const ValueType & > getOption(const KeyType &key) const
Fetches a cache entry via an optional.
Definition: Cache.h:59
bool has(const KeyType &key) const
Tests whether the cache contains an entry for a key.
Definition: Cache.h:70
void invalidate()
Invalidates the entire cache, removing all stored data.
Definition: Cache.h:191
boost::optional< T > getOption(const KeyType &key) const
Fetches a cache entry via an optional.
Definition: Cache.h:207
void add(const KeyType &key, const T &value)
Adds a data value for a key value into the cache.
Definition: Cache.h:126