magic_enum_format.hpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // __ __ _ ______ _____
  2. // | \/ | (_) | ____| / ____|_ _
  3. // | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
  4. // | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
  5. // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
  6. // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
  7. // __/ | https://github.com/Neargye/magic_enum
  8. // |___/ version 0.8.2
  9. //
  10. // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  11. // SPDX-License-Identifier: MIT
  12. // Copyright (c) 2019 - 2022 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. #ifndef NEARGYE_MAGIC_ENUM_FORMAT_HPP
  32. #define NEARGYE_MAGIC_ENUM_FORMAT_HPP
  33. #if !defined(__cpp_lib_format)
  34. # error "Format is not supported"
  35. #endif
  36. #include "magic_enum.hpp"
  37. #if !defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT)
  38. # define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT true
  39. # define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE
  40. #endif // MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT
  41. namespace magic_enum::customize {
  42. // customize enum to enable/disable automatic std::format
  43. template <typename E>
  44. constexpr bool enum_format_enabled() noexcept {
  45. return MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT;
  46. }
  47. } // magic_enum::customize
  48. #include <format>
  49. template <typename E>
  50. struct std::formatter<E, std::enable_if_t<std::is_enum_v<E> && magic_enum::customize::enum_format_enabled<E>(), char>> : std::formatter<std::string_view, char> {
  51. auto format(E e, format_context& ctx) {
  52. static_assert(std::is_same_v<char, string_view::value_type>, "formatter requires string_view::value_type type same as char.");
  53. using D = std::decay_t<E>;
  54. if constexpr (magic_enum::detail::supported<D>::value) {
  55. if (const auto name = magic_enum::enum_name<D, magic_enum::as_flags<magic_enum::detail::is_flags_v<D>>>(e); !name.empty()) {
  56. return std::formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx);
  57. }
  58. }
  59. return std::formatter<std::string_view, char>::format(std::to_string(magic_enum::enum_integer<D>(e)), ctx);
  60. }
  61. };
  62. #if defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE)
  63. # undef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT
  64. # undef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE
  65. #endif // MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE
  66. #endif // NEARGYE_MAGIC_ENUM_FORMAT_HPP