MakeWithValue.hpp
Go to the documentation of this file.
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 ///\file
5 /// Defines make_with_value
6 
7 #pragma once
8 
9 #include <array>
10 #include <type_traits>
11 
13 #include "Utilities/MakeArray.hpp"
15 
16 /// \ingroup DataStructuresGroup
17 /// Implementations of make_with_value.
18 namespace MakeWithValueImpls {
19 template <typename R, typename T>
21  template <typename ValueType>
22  static SPECTRE_ALWAYS_INLINE R apply(const T& input,
23  ValueType value) noexcept;
24 };
25 } // namespace MakeWithValueImpls
26 
27 /// \ingroup DataStructuresGroup
28 /// \brief Given an object of type `T`, create an object of type `R` whose
29 /// elements are initialized to `value`.
30 ///
31 /// \details This function is useful in function templates in order to
32 /// initialize the return type of a function template with `value` for functions
33 /// that can be called either at a single grid-point or to fill a data structure
34 /// at the same set of grid-points as the `input`
35 
36 /// \tparam ValueType The type of `value`. For many containers, this will be
37 /// `double`, but when making a `ComplexDataVector` with value,
38 /// `std::complex<double>` is appropriate.
39 ///
40 /// \see MakeWithValueImpls
41 template <typename R, typename T, typename ValueType>
43  const T& input, const ValueType& value) noexcept {
45  T>::apply(input, value);
46 }
47 
48 namespace MakeWithValueImpls {
49 /// \brief Returns a double initialized to `value` (`input` is ignored)
50 template <typename T>
51 struct MakeWithValueImpl<double, T> {
52  static SPECTRE_ALWAYS_INLINE double apply(const T& /* input */,
53  const double value) noexcept {
54  return value;
55  }
56 };
57 
58 template <typename T>
59 struct MakeWithValueImpl<std::complex<double>, T> {
61  const T& /* input */, const std::complex<double> value) noexcept {
62  return value;
63  }
64 };
65 
66 /// \brief Makes a `std::array`; each element of the `std::array`
67 /// must be `make_with_value`-creatable from a `InputType`.
68 template <size_t Size, typename T, typename InputType>
69 struct MakeWithValueImpl<std::array<T, Size>, InputType> {
70  template <typename ValueType>
72  const InputType& input, const ValueType value) noexcept {
73  return make_array<Size>(make_with_value<T>(input, value));
74  }
75 };
76 
77 /// \brief Makes a `TaggedTuple`; each element of the `TaggedTuple`
78 /// must be `make_with_value`-creatable from a `T`.
79 template <typename... Tags, typename T>
80 struct MakeWithValueImpl<tuples::TaggedTuple<Tags...>, T> {
81  template <typename ValueType>
83  const T& input, const ValueType value) noexcept {
84  return tuples::TaggedTuple<Tags...>(
85  make_with_value<typename Tags::type>(input, value)...);
86  }
87 };
88 
89 } // namespace MakeWithValueImpls
Implementations of make_with_value.
Definition: DenseVector.hpp:61
Defines class tuples::TaggedTuple.
Defines function make_array.
Definition: TaggedTuple.hpp:25
Definition: MakeWithValue.hpp:20
#define SPECTRE_ALWAYS_INLINE
Always inline a function. Only use this if you benchmarked the code.
Definition: ForceInline.hpp:20
An associative container that is indexed by structs.
Definition: TaggedTuple.hpp:272
std::remove_const_t< R > make_with_value(const T &input, const ValueType &value) noexcept
Given an object of type T, create an object of type R whose elements are initialized to value...
Definition: MakeWithValue.hpp:42
Defines macro to always inline a function.
Definition: DataBoxTag.hpp:29