From 11a1c26494dc09f3dd713a1efee448805bda87cb Mon Sep 17 00:00:00 2001 From: Anton Bachin Date: Fri, 12 Jun 2015 12:56:51 -0500 Subject: [PATCH] Internal improvements to stream operators. --- enum.h | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/enum.h b/enum.h index 02e7345..91b9289 100644 --- a/enum.h +++ b/enum.h @@ -1041,49 +1041,37 @@ BETTER_ENUMS__CONSTEXPR inline bool operator !=(const Enum &a, const Enum &b) \ namespace better_enums { -// This template is used both as a sort of enable_if for SFINAE, and to delay -// the compiler from type-checking certain expressions. enum.h does not include -// or , because those headers parse very slowly. Therefore, -// it is necessary to prevent the compiler from trying to do anything with types -// from those headers unless the operators <<, >> are actually used, by which -// point the user is expected to have included and/or . The -// alternative is to simply move the operator definitions into a separate add-on -// header file. -// -// It should be possible to use std::enable_if, however is not -// available in C++98. +// This template is used as a sort of enable_if for SFINAE. It should be +// possible to use std::enable_if, however is not available in +// C++98. Non-char streams are currently not supported. template -struct hide { typedef T type; }; +struct only_if_enum { typedef T type; }; } -template -inline typename better_enums::hide::type& -operator <<(std::ostream& stream, const Enum& value) +template +inline typename better_enums::only_if_enum, + typename Enum::_enumerated>::type& +operator <<(std::basic_ostream& stream, const Enum &value) { return stream << value._to_string(); } -template -inline typename better_enums::hide::type& -operator >>(typename better_enums::hide::type& stream, - Enum& value) +template +inline typename better_enums::only_if_enum, + typename Enum::_enumerated>::type& +operator >>(std::basic_istream& stream, Enum &value) { - typedef typename better_enums::hide::type ios_base; - typedef typename better_enums::hide::type string; - - string buffer; + std::basic_string buffer; stream >> buffer; - better_enums::optional converted = + better_enums::optional converted = Enum::_from_string_nothrow(buffer.c_str()); if (converted) value = *converted; else - stream.setstate(ios_base::failbit); + stream.setstate(std::basic_istream::failbit); return stream; }