1 // TR2 <bool_set> support files -*- C++ -*-
3 // Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file tr2/bool_set.tcc
26 * This is a TR2 C++ Library header.
29 #ifndef _GLIBCXX_TR2_BOOL_SET_TCC
30 #define _GLIBCXX_TR2_BOOL_SET_TCC 1
32 #pragma GCC system_header
34 namespace std _GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 bool_set::_Bool_set_val
42 { _S_true_, _S_false, _S_indet, _S_empty };
44 bool_set::_Bool_set_val
45 bool_set::_S_xor[4][4] =
46 { { _S_false, _S_true_, _S_indet, _S_empty },
47 { _S_true_, _S_false, _S_indet, _S_empty },
48 { _S_indet, _S_indet, _S_indet, _S_empty },
49 { _S_empty, _S_empty, _S_empty, _S_empty } };
51 bool_set::_Bool_set_val
52 bool_set::_S_or[4][4] =
53 { { _S_false, _S_true_, _S_indet, _S_empty },
54 { _S_true_, _S_true_, _S_true_, _S_empty },
55 { _S_indet, _S_true_, _S_indet, _S_empty },
56 { _S_empty, _S_empty, _S_empty, _S_empty } };
58 bool_set::_Bool_set_val
59 bool_set::_S_and[4][4] =
60 { { _S_false, _S_false, _S_false, _S_empty },
61 { _S_false, _S_true_, _S_indet, _S_empty },
62 { _S_false, _S_indet, _S_indet, _S_empty },
63 { _S_empty, _S_empty, _S_empty, _S_empty } };
65 bool_set::_Bool_set_val
66 bool_set::_S_eq[4][4] =
67 { { _S_true_, _S_false, _S_indet, _S_empty },
68 { _S_false, _S_true_, _S_indet, _S_empty },
69 { _S_indet, _S_indet, _S_indet, _S_empty },
70 { _S_empty, _S_empty, _S_empty, _S_empty } };
72 _GLIBCXX_END_NAMESPACE_VERSION
76 // I object to these things.
77 // The stuff in locale facets are for basic types.
78 // I think we could hack operator<< and operator>>.
81 * @brief Numeric parsing.
83 * Parses the input stream into the bool @a v. It does so by calling
86 * If ios_base::boolalpha is set, attempts to read
87 * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets
88 * @a v to true or false if successful. Sets err to
89 * ios_base::failbit if reading the string fails. Sets err to
90 * ios_base::eofbit if the stream is emptied.
92 * If ios_base::boolalpha is not set, proceeds as with reading a long,
93 * except if the value is 1, sets @a v to true, if the value is 0, sets
94 * @a v to false, and otherwise set err to ios_base::failbit.
96 * @param in Start of input stream.
97 * @param end End of input stream.
98 * @param io Source of locale and flags.
99 * @param err Error flags to set.
100 * @param v Value to format and insert.
101 * @return Iterator after reading.
103 get(iter_type __in, iter_type __end, ios_base& __io,
104 ios_base::iostate& __err, bool& __v) const
105 { return this->do_get(__in, __end, __io, __err, __v); }
108 template<typename _CharT, typename _InIter>
110 num_get<_CharT, _InIter>::
111 do_get(iter_type __beg, iter_type __end, ios_base& __io,
112 ios_base::iostate& __err, bool_set& __v) const
114 if (!(__io.flags() & ios_base::boolalpha))
116 // Parse bool values as long.
117 // NB: We can't just call do_get(long) here, as it might
118 // refer to a derived class.
120 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
121 if (__c >= _S_false && __c < _S_empty)
122 __b._M_b = static_cast<_Bool_set_val>(__c);
125 // What should we do here?
127 __err = ios_base::failbit;
129 __err |= ios_base::eofbit;
134 // Parse bool values as alphanumeric.
135 typedef __numpunct_cache<_CharT> __cache_type;
136 __use_cache<__cache_type> __uc;
137 const locale& __loc = __io._M_getloc();
138 const __cache_type* __lc = __uc(__loc);
142 bool __donef = __lc->_M_falsename_size == 0;
143 bool __donet = __lc->_M_truename_size == 0;
144 bool __testeof = false;
146 while (!__donef || !__donet)
154 const char_type __c = *__beg;
157 __testf = __c == __lc->_M_falsename[__n];
159 if (!__testf && __donet)
163 __testt = __c == __lc->_M_truename[__n];
165 if (!__testt && __donef)
168 if (!__testt && !__testf)
174 __donef = !__testf || __n >= __lc->_M_falsename_size;
175 __donet = !__testt || __n >= __lc->_M_truename_size;
177 if (__testf && __n == __lc->_M_falsename_size && __n)
180 if (__testt && __n == __lc->_M_truename_size)
181 __err = ios_base::failbit;
183 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
185 else if (__testt && __n == __lc->_M_truename_size && __n)
188 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
192 // _GLIBCXX_RESOLVE_LIB_DEFECTS
193 // 23. Num_get overflow result.
195 __err = ios_base::failbit;
197 __err |= ios_base::eofbit;
205 * @brief Numeric formatting.
207 * Formats the boolean @a v and inserts it into a stream. It does so
208 * by calling num_put::do_put().
210 * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
211 * ctype<CharT>::falsename(). Otherwise formats @a v as an int.
213 * @param s Stream to write to.
214 * @param io Source of locale and flags.
215 * @param fill Char_type to use for filling.
216 * @param v Value to format and insert.
217 * @return Iterator after writing.
219 put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const
220 { return this->do_put(__s, __f, __fill, __v); }
224 template<typename _CharT, typename _OutIter>
226 num_put<_CharT, _OutIter>::
227 do_put(iter_type __s, ios_base& __io, char_type __fill, bool_set __v) const
229 const ios_base::fmtflags __flags = __io.flags();
230 if ((__flags & ios_base::boolalpha) == 0)
232 const long __l = __v;
233 __s = _M_insert_int(__s, __io, __fill, __l);
237 typedef __numpunct_cache<_CharT> __cache_type;
238 __use_cache<__cache_type> __uc;
239 const locale& __loc = __io._M_getloc();
240 const __cache_type* __lc = __uc(__loc);
242 const _CharT* __name = __v ? __lc->_M_truename
243 : __lc->_M_falsename;
244 int __len = __v ? __lc->_M_truename_size
245 : __lc->_M_falsename_size;
247 const streamsize __w = __io.width();
248 if (__w > static_cast<streamsize>(__len))
250 const streamsize __plen = __w - __len;
252 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
255 char_traits<_CharT>::assign(__ps, __plen, __fill);
258 if ((__flags & ios_base::adjustfield) == ios_base::left)
260 __s = std::__write(__s, __name, __len);
261 __s = std::__write(__s, __ps, __plen);
265 __s = std::__write(__s, __ps, __plen);
266 __s = std::__write(__s, __name, __len);
271 __s = std::__write(__s, __name, __len);
277 #endif // _GLIBCXX_TR2_BOOL_SET_TCC