magic_enum_iostream.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_IOSTREAM_HPP
33 #define NEARGYE_MAGIC_ENUM_IOSTREAM_HPP
34 
35 #include "magic_enum.hpp"
36 #include "magic_enum_flags.hpp"
37 
38 #ifndef MAGIC_ENUM_USE_STD_MODULE
39 #include <iosfwd>
40 #endif
41 
42 namespace magic_enum {
43 
44 namespace ostream_operators {
45 
46 template <typename Char, typename Traits, typename E, detail::enable_if_t<E, int> = 0>
47 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, E value) {
48  using D = std::decay_t<E>;
49  using U = underlying_type_t<D>;
50 
51  if constexpr (detail::supported<D>::value) {
52  if constexpr (detail::subtype_v<D> == detail::enum_subtype::flags) {
53  if (const auto name = enum_flags_name<D>(value); !name.empty()) {
54  for (const auto c : name) {
55  os.put(c);
56  }
57  return os;
58  }
59  } else {
60  if (const auto name = enum_name<D>(value); !name.empty()) {
61  for (const auto c : name) {
62  os.put(c);
63  }
64  return os;
65  }
66  }
67  }
68  return (os << static_cast<U>(value));
69 }
70 
71 template <typename Char, typename Traits, typename E, detail::enable_if_t<E, int> = 0>
72 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, optional<E> value) {
73  return value ? (os << *value) : os;
74 }
75 
76 } // namespace magic_enum::ostream_operators
77 
78 namespace istream_operators {
79 
80 template <typename Char, typename Traits, typename E, detail::enable_if_t<E, int> = 0>
81 std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& is, E& value) {
82  using D = std::decay_t<E>;
83 
84  std::basic_string<Char, Traits> s;
85  is >> s;
86  if constexpr (detail::supported<D>::value) {
87  if constexpr (detail::subtype_v<D> == detail::enum_subtype::flags) {
88  if (const auto v = enum_flags_cast<D>(s)) {
89  value = *v;
90  } else {
91  is.setstate(std::basic_ios<Char>::failbit);
92  }
93  } else {
94  if (const auto v = enum_cast<D>(s)) {
95  value = *v;
96  } else {
97  is.setstate(std::basic_ios<Char>::failbit);
98  }
99  }
100  } else {
101  is.setstate(std::basic_ios<Char>::failbit);
102  }
103  return is;
104 }
105 
106 } // namespace magic_enum::istream_operators
107 
108 namespace iostream_operators {
109 
110 using magic_enum::ostream_operators::operator<<;
111 using magic_enum::istream_operators::operator>>;
112 
113 } // namespace magic_enum::iostream_operators
114 
115 } // namespace magic_enum
116 
117 #endif // NEARGYE_MAGIC_ENUM_IOSTREAM_HPP
magic_enum::detail::enum_subtype::flags
@ flags
magic_enum.hpp
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:679
magic_enum::underlying_type_t
typename underlying_type< T >::type underlying_type_t
Definition: magic_enum.hpp:1168
magic_enum::ostream_operators::operator<<
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, E value)
Definition: magic_enum_iostream.hpp:47
magic_enum_flags.hpp
magic_enum
Definition: magic_enum.hpp:126
magic_enum::detail::supported
Definition: magic_enum.hpp:220
magic_enum::istream_operators::operator>>
std::basic_istream< Char, Traits > & operator>>(std::basic_istream< Char, Traits > &is, E &value)
Definition: magic_enum_iostream.hpp:81


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