CachedTempBuffer.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <utility> // IWYU pragma: keep // for std::move
8 
10 #include "DataStructures/TempBuffer.hpp"
11 #include "Utilities/Gsl.hpp"
12 #include "Utilities/TMPL.hpp"
13 #include "Utilities/TaggedTuple.hpp"
14 
15 /*!
16  * \ingroup DataStructuresGroup
17  * A temporary buffer with contents computed on demand.
18  *
19  * When an entry in the buffer is first requested, it is computed by
20  * the `Computer` class. Subsequent requests just return the cached
21  * value. The computer can itself request data from the cache to use
22  * in its computations.
23  *
24  * For the cache
25  * \snippet Test_CachedTempBuffer.cpp alias
26  * the function used to compute `Tags::Scalar2<DataType>` is
27  * \snippet Test_CachedTempBuffer.cpp compute_func
28  */
29 template <typename Computer, typename... Tags>
31  public:
32  /// Construct the buffer with the given computer. `size` is passed
33  /// to the underlying `TempBuffer` constructor.
34  CachedTempBuffer(const size_t size, Computer computer) noexcept
35  : data_(size), computer_(std::move(computer)) {}
36 
37  /// Obtain a value from the buffer, computing it if necessary.
38  template <typename Tag>
39  const db::const_item_type<Tag>& get_var(Tag /*meta*/) noexcept {
40  // This function can't be called "get" because that interferes
41  // with the ADL needed to access data_.
42  if (not get<Computed<Tag>>(computed_flags_)) {
43  computer_(make_not_null(&get<Tag>(data_)), make_not_null(this), Tag{});
44  get<Computed<Tag>>(computed_flags_) = true;
45  }
46  return get<Tag>(data_);
47  }
48 
49  private:
50  template <typename Tag>
51  struct Computed {
52  using type = bool;
53  };
54 
55  TempBuffer<tmpl::list<Tags...>> data_;
56  tuples::TaggedTuple<Computed<Tags>...> computed_flags_{
57  ((void)Tags{}, false)...};
58  Computer computer_;
59 };
A TempBuffer holds a set of Tensor<DataType>s, where DataType is either a DataVector (or similar type...
Definition: TempBuffer.hpp:30
CachedTempBuffer(const size_t size, Computer computer) noexcept
Construct the buffer with the given computer. size is passed to the underlying TempBuffer constructor...
Definition: CachedTempBuffer.hpp:34
An associative container that is indexed by structs.
Definition: TaggedTuple.hpp:272
Definition: DataBoxTag.hpp:27
Definition: CachedTempBuffer.hpp:30
Wraps the template metaprogramming library used (brigand)
Defines functions and classes from the GSL.
gsl::not_null< T * > make_not_null(T *ptr) noexcept
Construct a not_null from a pointer. Often this will be done as an implicit conversion, but it may be necessary to perform the conversion explicitly when type deduction is desired.
Definition: Gsl.hpp:879
Defines classes SimpleTag, PrefixTag, ComputeTag and several functions for retrieving tag info...
const db::const_item_type< Tag > & get_var(Tag) noexcept
Obtain a value from the buffer, computing it if necessary.
Definition: CachedTempBuffer.hpp:39