Skip to content

Instantly share code, notes, and snippets.

@Rapptz
Created July 5, 2014 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rapptz/c6f7442cb57b181c663b to your computer and use it in GitHub Desktop.
Save Rapptz/c6f7442cb57b181c663b to your computer and use it in GitHub Desktop.
template<typename CharT, typename Traits>
inline auto operator>>(std::basic_istream<CharT, Traits>& in, tribool& tri) -> decltype(in) {
if(in.flags() & std::ios_base::boolalpha) {
typename std::basic_istream<CharT, Traits>::sentry sentry(in);
if(sentry) {
auto&& loc = in.getloc();
auto&& numpunct_facet = std::use_facet<std::numpunct<CharT>>(loc);
auto&& false_name = numpunct_facet.falsename();
auto&& true_name = numpunct_facet.truename();
using other_facet = indeterminate_name<CharT>;
std::basic_string<CharT> other_name = get_default_indeterminate_name<CharT>();
if(std::has_facet<other_facet>(loc)) {
auto&& facet = std::use_facet<other_facet>(loc);
other_name = facet.name();
}
std::basic_string<CharT> str;
if(in >> str) {
if(str == false_name) {
tri = false;
}
else if(str == true_name) {
tri = true;
}
else if(str == other_name) {
tri = indeterminate;
}
else {
in.setstate(std::ios_base::failbit);
}
}
return in;
}
}
else {
int x;
if(in >> x) {
switch(x) {
case 0: tri = false; break;
case 1: tri = true; break;
case 2: tri = indeterminate; break;
default:
in.setstate(std::ios_base::failbit);
break;
}
}
}
return in;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment