TagName.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <string>
8 #include <type_traits>
9 
10 #include "DataStructures/DataBox/TagTraits.hpp"
11 #include "Utilities/PrettyType.hpp"
12 #include "Utilities/Requires.hpp"
13 #include "Utilities/TypeTraits.hpp"
14 
15 /// \cond
16 namespace db {
17 struct PrefixTag;
18 } // namespace db
19 /// \endcond
20 
21 namespace db {
22 
23 namespace DataBox_detail {
24 template <typename Tag, typename = cpp17::void_t<>>
25 struct tag_name_impl;
26 
27 template <typename Tag, typename = std::nullptr_t, typename = cpp17::void_t<>>
28 struct tag_name_impl2 {
29  static_assert(not is_compute_item_v<Tag>,
30  "Compute tags must have a name function or a base alias.");
31  static std::string name() noexcept { return pretty_type::short_name<Tag>(); }
32 };
33 
34 template <typename Tag>
35 struct tag_name_impl2<Tag, Requires<is_compute_item_v<Tag>>,
36  cpp17::void_t<typename Tag::base>>
37  : tag_name_impl<typename Tag::base> {};
38 
39 template <typename Tag>
40 struct tag_name_impl2<Tag, Requires<cpp17::is_base_of_v<db::PrefixTag, Tag> and
41  not is_compute_item_v<Tag>>> {
42  static std::string name() noexcept {
43  return pretty_type::short_name<Tag>() + "(" +
44  tag_name_impl<typename Tag::tag>::name() + ")";
45  }
46 };
47 
48 template <typename Tag, typename>
49 struct tag_name_impl : tag_name_impl2<Tag> {};
50 
51 template <typename Tag>
52 struct tag_name_impl<Tag, cpp17::void_t<decltype(Tag::name())>> : public Tag {};
53 } // namespace DataBox_detail
54 
55 /*!
56  * \ingroup DataBoxGroup
57  * \brief Get the name of a DataBoxTag, including prefixes
58  *
59  * \details
60  * Given a DataBoxTag returns the name of the DataBoxTag as a std::string. If
61  * the DataBoxTag is also a PrefixTag then the prefix is added.
62  *
63  * \tparam Tag the DataBoxTag whose name to get
64  * \return string holding the DataBoxTag's name
65  */
66 template <typename Tag>
67 std::string tag_name() noexcept {
68  return DataBox_detail::tag_name_impl<Tag>::name();
69 }
70 
71 } // namespace db
void void_t
Given a set of types, returns void
Definition: TypeTraits.hpp:187
Defines the type alias Requires.
Namespace for DataBox related things.
Definition: DataBox.hpp:42
Contains a pretty_type library to write types in a "pretty" format.
T is_base_of_v
std::string tag_name() noexcept
Get the name of a DataBoxTag, including prefixes.
Definition: TagName.hpp:67
typename Requires_detail::requires_impl< B >::template_error_type_failed_to_meet_requirements_on_template_parameters Requires
Express requirements on the template parameters of a function or class, replaces std::enable_if_t ...
Definition: Requires.hpp:67
C++ STL code present in C++17.
Definition: Array.hpp:16