annotate DEPENDENCIES/generic/include/boost/logic/tribool_io.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // Three-state boolean logic library
Chris@16 2
Chris@16 3 // Copyright Douglas Gregor 2002-2004. Use, modification and
Chris@16 4 // distribution is subject to the Boost Software License, Version
Chris@16 5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7 #ifndef BOOST_LOGIC_TRIBOOL_IO_HPP
Chris@16 8 #define BOOST_LOGIC_TRIBOOL_IO_HPP
Chris@16 9
Chris@16 10 #include <boost/logic/tribool.hpp>
Chris@16 11 #include <boost/detail/workaround.hpp>
Chris@16 12 #include <boost/noncopyable.hpp>
Chris@16 13
Chris@101 14 #if defined(_MSC_VER)
Chris@16 15 # pragma once
Chris@16 16 #endif
Chris@16 17
Chris@16 18 #ifndef BOOST_NO_STD_LOCALE
Chris@16 19 # include <locale>
Chris@16 20 #endif
Chris@16 21
Chris@16 22 #include <string>
Chris@16 23 #include <iostream>
Chris@16 24
Chris@16 25 namespace boost { namespace logic {
Chris@16 26
Chris@16 27 #ifdef BOOST_NO_STD_LOCALE
Chris@16 28
Chris@16 29 /**
Chris@16 30 * \brief Returns a string containing the default name for the \c
Chris@16 31 * false value of a tribool with the given character type T.
Chris@16 32 *
Chris@16 33 * This function only exists when the C++ standard library
Chris@16 34 * implementation does not support locales.
Chris@16 35 */
Chris@16 36 template<typename T> std::basic_string<T> default_false_name();
Chris@16 37
Chris@16 38 /**
Chris@16 39 * \brief Returns the character string "false".
Chris@16 40 *
Chris@16 41 * This function only exists when the C++ standard library
Chris@16 42 * implementation does not support locales.
Chris@16 43 */
Chris@16 44 template<>
Chris@16 45 inline std::basic_string<char> default_false_name<char>()
Chris@16 46 { return "false"; }
Chris@16 47
Chris@16 48 # ifndef BOOST_NO_WCHAR_T
Chris@16 49 /**
Chris@16 50 * \brief Returns the wide character string L"false".
Chris@16 51 *
Chris@16 52 * This function only exists when the C++ standard library
Chris@16 53 * implementation does not support locales.
Chris@16 54 */
Chris@16 55 template<>
Chris@16 56 inline std::basic_string<wchar_t> default_false_name<wchar_t>()
Chris@16 57 { return L"false"; }
Chris@16 58 # endif
Chris@16 59
Chris@16 60 /**
Chris@16 61 * \brief Returns a string containing the default name for the \c true
Chris@16 62 * value of a tribool with the given character type T.
Chris@16 63 *
Chris@16 64 * This function only exists when the C++ standard library
Chris@16 65 * implementation does not support locales.
Chris@16 66 */
Chris@16 67 template<typename T> std::basic_string<T> default_true_name();
Chris@16 68
Chris@16 69 /**
Chris@16 70 * \brief Returns the character string "true".
Chris@16 71 *
Chris@16 72 * This function only exists when the C++ standard library
Chris@16 73 * implementation does not support locales.
Chris@16 74 */
Chris@16 75 template<>
Chris@16 76 inline std::basic_string<char> default_true_name<char>()
Chris@16 77 { return "true"; }
Chris@16 78
Chris@16 79 # ifndef BOOST_NO_WCHAR_T
Chris@16 80 /**
Chris@16 81 * \brief Returns the wide character string L"true".
Chris@16 82 *
Chris@16 83 * This function only exists * when the C++ standard library
Chris@16 84 * implementation does not support * locales.
Chris@16 85 */
Chris@16 86 template<>
Chris@16 87 inline std::basic_string<wchar_t> default_true_name<wchar_t>()
Chris@16 88 { return L"true"; }
Chris@16 89 # endif
Chris@16 90 #endif
Chris@16 91
Chris@16 92 /**
Chris@16 93 * \brief Returns a string containing the default name for the indeterminate
Chris@16 94 * value of a tribool with the given character type T.
Chris@16 95 *
Chris@16 96 * This routine is used by the input and output streaming operators
Chris@16 97 * for tribool when there is no locale support or the stream's locale
Chris@16 98 * does not contain the indeterminate_name facet.
Chris@16 99 */
Chris@16 100 template<typename T> std::basic_string<T> get_default_indeterminate_name();
Chris@16 101
Chris@16 102 /// Returns the character string "indeterminate".
Chris@16 103 template<>
Chris@16 104 inline std::basic_string<char> get_default_indeterminate_name<char>()
Chris@16 105 { return "indeterminate"; }
Chris@16 106
Chris@101 107 #ifndef BOOST_NO_WCHAR_T
Chris@16 108 /// Returns the wide character string L"indeterminate".
Chris@16 109 template<>
Chris@16 110 inline std::basic_string<wchar_t> get_default_indeterminate_name<wchar_t>()
Chris@16 111 { return L"indeterminate"; }
Chris@16 112 #endif
Chris@16 113
Chris@16 114 // http://www.cantrip.org/locale.html
Chris@16 115
Chris@16 116 #ifndef BOOST_NO_STD_LOCALE
Chris@16 117 /**
Chris@16 118 * \brief A locale facet specifying the name of the indeterminate
Chris@16 119 * value of a tribool.
Chris@16 120 *
Chris@16 121 * The facet is used to perform I/O on tribool values when \c
Chris@16 122 * std::boolalpha has been specified. This class template is only
Chris@16 123 * available if the C++ standard library implementation supports
Chris@16 124 * locales.
Chris@16 125 */
Chris@16 126 template<typename CharT>
Chris@16 127 class indeterminate_name : public std::locale::facet, private boost::noncopyable
Chris@16 128 {
Chris@16 129 public:
Chris@16 130 typedef CharT char_type;
Chris@16 131 typedef std::basic_string<CharT> string_type;
Chris@16 132
Chris@16 133 /// Construct the facet with the default name
Chris@16 134 indeterminate_name() : name_(get_default_indeterminate_name<CharT>()) {}
Chris@16 135
Chris@16 136 /// Construct the facet with the given name for the indeterminate value
Chris@16 137 explicit indeterminate_name(const string_type& initial_name)
Chris@16 138 : name_(initial_name) {}
Chris@16 139
Chris@16 140 /// Returns the name for the indeterminate value
Chris@16 141 string_type name() const { return name_; }
Chris@16 142
Chris@16 143 /// Uniquily identifies this facet with the locale.
Chris@16 144 static std::locale::id id;
Chris@16 145
Chris@16 146 private:
Chris@16 147 string_type name_;
Chris@16 148 };
Chris@16 149
Chris@16 150 template<typename CharT> std::locale::id indeterminate_name<CharT>::id;
Chris@16 151 #endif
Chris@16 152
Chris@16 153 /**
Chris@16 154 * \brief Writes the value of a tribool to a stream.
Chris@16 155 *
Chris@16 156 * When the value of @p x is either \c true or \c false, this routine
Chris@16 157 * is semantically equivalent to:
Chris@16 158 * \code out << static_cast<bool>(x); \endcode
Chris@16 159 *
Chris@16 160 * When @p x has an indeterminate value, it outputs either the integer
Chris@16 161 * value 2 (if <tt>(out.flags() & std::ios_base::boolalpha) == 0</tt>)
Chris@16 162 * or the name of the indeterminate value. The name of the
Chris@16 163 * indeterminate value comes from the indeterminate_name facet (if it
Chris@16 164 * is defined in the output stream's locale), or from the
Chris@16 165 * get_default_indeterminate_name function (if it is not defined in the
Chris@16 166 * locale or if the C++ standard library implementation does not
Chris@16 167 * support locales).
Chris@16 168 *
Chris@16 169 * \returns @p out
Chris@16 170 */
Chris@16 171 template<typename CharT, typename Traits>
Chris@16 172 inline std::basic_ostream<CharT, Traits>&
Chris@16 173 operator<<(std::basic_ostream<CharT, Traits>& out, tribool x)
Chris@16 174 {
Chris@16 175 if (!indeterminate(x)) {
Chris@16 176 out << static_cast<bool>(x);
Chris@16 177 } else {
Chris@16 178 typename std::basic_ostream<CharT, Traits>::sentry cerberus(out);
Chris@16 179 if (cerberus) {
Chris@16 180 if (out.flags() & std::ios_base::boolalpha) {
Chris@16 181 #ifndef BOOST_NO_STD_LOCALE
Chris@16 182 if (BOOST_HAS_FACET(indeterminate_name<CharT>, out.getloc())) {
Chris@16 183 const indeterminate_name<CharT>& facet =
Chris@16 184 BOOST_USE_FACET(indeterminate_name<CharT>, out.getloc());
Chris@16 185 out << facet.name();
Chris@16 186 } else {
Chris@16 187 out << get_default_indeterminate_name<CharT>();
Chris@16 188 }
Chris@16 189 #else
Chris@16 190 out << get_default_indeterminate_name<CharT>();
Chris@16 191 #endif
Chris@16 192 }
Chris@16 193 else
Chris@16 194 out << 2;
Chris@16 195 }
Chris@16 196 }
Chris@16 197 return out;
Chris@16 198 }
Chris@16 199
Chris@16 200 /**
Chris@16 201 * \brief Writes the indeterminate tribool value to a stream.
Chris@16 202 *
Chris@16 203 * This routine outputs either the integer
Chris@16 204 * value 2 (if <tt>(out.flags() & std::ios_base::boolalpha) == 0</tt>)
Chris@16 205 * or the name of the indeterminate value. The name of the
Chris@16 206 * indeterminate value comes from the indeterminate_name facet (if it
Chris@16 207 * is defined in the output stream's locale), or from the
Chris@16 208 * get_default_indeterminate_name function (if it is not defined in the
Chris@16 209 * locale or if the C++ standard library implementation does not
Chris@16 210 * support locales).
Chris@16 211 *
Chris@16 212 * \returns @p out
Chris@16 213 */
Chris@16 214 template<typename CharT, typename Traits>
Chris@16 215 inline std::basic_ostream<CharT, Traits>&
Chris@16 216 operator<<(std::basic_ostream<CharT, Traits>& out,
Chris@16 217 bool (*)(tribool, detail::indeterminate_t))
Chris@16 218 { return out << tribool(indeterminate); }
Chris@16 219
Chris@16 220 /**
Chris@16 221 * \brief Reads a tribool value from a stream.
Chris@16 222 *
Chris@16 223 * When <tt>(out.flags() & std::ios_base::boolalpha) == 0</tt>, this
Chris@16 224 * function reads a \c long value from the input stream @p in and
Chris@16 225 * converts that value to a tribool. If that value is 0, @p x becomes
Chris@16 226 * \c false; if it is 1, @p x becomes \c true; if it is 2, @p becomes
Chris@16 227 * \c indetermine; otherwise, the operation fails (and the fail bit is
Chris@16 228 * set on the input stream @p in).
Chris@16 229 *
Chris@16 230 * When <tt>(out.flags() & std::ios_base::boolalpha) != 0</tt>, this
Chris@16 231 * function first determines the names of the false, true, and
Chris@16 232 * indeterminate values. The false and true names are extracted from
Chris@16 233 * the \c std::numpunct facet of the input stream's locale (if the C++
Chris@16 234 * standard library implementation supports locales), or from the \c
Chris@16 235 * default_false_name and \c default_true_name functions (if there is
Chris@16 236 * no locale support). The indeterminate name is extracted from the
Chris@16 237 * appropriate \c indeterminate_name facet (if it is available in the
Chris@16 238 * input stream's locale), or from the \c get_default_indeterminate_name
Chris@16 239 * function (if the C++ standard library implementation does not
Chris@16 240 * support locales, or the \c indeterminate_name facet is not
Chris@16 241 * specified for this locale object). The input is then matched to
Chris@16 242 * each of these names, and the tribool @p x is assigned the value
Chris@16 243 * corresponding to the longest name that matched. If no name is
Chris@16 244 * matched or all names are empty, the operation fails (and the fail
Chris@16 245 * bit is set on the input stream @p in).
Chris@16 246 *
Chris@16 247 * \returns @p in
Chris@16 248 */
Chris@16 249 template<typename CharT, typename Traits>
Chris@16 250 inline std::basic_istream<CharT, Traits>&
Chris@16 251 operator>>(std::basic_istream<CharT, Traits>& in, tribool& x)
Chris@16 252 {
Chris@16 253 if (in.flags() & std::ios_base::boolalpha) {
Chris@16 254 typename std::basic_istream<CharT, Traits>::sentry cerberus(in);
Chris@16 255 if (cerberus) {
Chris@16 256 typedef std::basic_string<CharT> string_type;
Chris@16 257
Chris@16 258 #ifndef BOOST_NO_STD_LOCALE
Chris@16 259 const std::numpunct<CharT>& numpunct_facet =
Chris@16 260 BOOST_USE_FACET(std::numpunct<CharT>, in.getloc());
Chris@16 261
Chris@16 262 string_type falsename = numpunct_facet.falsename();
Chris@16 263 string_type truename = numpunct_facet.truename();
Chris@16 264
Chris@16 265 string_type othername;
Chris@16 266 if (BOOST_HAS_FACET(indeterminate_name<CharT>, in.getloc())) {
Chris@16 267 othername =
Chris@16 268 BOOST_USE_FACET(indeterminate_name<CharT>, in.getloc()).name();
Chris@16 269 } else {
Chris@16 270 othername = get_default_indeterminate_name<CharT>();
Chris@16 271 }
Chris@16 272 #else
Chris@16 273 string_type falsename = default_false_name<CharT>();
Chris@16 274 string_type truename = default_true_name<CharT>();
Chris@16 275 string_type othername = get_default_indeterminate_name<CharT>();
Chris@16 276 #endif
Chris@16 277
Chris@16 278 typename string_type::size_type pos = 0;
Chris@16 279 bool falsename_ok = true, truename_ok = true, othername_ok = true;
Chris@16 280
Chris@16 281 // Modeled after the code from Library DR 17
Chris@101 282 while ((falsename_ok && pos < falsename.size())
Chris@101 283 || (truename_ok && pos < truename.size())
Chris@101 284 || (othername_ok && pos < othername.size())) {
Chris@16 285 typename Traits::int_type c = in.get();
Chris@16 286 if (c == Traits::eof())
Chris@16 287 return in;
Chris@16 288
Chris@16 289 bool matched = false;
Chris@16 290 if (falsename_ok && pos < falsename.size()) {
Chris@16 291 if (Traits::eq(Traits::to_char_type(c), falsename[pos]))
Chris@16 292 matched = true;
Chris@16 293 else
Chris@16 294 falsename_ok = false;
Chris@16 295 }
Chris@16 296
Chris@16 297 if (truename_ok && pos < truename.size()) {
Chris@16 298 if (Traits::eq(Traits::to_char_type(c), truename[pos]))
Chris@16 299 matched = true;
Chris@16 300 else
Chris@16 301 truename_ok = false;
Chris@16 302 }
Chris@16 303
Chris@16 304 if (othername_ok && pos < othername.size()) {
Chris@16 305 if (Traits::eq(Traits::to_char_type(c), othername[pos]))
Chris@16 306 matched = true;
Chris@16 307 else
Chris@16 308 othername_ok = false;
Chris@16 309 }
Chris@16 310
Chris@16 311 if (matched) { ++pos; }
Chris@16 312 if (pos > falsename.size()) falsename_ok = false;
Chris@16 313 if (pos > truename.size()) truename_ok = false;
Chris@16 314 if (pos > othername.size()) othername_ok = false;
Chris@16 315 }
Chris@16 316
Chris@16 317 if (pos == 0)
Chris@16 318 in.setstate(std::ios_base::failbit);
Chris@16 319 else {
Chris@16 320 if (falsename_ok) x = false;
Chris@16 321 else if (truename_ok) x = true;
Chris@16 322 else if (othername_ok) x = indeterminate;
Chris@16 323 else in.setstate(std::ios_base::failbit);
Chris@16 324 }
Chris@16 325 }
Chris@16 326 } else {
Chris@16 327 long value;
Chris@16 328 if (in >> value) {
Chris@16 329 switch (value) {
Chris@16 330 case 0: x = false; break;
Chris@16 331 case 1: x = true; break;
Chris@16 332 case 2: x = indeterminate; break;
Chris@16 333 default: in.setstate(std::ios_base::failbit); break;
Chris@16 334 }
Chris@16 335 }
Chris@16 336 }
Chris@16 337
Chris@16 338 return in;
Chris@16 339 }
Chris@16 340
Chris@16 341 } } // end namespace boost::logic
Chris@16 342
Chris@16 343 #endif // BOOST_LOGIC_TRIBOOL_IO_HPP