BoostHelpers.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 helper functions for working with boost.
6 
7 #pragma once
8 
9 #include <array>
10 #include <boost/none.hpp>
11 #include <boost/variant.hpp>
12 #include <cstddef>
13 #include <initializer_list>
14 #include <pup.h>
15 #include <string>
16 #include <utility> // IWYU pragma: keep
17 
18 #include "Utilities/PrettyType.hpp"
19 #include "Utilities/TMPL.hpp"
20 #include "Utilities/TypeTraits.hpp"
21 
22 /// \cond
23 namespace boost {
24 template <typename T>
25 class optional;
26 } // namespace boost
27 /// \endcond
28 
29 namespace detail {
30 template <typename Sequence>
31 struct make_boost_variant_over_impl;
32 
33 template <template <typename...> class Sequence, typename... Ts>
34 struct make_boost_variant_over_impl<Sequence<Ts...>> {
35  static_assert(not cpp17::disjunction<std::is_same<
36  std::decay_t<std::remove_pointer_t<Ts>>, void>...>::value,
37  "Cannot create a boost::variant with a 'void' type.");
38  using type = boost::variant<Ts...>;
39 };
40 } // namespace detail
41 
42 /*!
43  * \ingroup UtilitiesGroup
44  * \brief Create a boost::variant with all all the types inside the typelist
45  * Sequence
46  *
47  * \metareturns boost::variant of all types inside `Sequence`
48  */
49 template <typename Sequence>
50 using make_boost_variant_over = typename detail::make_boost_variant_over_impl<
51  tmpl::remove_duplicates<Sequence>>::type;
52 
53 namespace BoostVariant_detail {
54 // clang-tidy: do not use non-const references
55 template <class T, class... Ts>
56 char pup_helper(int& index, PUP::er& p, boost::variant<Ts...>& var, // NOLINT
57  const int send_index) {
58  if (index == send_index) {
59  if (p.isUnpacking()) {
60  T t{};
61  p | t;
62  var = std::move(t);
63  } else {
64  p | boost::get<T>(var);
65  }
66  }
67  index++;
68  return '0';
69 }
70 } // namespace BoostVariant_detail
71 
72 namespace PUP {
73 template <class... Ts>
74 void pup(er& p, boost::variant<Ts...>& var) noexcept { // NOLINT
75  int index = 0;
76  int send_index = var.which();
77  p | send_index;
79  ::BoostVariant_detail::pup_helper<Ts>(index, p, var, send_index)...};
80 }
81 
82 template <typename... Ts>
83 inline void operator|(er& p, boost::variant<Ts...>& d) noexcept { // NOLINT
84  pup(p, d);
85 }
86 } // namespace PUP
87 
88 /*!
89  * \ingroup UtilitiesGroup
90  * \brief Get the type name of the current state of the boost::variant
91  */
92 template <typename... Ts>
94  const boost::variant<Ts...>& variant) noexcept {
95  // clang-format off
96  // clang-tidy: use gsl::at (we know it'll be in bounds and want fewer
97  // includes) clang-format moves the comment to the wrong line
98  return std::array<std::string, sizeof...(Ts)>{ // NOLINT
99  {pretty_type::get_name<Ts>()...}}[static_cast<size_t>(variant.which())];
100  // clang-format on
101 }
102 
103 namespace PUP {
104 template <class T>
105 void pup(er& p, boost::optional<T>& var) noexcept { // NOLINT
106  bool has_data = var != boost::none;
107  p | has_data;
108  if (has_data) {
109  if (p.isUnpacking()) {
110  var.emplace();
111  }
112  p | *var;
113  } else {
114  var = boost::none;
115  }
116 }
117 
118 template <typename T>
119 inline void operator|(er& p, boost::optional<T>& var) noexcept { // NOLINT
120  pup(p, var);
121 }
122 } // namespace PUP
Definition: Strahlkorper.hpp:14
Definition: BoostHelpers.hpp:53
Definition: BoostMultiArray.hpp:11
Definition: Determinant.hpp:11
typename detail::make_boost_variant_over_impl< tmpl::remove_duplicates< Sequence > >::type make_boost_variant_over
Create a boost::variant with all all the types inside the typelist Sequence.
Definition: BoostHelpers.hpp:51
Contains a pretty_type library to write types in a "pretty" format.
Wraps the template metaprogramming library used (brigand)
std::string type_of_current_state(const boost::variant< Ts... > &variant) noexcept
Get the type name of the current state of the boost::variant.
Definition: BoostHelpers.hpp:93
Defines type traits, some of which are future STL type_traits header.
A logical OR on the template parameters.
Definition: TypeTraits.hpp:145