mirror of
https://github.com/aantron/better-enums.git
synced 2026-02-16 15:19:46 +08:00
Move stream I/O operators to enum namespace
Stream I/O operators defined in a global namespace caused name resolution errors in some cases.
This commit is contained in:
parent
18cbeb0808
commit
9e522eaf3c
67
enum.h
67
enum.h
@ -834,7 +834,33 @@ BETTER_ENUMS_CONSTEXPR_ inline bool operator <=(const Enum &a, const Enum &b) \
|
|||||||
BETTER_ENUMS_CONSTEXPR_ inline bool operator >(const Enum &a, const Enum &b) \
|
BETTER_ENUMS_CONSTEXPR_ inline bool operator >(const Enum &a, const Enum &b) \
|
||||||
{ return a._to_integral() > b._to_integral(); } \
|
{ return a._to_integral() > b._to_integral(); } \
|
||||||
BETTER_ENUMS_CONSTEXPR_ inline bool operator >=(const Enum &a, const Enum &b) \
|
BETTER_ENUMS_CONSTEXPR_ inline bool operator >=(const Enum &a, const Enum &b) \
|
||||||
{ return a._to_integral() >= b._to_integral(); }
|
{ return a._to_integral() >= b._to_integral(); } \
|
||||||
|
\
|
||||||
|
\
|
||||||
|
template <typename Char, typename Traits> \
|
||||||
|
std::basic_ostream<Char, Traits>& \
|
||||||
|
operator <<(std::basic_ostream<Char, Traits>& stream, const Enum &value) \
|
||||||
|
{ \
|
||||||
|
return stream << value._to_string(); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
template <typename Char, typename Traits> \
|
||||||
|
std::basic_istream<Char, Traits>& \
|
||||||
|
operator >>(std::basic_istream<Char, Traits>& stream, Enum &value) \
|
||||||
|
{ \
|
||||||
|
std::basic_string<Char, Traits> buffer; \
|
||||||
|
\
|
||||||
|
stream >> buffer; \
|
||||||
|
::better_enums::optional<Enum> converted = \
|
||||||
|
Enum::_from_string_nothrow(buffer.c_str()); \
|
||||||
|
\
|
||||||
|
if (converted) \
|
||||||
|
value = *converted; \
|
||||||
|
else \
|
||||||
|
stream.setstate(std::basic_istream<Char, Traits>::failbit); \
|
||||||
|
\
|
||||||
|
return stream; \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1140,45 +1166,6 @@ BETTER_ENUMS_CONSTEXPR_ map<Enum, T> make_map(T (*f)(Enum))
|
|||||||
return map<Enum, T>(f);
|
return map<Enum, T>(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Stream I/O operators.
|
|
||||||
|
|
||||||
// This template is used as a sort of enable_if for SFINAE. It should be
|
|
||||||
// possible to use std::enable_if, however <type_traits> is not available in
|
|
||||||
// C++98. Non-char streams are currently not supported.
|
|
||||||
template <typename T, typename Enum>
|
|
||||||
struct only_if_enum { typedef T type; };
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char, typename Traits, typename Enum>
|
|
||||||
inline typename better_enums::only_if_enum<std::basic_ostream<Char, Traits>,
|
|
||||||
typename Enum::_enumerated>::type&
|
|
||||||
operator <<(std::basic_ostream<Char, Traits>& stream, const Enum &value)
|
|
||||||
{
|
|
||||||
return stream << value._to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Char, typename Traits, class Enum>
|
|
||||||
inline typename better_enums::only_if_enum<std::basic_istream<Char, Traits>,
|
|
||||||
typename Enum::_enumerated>::type&
|
|
||||||
operator >>(std::basic_istream<Char, Traits>& stream, Enum &value)
|
|
||||||
{
|
|
||||||
std::basic_string<Char, Traits> buffer;
|
|
||||||
|
|
||||||
stream >> buffer;
|
|
||||||
better_enums::optional<Enum> converted =
|
|
||||||
Enum::_from_string_nothrow(buffer.c_str());
|
|
||||||
|
|
||||||
if (converted)
|
|
||||||
value = *converted;
|
|
||||||
else
|
|
||||||
stream.setstate(std::basic_istream<Char, Traits>::failbit);
|
|
||||||
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // #ifndef BETTER_ENUMS_ENUM_H
|
#endif // #ifndef BETTER_ENUMS_ENUM_H
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user