magic_enum_format.hpp
Go to the documentation of this file.
1 // __ __ _ ______ _____
2 // | \/ | (_) | ____| / ____|_ _
3 // | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
4 // | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
5 // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
6 // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
7 // __/ | https://github.com/Neargye/magic_enum
8 // |___/ version 0.9.7
9 //
10 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
11 // SPDX-License-Identifier: MIT
12 // Copyright (c) 2019 - 2024 Daniil Goncharov <neargye@gmail.com>.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to deal
16 // in the Software without restriction, including without limitation the rights
17 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 // copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included in all
22 // copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 // SOFTWARE.
31 
32 #ifndef NEARGYE_MAGIC_ENUM_FORMAT_HPP
33 #define NEARGYE_MAGIC_ENUM_FORMAT_HPP
34 
35 #include "magic_enum.hpp"
36 #include "magic_enum_flags.hpp"
37 
38 namespace magic_enum::detail {
39 
40 template <typename E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, int> = 0>
41 std::string format_as(E e) {
42  using D = std::decay_t<E>;
43  static_assert(std::is_same_v<char, magic_enum::string_view::value_type>, "magic_enum::formatter requires string_view::value_type type same as char.");
45  if constexpr (magic_enum::detail::subtype_v<D> == magic_enum::detail::enum_subtype::flags) {
46  if (const auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) {
47  return {name.data(), name.size()};
48  }
49  } else {
50  if (const auto name = magic_enum::enum_name<D>(e); !name.empty()) {
51  return {name.data(), name.size()};
52  }
53  }
54  }
55  return std::to_string(magic_enum::enum_integer<D>(e));
56 }
57 
58 } // namespace magic_enum::format
59 
60 #if defined(__cpp_lib_format)
61 
62 template <typename E>
63 struct std::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, char>> : std::formatter<std::string_view, char> {
64  template <class FormatContext>
65  auto format(E e, FormatContext& ctx) const {
66  return std::formatter<std::string_view, char>::format(magic_enum::detail::format_as<E>(e), ctx);
67  }
68 };
69 
70 #endif
71 
72 #if defined(FMT_VERSION)
73 
74 template <typename E>
75 struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, char>> : fmt::formatter<std::string_view, char> {
76  template <class FormatContext>
77  auto format(E e, FormatContext& ctx) const {
78  return fmt::formatter<std::string_view, char>::format(magic_enum::detail::format_as<E>(e), ctx);
79  }
80 };
81 
82 #endif
83 
84 #endif // NEARGYE_MAGIC_ENUM_FORMAT_HPP
magic_enum::detail::enum_subtype::flags
@ flags
magic_enum.hpp
magic_enum::detail::enable_if_t
typename enable_if_enum< std::is_enum_v< D > &&std::is_invocable_r_v< bool, BinaryPredicate, char_type, char_type >, R >::type enable_if_t
Definition: magic_enum.hpp:904
magic_enum::detail
Definition: magic_enum.hpp:217
magic_enum::detail::format_as
std::string format_as(E e)
Definition: magic_enum_format.hpp:41
magic_enum_flags.hpp
std
magic_enum::detail::supported
Definition: magic_enum.hpp:220


magic_enum
Author(s):
autogenerated on Fri Feb 21 2025 03:20:19