MutateAssign.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <utility>
7 
9 #include "Utilities/Gsl.hpp"
10 #include "Utilities/TMPL.hpp"
11 
12 namespace Initialization {
13 namespace detail {
14 template <typename... MutateTags, typename BoxTags, typename... Args>
15 SPECTRE_ALWAYS_INLINE constexpr void mutate_assign_impl(
16  // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
17  const gsl::not_null<db::DataBox<BoxTags>*> box,
18  tmpl::list<MutateTags...> /*meta*/, Args&&... args) noexcept {
19  static_assert(sizeof...(MutateTags) == sizeof...(args),
20  "The number of arguments passed to `mutate_assign` must be "
21  "equal to the number of tags passed.");
22  db::mutate<MutateTags...>(box, [&args...](const auto... box_args) noexcept {
23  // silence unused capture warnings when there are zero args.
24  // This function still gets instantiated despite the `static_assert` in the
25  // parent function.
26  EXPAND_PACK_LEFT_TO_RIGHT((void)args);
27  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
28  EXPAND_PACK_LEFT_TO_RIGHT((*box_args = std::forward<Args>(args)));
29  });
30 }
31 } // namespace detail
32 
33 /*!
34  * \ingroup DataBoxGroup
35  * \brief Perform a mutation to the \ref DataBoxGroup `box`, assigning the
36  * `args` to the tags in `MutateTagList` in order.
37  */
38 template <typename MutateTagList, typename BoxTags, typename... Args>
40  // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
41  const gsl::not_null<db::DataBox<BoxTags>*> box, Args&&... args) noexcept {
42  static_assert(
43  tmpl::size<MutateTagList>::value > 0,
44  "At least one tag must be passed to `Initialization::mutate_assign`, but "
45  "received 0 tags to mutate.");
46  detail::mutate_assign_impl(box, MutateTagList{}, std::forward<Args>(args)...);
47 }
48 } // namespace Initialization
EXPAND_PACK_LEFT_TO_RIGHT
#define EXPAND_PACK_LEFT_TO_RIGHT(...)
Expand a parameter pack evaluating the terms from left to right.
Definition: TMPL.hpp:601
utility
Initialization::mutate_assign
constexpr void mutate_assign(const gsl::not_null< db::DataBox< BoxTags > * > box, Args &&... args) noexcept
Perform a mutation to the DataBox box, assigning the args to the tags in MutateTagList in order.
Definition: MutateAssign.hpp:39
db::mutate
decltype(auto) mutate(const gsl::not_null< DataBox< TagList > * > box, Invokable &&invokable, Args &&... args) noexcept
Allows changing the state of one or more non-computed elements in the DataBox.
Definition: DataBox.hpp:641
DataBox.hpp
SPECTRE_ALWAYS_INLINE
#define SPECTRE_ALWAYS_INLINE
Definition: ForceInline.hpp:16
Gsl.hpp
TMPL.hpp
gsl::not_null
Require a pointer to not be a nullptr
Definition: ReadSpecPiecewisePolynomial.hpp:13