1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/logic/tribool_io.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,348 @@
1.4 +// Three-state boolean logic library
1.5 +
1.6 +// Copyright Douglas Gregor 2002-2004. Use, modification and
1.7 +// distribution is subject to the Boost Software License, Version
1.8 +// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
1.9 +// http://www.boost.org/LICENSE_1_0.txt)
1.10 +#ifndef BOOST_LOGIC_TRIBOOL_IO_HPP
1.11 +#define BOOST_LOGIC_TRIBOOL_IO_HPP
1.12 +
1.13 +#include <boost/logic/tribool.hpp>
1.14 +#include <boost/detail/workaround.hpp>
1.15 +#include <boost/noncopyable.hpp>
1.16 +
1.17 +#if BOOST_WORKAROUND(_MSC_VER, >= 1200)
1.18 +# pragma once
1.19 +#endif
1.20 +
1.21 +#ifndef BOOST_NO_STD_LOCALE
1.22 +# include <locale>
1.23 +#endif
1.24 +
1.25 +#include <string>
1.26 +#include <iostream>
1.27 +
1.28 +namespace boost { namespace logic {
1.29 +
1.30 +#ifdef BOOST_NO_STD_LOCALE
1.31 +
1.32 +/**
1.33 + * \brief Returns a string containing the default name for the \c
1.34 + * false value of a tribool with the given character type T.
1.35 + *
1.36 + * This function only exists when the C++ standard library
1.37 + * implementation does not support locales.
1.38 + */
1.39 +template<typename T> std::basic_string<T> default_false_name();
1.40 +
1.41 +/**
1.42 + * \brief Returns the character string "false".
1.43 + *
1.44 + * This function only exists when the C++ standard library
1.45 + * implementation does not support locales.
1.46 + */
1.47 +template<>
1.48 +inline std::basic_string<char> default_false_name<char>()
1.49 +{ return "false"; }
1.50 +
1.51 +# ifndef BOOST_NO_WCHAR_T
1.52 +/**
1.53 + * \brief Returns the wide character string L"false".
1.54 + *
1.55 + * This function only exists when the C++ standard library
1.56 + * implementation does not support locales.
1.57 + */
1.58 +template<>
1.59 +inline std::basic_string<wchar_t> default_false_name<wchar_t>()
1.60 +{ return L"false"; }
1.61 +# endif
1.62 +
1.63 +/**
1.64 + * \brief Returns a string containing the default name for the \c true
1.65 + * value of a tribool with the given character type T.
1.66 + *
1.67 + * This function only exists when the C++ standard library
1.68 + * implementation does not support locales.
1.69 + */
1.70 +template<typename T> std::basic_string<T> default_true_name();
1.71 +
1.72 +/**
1.73 + * \brief Returns the character string "true".
1.74 + *
1.75 + * This function only exists when the C++ standard library
1.76 + * implementation does not support locales.
1.77 + */
1.78 +template<>
1.79 +inline std::basic_string<char> default_true_name<char>()
1.80 +{ return "true"; }
1.81 +
1.82 +# ifndef BOOST_NO_WCHAR_T
1.83 +/**
1.84 + * \brief Returns the wide character string L"true".
1.85 + *
1.86 + * This function only exists * when the C++ standard library
1.87 + * implementation does not support * locales.
1.88 + */
1.89 +template<>
1.90 +inline std::basic_string<wchar_t> default_true_name<wchar_t>()
1.91 +{ return L"true"; }
1.92 +# endif
1.93 +#endif
1.94 +
1.95 +/**
1.96 + * \brief Returns a string containing the default name for the indeterminate
1.97 + * value of a tribool with the given character type T.
1.98 + *
1.99 + * This routine is used by the input and output streaming operators
1.100 + * for tribool when there is no locale support or the stream's locale
1.101 + * does not contain the indeterminate_name facet.
1.102 + */
1.103 +template<typename T> std::basic_string<T> get_default_indeterminate_name();
1.104 +
1.105 +/// Returns the character string "indeterminate".
1.106 +template<>
1.107 +inline std::basic_string<char> get_default_indeterminate_name<char>()
1.108 +{ return "indeterminate"; }
1.109 +
1.110 +#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
1.111 +// VC++ 6.0 chokes on the specialization below, so we're stuck without
1.112 +// wchar_t support. What a pain. TODO: it might just need a the template
1.113 +// parameter as function parameter...
1.114 +#else
1.115 +# ifndef BOOST_NO_WCHAR_T
1.116 +/// Returns the wide character string L"indeterminate".
1.117 +template<>
1.118 +inline std::basic_string<wchar_t> get_default_indeterminate_name<wchar_t>()
1.119 +{ return L"indeterminate"; }
1.120 +# endif
1.121 +#endif
1.122 +
1.123 +// http://www.cantrip.org/locale.html
1.124 +
1.125 +#ifndef BOOST_NO_STD_LOCALE
1.126 +/**
1.127 + * \brief A locale facet specifying the name of the indeterminate
1.128 + * value of a tribool.
1.129 + *
1.130 + * The facet is used to perform I/O on tribool values when \c
1.131 + * std::boolalpha has been specified. This class template is only
1.132 + * available if the C++ standard library implementation supports
1.133 + * locales.
1.134 + */
1.135 +template<typename CharT>
1.136 +class indeterminate_name : public std::locale::facet, private boost::noncopyable
1.137 +{
1.138 +public:
1.139 + typedef CharT char_type;
1.140 + typedef std::basic_string<CharT> string_type;
1.141 +
1.142 + /// Construct the facet with the default name
1.143 + indeterminate_name() : name_(get_default_indeterminate_name<CharT>()) {}
1.144 +
1.145 + /// Construct the facet with the given name for the indeterminate value
1.146 + explicit indeterminate_name(const string_type& name) : name_(name) {}
1.147 +
1.148 + /// Returns the name for the indeterminate value
1.149 + string_type name() const { return name_; }
1.150 +
1.151 + /// Uniquily identifies this facet with the locale.
1.152 + static std::locale::id id;
1.153 +
1.154 +private:
1.155 + string_type name_;
1.156 +};
1.157 +
1.158 +template<typename CharT> std::locale::id indeterminate_name<CharT>::id;
1.159 +#endif
1.160 +
1.161 +/**
1.162 + * \brief Writes the value of a tribool to a stream.
1.163 + *
1.164 + * When the value of @p x is either \c true or \c false, this routine
1.165 + * is semantically equivalent to:
1.166 + * \code out << static_cast<bool>(x); \endcode
1.167 + *
1.168 + * When @p x has an indeterminate value, it outputs either the integer
1.169 + * value 2 (if <tt>(out.flags() & std::ios_base::boolalpha) == 0</tt>)
1.170 + * or the name of the indeterminate value. The name of the
1.171 + * indeterminate value comes from the indeterminate_name facet (if it
1.172 + * is defined in the output stream's locale), or from the
1.173 + * get_default_indeterminate_name function (if it is not defined in the
1.174 + * locale or if the C++ standard library implementation does not
1.175 + * support locales).
1.176 + *
1.177 + * \returns @p out
1.178 + */
1.179 +template<typename CharT, typename Traits>
1.180 +inline std::basic_ostream<CharT, Traits>&
1.181 +operator<<(std::basic_ostream<CharT, Traits>& out, tribool x)
1.182 +{
1.183 + if (!indeterminate(x)) {
1.184 + out << static_cast<bool>(x);
1.185 + } else {
1.186 + typename std::basic_ostream<CharT, Traits>::sentry cerberus(out);
1.187 + if (cerberus) {
1.188 + if (out.flags() & std::ios_base::boolalpha) {
1.189 +#ifndef BOOST_NO_STD_LOCALE
1.190 + if (BOOST_HAS_FACET(indeterminate_name<CharT>, out.getloc())) {
1.191 + const indeterminate_name<CharT>& facet =
1.192 + BOOST_USE_FACET(indeterminate_name<CharT>, out.getloc());
1.193 + out << facet.name();
1.194 + } else {
1.195 + out << get_default_indeterminate_name<CharT>();
1.196 + }
1.197 +#else
1.198 + out << get_default_indeterminate_name<CharT>();
1.199 +#endif
1.200 + }
1.201 + else
1.202 + out << 2;
1.203 + }
1.204 + }
1.205 + return out;
1.206 +}
1.207 +
1.208 +/**
1.209 + * \brief Writes the indeterminate tribool value to a stream.
1.210 + *
1.211 + * This routine outputs either the integer
1.212 + * value 2 (if <tt>(out.flags() & std::ios_base::boolalpha) == 0</tt>)
1.213 + * or the name of the indeterminate value. The name of the
1.214 + * indeterminate value comes from the indeterminate_name facet (if it
1.215 + * is defined in the output stream's locale), or from the
1.216 + * get_default_indeterminate_name function (if it is not defined in the
1.217 + * locale or if the C++ standard library implementation does not
1.218 + * support locales).
1.219 + *
1.220 + * \returns @p out
1.221 + */
1.222 +template<typename CharT, typename Traits>
1.223 +inline std::basic_ostream<CharT, Traits>&
1.224 +operator<<(std::basic_ostream<CharT, Traits>& out,
1.225 + bool (*)(tribool, detail::indeterminate_t))
1.226 +{ return out << tribool(indeterminate); }
1.227 +
1.228 +/**
1.229 + * \brief Reads a tribool value from a stream.
1.230 + *
1.231 + * When <tt>(out.flags() & std::ios_base::boolalpha) == 0</tt>, this
1.232 + * function reads a \c long value from the input stream @p in and
1.233 + * converts that value to a tribool. If that value is 0, @p x becomes
1.234 + * \c false; if it is 1, @p x becomes \c true; if it is 2, @p becomes
1.235 + * \c indetermine; otherwise, the operation fails (and the fail bit is
1.236 + * set on the input stream @p in).
1.237 + *
1.238 + * When <tt>(out.flags() & std::ios_base::boolalpha) != 0</tt>, this
1.239 + * function first determines the names of the false, true, and
1.240 + * indeterminate values. The false and true names are extracted from
1.241 + * the \c std::numpunct facet of the input stream's locale (if the C++
1.242 + * standard library implementation supports locales), or from the \c
1.243 + * default_false_name and \c default_true_name functions (if there is
1.244 + * no locale support). The indeterminate name is extracted from the
1.245 + * appropriate \c indeterminate_name facet (if it is available in the
1.246 + * input stream's locale), or from the \c get_default_indeterminate_name
1.247 + * function (if the C++ standard library implementation does not
1.248 + * support locales, or the \c indeterminate_name facet is not
1.249 + * specified for this locale object). The input is then matched to
1.250 + * each of these names, and the tribool @p x is assigned the value
1.251 + * corresponding to the longest name that matched. If no name is
1.252 + * matched or all names are empty, the operation fails (and the fail
1.253 + * bit is set on the input stream @p in).
1.254 + *
1.255 + * \returns @p in
1.256 + */
1.257 +template<typename CharT, typename Traits>
1.258 +inline std::basic_istream<CharT, Traits>&
1.259 +operator>>(std::basic_istream<CharT, Traits>& in, tribool& x)
1.260 +{
1.261 + if (in.flags() & std::ios_base::boolalpha) {
1.262 + typename std::basic_istream<CharT, Traits>::sentry cerberus(in);
1.263 + if (cerberus) {
1.264 + typedef std::basic_string<CharT> string_type;
1.265 +
1.266 +#ifndef BOOST_NO_STD_LOCALE
1.267 + const std::numpunct<CharT>& numpunct_facet =
1.268 + BOOST_USE_FACET(std::numpunct<CharT>, in.getloc());
1.269 +
1.270 + string_type falsename = numpunct_facet.falsename();
1.271 + string_type truename = numpunct_facet.truename();
1.272 +
1.273 + string_type othername;
1.274 + if (BOOST_HAS_FACET(indeterminate_name<CharT>, in.getloc())) {
1.275 + othername =
1.276 + BOOST_USE_FACET(indeterminate_name<CharT>, in.getloc()).name();
1.277 + } else {
1.278 + othername = get_default_indeterminate_name<CharT>();
1.279 + }
1.280 +#else
1.281 + string_type falsename = default_false_name<CharT>();
1.282 + string_type truename = default_true_name<CharT>();
1.283 + string_type othername = get_default_indeterminate_name<CharT>();
1.284 +#endif
1.285 +
1.286 + typename string_type::size_type pos = 0;
1.287 + bool falsename_ok = true, truename_ok = true, othername_ok = true;
1.288 +
1.289 + // Modeled after the code from Library DR 17
1.290 + while (falsename_ok && pos < falsename.size()
1.291 + || truename_ok && pos < truename.size()
1.292 + || othername_ok && pos < othername.size()) {
1.293 + typename Traits::int_type c = in.get();
1.294 + if (c == Traits::eof())
1.295 + return in;
1.296 +
1.297 + bool matched = false;
1.298 + if (falsename_ok && pos < falsename.size()) {
1.299 + if (Traits::eq(Traits::to_char_type(c), falsename[pos]))
1.300 + matched = true;
1.301 + else
1.302 + falsename_ok = false;
1.303 + }
1.304 +
1.305 + if (truename_ok && pos < truename.size()) {
1.306 + if (Traits::eq(Traits::to_char_type(c), truename[pos]))
1.307 + matched = true;
1.308 + else
1.309 + truename_ok = false;
1.310 + }
1.311 +
1.312 + if (othername_ok && pos < othername.size()) {
1.313 + if (Traits::eq(Traits::to_char_type(c), othername[pos]))
1.314 + matched = true;
1.315 + else
1.316 + othername_ok = false;
1.317 + }
1.318 +
1.319 + if (matched) { ++pos; }
1.320 + if (pos > falsename.size()) falsename_ok = false;
1.321 + if (pos > truename.size()) truename_ok = false;
1.322 + if (pos > othername.size()) othername_ok = false;
1.323 + }
1.324 +
1.325 + if (pos == 0)
1.326 + in.setstate(std::ios_base::failbit);
1.327 + else {
1.328 + if (falsename_ok) x = false;
1.329 + else if (truename_ok) x = true;
1.330 + else if (othername_ok) x = indeterminate;
1.331 + else in.setstate(std::ios_base::failbit);
1.332 + }
1.333 + }
1.334 + } else {
1.335 + long value;
1.336 + if (in >> value) {
1.337 + switch (value) {
1.338 + case 0: x = false; break;
1.339 + case 1: x = true; break;
1.340 + case 2: x = indeterminate; break;
1.341 + default: in.setstate(std::ios_base::failbit); break;
1.342 + }
1.343 + }
1.344 + }
1.345 +
1.346 + return in;
1.347 +}
1.348 +
1.349 +} } // end namespace boost::logic
1.350 +
1.351 +#endif // BOOST_LOGIC_TRIBOOL_IO_HPP