SpECTRE  v2024.03.19
StaticCache< Generator, T, Ranges > Class Template Reference

A cache of objects intended to be stored in a static variable. More...

#include <StaticCache.hpp>

Public Member Functions

template<typename Gen >
 StaticCache (Gen &&generator)
 
template<typename... Args>
const T & operator() (const Args... parameters) const
 

Detailed Description

template<typename Generator, typename T, typename... Ranges>
class StaticCache< Generator, T, Ranges >

A cache of objects intended to be stored in a static variable.

Objects can be accessed via a combination of several size_t and enum arguments. The range of each integral argument is specified via a template parameter of type CacheRange<start, end>, giving the first and one-past-last values for the range. Each enum argument is specified by a template parameter of type CacheEnumeration<EnumerationType, Members...> giving the enumeration type and an explicit set of every enum member to be cached.

Example

A cache with only numeric indices:

const static auto cache =
make_static_cache<CacheRange<0_st, 3_st>, CacheRange<3_st, 5_st>>(
[](const size_t a, const size_t b) { return a + b; });
CHECK(cache(0, 3) == 3); // smallest entry
CHECK(cache(2, 4) == 6); // largest entry
Parallel::GlobalCache< Metavariables > & cache(MockRuntimeSystem< Metavariables > &runner, const ArrayIndex &array_index)
Returns the GlobalCache of Component with index array_index.
Definition: MockRuntimeSystemFreeFunctions.hpp:379
Range of integral values for StaticCache indices. The Start is inclusive and the End is exclusive....
Definition: StaticCache.hpp:19

Example

A cache with enumeration indices:

const auto simple_enum_cache = make_static_cache<
[](const Color color) { return std::string{MakeString{} << color}; });
CHECK(simple_enum_cache(Color::Red) == "Red");
Make a string by streaming into object.
Definition: MakeString.hpp:18
auto make_static_cache(Generator &&generator)
Create a StaticCache, inferring the cached type from the generator.
Definition: StaticCache.hpp:198
Possible enumeration values for the StaticCache. Only values specified here are retrievable.
Definition: StaticCache.hpp:34

Example

A cache with mixed numeric and enumeration indices:

const auto simple_enum_size_t_enum_cache = make_static_cache<
CacheEnumeration<Animal, Animal::Goldendoodle, Animal::Labradoodle,
Animal::Poodle>>(
[](const Color color, const size_t value, const Animal animal) {
return std::string{MakeString{} << color << value << animal};
});
CHECK(simple_enum_size_t_enum_cache(Color::Red, 3, Animal::Labradoodle) ==
"Red3Labradoodle");
CHECK(simple_enum_size_t_enum_cache(Color::Purple, 4, Animal::Poodle) ==
"Purple4Poodle");
constexpr T & value(T &t)
Returns t.value() if t is a std::optional otherwise returns t.
Definition: OptionalHelpers.hpp:32

Example

A cache with no arguments at all (caching only a single object)

const auto simple_small_cache =
make_static_cache([]() { return size_t{10}; });
CHECK(simple_small_cache() == 10);
See also
make_static_cache
Template Parameters
Ttype held in the cache
Rangesranges of valid indices

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