// Input streams -*- C++ -*-
-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-// 2006, 2007, 2008, 2009, 2010, 2011
-// Free Software Foundation, Inc.
+// Copyright (C) 1997-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
- // [27.6.1.1] Template class basic_istream
/**
- * @brief Controlling input.
+ * @brief Template class basic_istream.
* @ingroup io
*
+ * @tparam _CharT Type of character stream.
+ * @tparam _Traits Traits for character type, defaults to
+ * char_traits<_CharT>.
+ *
* This is the base class for all input streams. It provides text
* formatting of all builtin types, and communicates with any class
* derived from basic_streambuf to do the actual input.
{
public:
// Types (inherited from basic_ios (27.4.4)):
- typedef _CharT char_type;
+ typedef _CharT char_type;
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
- typedef _Traits traits_type;
-
+ typedef _Traits traits_type;
+
// Non-standard Types:
typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
typedef basic_ios<_CharT, _Traits> __ios_type;
typedef basic_istream<_CharT, _Traits> __istream_type;
- typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
+ typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
__num_get_type;
- typedef ctype<_CharT> __ctype_type;
+ typedef ctype<_CharT> __ctype_type;
protected:
// Data Members:
streamsize _M_gcount;
public:
- // [27.6.1.1.1] constructor/destructor
/**
* @brief Base constructor.
*
*
* This does very little apart from providing a virtual base dtor.
*/
- virtual
- ~basic_istream()
+ virtual
+ ~basic_istream()
{ _M_gcount = streamsize(0); }
- // [27.6.1.1.2] prefix/suffix
+ /// Safe prefix/suffix operations.
class sentry;
friend class sentry;
- // [27.6.1.2] formatted input
- // [27.6.1.2.3] basic_istream::operator>>
//@{
/**
* @brief Interface for manipulators.
*
* Manipulators such as @c std::ws and @c std::dec use these
- * functions in constructs like
- * <code>std::cin >> std::ws</code>.
+ * functions in constructs like
+ * <code>std::cin >> std::ws</code>.
* For more information, see the iomanip header.
*/
__istream_type&
__istream_type&
operator>>(__ios_type& (*__pf)(__ios_type&))
- {
+ {
__pf(*this);
return *this;
}
return *this;
}
//@}
-
- // [27.6.1.2.2] arithmetic extractors
+
+ //@{
/**
- * @name Arithmetic Extractors
+ * @name Extractors
*
* All the @c operator>> functions (aka <em>formatted input
* functions</em>) have some common behavior. Each starts by
* whatever data is appropriate for the type of the argument.
*
* If an exception is thrown during extraction, ios_base::badbit
- * will be turned on in the stream's error state without causing an
- * ios_base::failure to be thrown. The original exception will then
- * be rethrown.
+ * will be turned on in the stream's error state (without causing an
+ * ios_base::failure to be thrown) and the original exception will
+ * be rethrown if badbit is set in the exceptions mask.
*/
+
//@{
/**
- * @brief Basic arithmetic extractors
- * @param __n, __f, __p: A variable of builtin type.
+ * @brief Integer arithmetic extractors
+ * @param __n A variable of builtin integral type.
* @return @c *this if successful
*
* These functions use the stream's current locale (specifically, the
* @c num_get facet) to parse the input data.
*/
- __istream_type&
+ __istream_type&
operator>>(bool& __n)
{ return _M_extract(__n); }
-
- __istream_type&
+
+ __istream_type&
operator>>(short& __n);
-
- __istream_type&
+
+ __istream_type&
operator>>(unsigned short& __n)
{ return _M_extract(__n); }
- __istream_type&
+ __istream_type&
operator>>(int& __n);
-
- __istream_type&
+
+ __istream_type&
operator>>(unsigned int& __n)
{ return _M_extract(__n); }
- __istream_type&
+ __istream_type&
operator>>(long& __n)
{ return _M_extract(__n); }
-
- __istream_type&
+
+ __istream_type&
operator>>(unsigned long& __n)
{ return _M_extract(__n); }
#ifdef _GLIBCXX_USE_LONG_LONG
- __istream_type&
+ __istream_type&
operator>>(long long& __n)
{ return _M_extract(__n); }
- __istream_type&
+ __istream_type&
operator>>(unsigned long long& __n)
{ return _M_extract(__n); }
#endif
+ //@}
- __istream_type&
+ //@{
+ /**
+ * @brief Floating point arithmetic extractors
+ * @param __f A variable of builtin floating point type.
+ * @return @c *this if successful
+ *
+ * These functions use the stream's current locale (specifically, the
+ * @c num_get facet) to parse the input data.
+ */
+ __istream_type&
operator>>(float& __f)
{ return _M_extract(__f); }
- __istream_type&
+ __istream_type&
operator>>(double& __f)
{ return _M_extract(__f); }
- __istream_type&
+ __istream_type&
operator>>(long double& __f)
{ return _M_extract(__f); }
+ //@}
- __istream_type&
+ /**
+ * @brief Basic arithmetic extractors
+ * @param __p A variable of pointer type.
+ * @return @c *this if successful
+ *
+ * These functions use the stream's current locale (specifically, the
+ * @c num_get facet) to parse the input data.
+ */
+ __istream_type&
operator>>(void*& __p)
{ return _M_extract(__p); }
* in that it also constructs a sentry object and has the same error
* handling behavior.
*
- * If @a __sb is NULL, the stream will set failbit in its error state.
+ * If @p __sb is NULL, the stream will set failbit in its error state.
*
* Characters are extracted from this stream and inserted into the
- * @a sb streambuf until one of the following occurs:
+ * @p __sb streambuf until one of the following occurs:
*
* - the input stream reaches end-of-file,
* - insertion into the output buffer fails (in this case, the
*
* If the function inserts no characters, failbit is set.
*/
- __istream_type&
+ __istream_type&
operator>>(__streambuf_type* __sb);
//@}
-
+
// [27.6.1.3] unformatted input
/**
* @brief Character counting
* @return The number of characters extracted by the previous
* unformatted input function dispatched for this stream.
*/
- streamsize
- gcount() const
+ streamsize
+ gcount() const
{ return _M_gcount; }
-
+
+ //@{
/**
* @name Unformatted Input Functions
*
* by gcount().
*
* If an exception is thrown during extraction, ios_base::badbit
- * will be turned on in the stream's error state without causing an
- * ios_base::failure to be thrown. The original exception will then
- * be rethrown.
+ * will be turned on in the stream's error state (without causing an
+ * ios_base::failure to be thrown) and the original exception will
+ * be rethrown if badbit is set in the exceptions mask.
*/
- //@{
+
/**
* @brief Simple extraction.
* @return A character, or eof().
* Tries to extract a character. If none are available, sets failbit
* and returns traits::eof().
*/
- int_type
+ int_type
get();
/**
* @note This function is not overloaded on signed char and
* unsigned char.
*/
- __istream_type&
+ __istream_type&
get(char_type& __c);
/**
* @note This function is not overloaded on signed char and
* unsigned char.
*/
- __istream_type&
+ __istream_type&
get(char_type* __s, streamsize __n, char_type __delim);
/**
*
* Returns @c get(__s,__n,widen('\\n')).
*/
- __istream_type&
+ __istream_type&
get(char_type* __s, streamsize __n)
{ return this->get(__s, __n, this->widen('\n')); }
* In any case, a null character is stored in the next location in
* the array.
*/
- __istream_type&
+ __istream_type&
getline(char_type* __s, streamsize __n, char_type __delim);
/**
*
* Returns @c getline(__s,__n,widen('\\n')).
*/
- __istream_type&
+ __istream_type&
getline(char_type* __s, streamsize __n)
{ return this->getline(__s, __n, this->widen('\n')); }
* better performing implementation, while still conforming to
* the Standard.
*/
- __istream_type&
+ __istream_type&
ignore(streamsize __n, int_type __delim);
- __istream_type&
+ __istream_type&
ignore(streamsize __n);
- __istream_type&
+ __istream_type&
ignore();
/**
* returns @c traits::eof(). Otherwise reads but does not extract
* the next input character.
*/
- int_type
+ int_type
peek();
-
+
/**
* @brief Extraction without delimiters.
* @param __s A character array.
* @note This function is not overloaded on signed char and
* unsigned char.
*/
- __istream_type&
+ __istream_type&
read(char_type* __s, streamsize __n);
/**
* The goal is to empty the current buffer, and to not request any
* more from the external input sequence controlled by the streambuf.
*/
- streamsize
+ streamsize
readsome(char_type* __s, streamsize __n);
-
+
/**
* @brief Unextracting a single character.
* @param __c The character to push back into the input stream.
* are extracted, the next call to @c gcount() will return 0,
* as required by DR 60.
*/
- __istream_type&
+ __istream_type&
putback(char_type __c);
/**
* are extracted, the next call to @c gcount() will return 0,
* as required by DR 60.
*/
- __istream_type&
+ __istream_type&
unget();
/**
* extracted, if any, and therefore does not affect the next
* call to @c gcount().
*/
- int
+ int
sync();
/**
* number of characters extracted, if any, and therefore does
* not affect the next call to @c gcount().
*/
- __istream_type&
+ __istream_type&
seekg(off_type, ios_base::seekdir);
//@}
: _M_gcount(streamsize(0))
{ this->init(0); }
+#if __cplusplus >= 201103L
+ basic_istream(const basic_istream&) = delete;
+
+ basic_istream(basic_istream&& __rhs)
+ : __ios_type(), _M_gcount(__rhs._M_gcount)
+ {
+ __ios_type::move(__rhs);
+ __rhs._M_gcount = 0;
+ }
+
+ // 27.7.3.3 Assign/swap
+
+ basic_istream& operator=(const basic_istream&) = delete;
+
+ basic_istream&
+ operator=(basic_istream&& __rhs)
+ {
+ swap(__rhs);
+ return *this;
+ }
+
+ void
+ swap(basic_istream& __rhs)
+ {
+ __ios_type::swap(__rhs);
+ std::swap(_M_gcount, __rhs._M_gcount);
+ }
+#endif
+
template<typename _ValueT>
- __istream_type&
- _M_extract(_ValueT& __v);
+ __istream_type&
+ _M_extract(_ValueT& __v);
};
- // Explicit specialization declarations, defined in src/istream.cc.
- template<>
- basic_istream<char>&
+ /// Explicit specialization declarations, defined in src/istream.cc.
+ template<>
+ basic_istream<char>&
basic_istream<char>::
getline(char_type* __s, streamsize __n, char_type __delim);
-
+
template<>
basic_istream<char>&
basic_istream<char>::
ignore(streamsize __n);
-
+
template<>
basic_istream<char>&
basic_istream<char>::
ignore(streamsize __n, int_type __delim);
#ifdef _GLIBCXX_USE_WCHAR_T
- template<>
- basic_istream<wchar_t>&
+ template<>
+ basic_istream<wchar_t>&
basic_istream<wchar_t>::
getline(char_type* __s, streamsize __n, char_type __delim);
basic_istream<wchar_t>&
basic_istream<wchar_t>::
ignore(streamsize __n);
-
+
template<>
basic_istream<wchar_t>&
basic_istream<wchar_t>::
bool _M_ok;
public:
- /// Easy access to dependant types.
+ /// Easy access to dependent types.
typedef _Traits traits_type;
typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
typedef basic_istream<_CharT, _Traits> __istream_type;
* For ease of use, sentries may be converted to booleans. The
* return value is that of the sentry state (true == okay).
*/
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
explicit
#endif
operator bool() const
{ return _M_ok; }
};
- // [27.6.1.2.3] character extraction templates
//@{
/**
* @brief Character extractors
{ return (__in >> reinterpret_cast<char*>(__s)); }
//@}
- // 27.6.1.5 Template class basic_iostream
/**
- * @brief Merging istream and ostream capabilities.
+ * @brief Template class basic_iostream
* @ingroup io
*
+ * @tparam _CharT Type of character stream.
+ * @tparam _Traits Traits for character type, defaults to
+ * char_traits<_CharT>.
+ *
* This class multiply inherits from the input and output stream classes
* simply to provide a single interface.
*/
template<typename _CharT, typename _Traits>
class basic_iostream
- : public basic_istream<_CharT, _Traits>,
+ : public basic_istream<_CharT, _Traits>,
public basic_ostream<_CharT, _Traits>
{
public:
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 271. basic_iostream missing typedefs
// Types (inherited):
- typedef _CharT char_type;
+ typedef _CharT char_type;
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
- typedef _Traits traits_type;
+ typedef _Traits traits_type;
// Non-standard Types:
typedef basic_istream<_CharT, _Traits> __istream_type;
/**
* @brief Destructor does nothing.
*/
- virtual
+ virtual
~basic_iostream() { }
protected:
basic_iostream()
: __istream_type(), __ostream_type() { }
+
+#if __cplusplus >= 201103L
+ basic_iostream(const basic_iostream&) = delete;
+
+ basic_iostream(basic_iostream&& __rhs)
+ : __istream_type(std::move(__rhs)), __ostream_type(*this)
+ { }
+
+ // 27.7.3.3 Assign/swap
+
+ basic_iostream& operator=(const basic_iostream&) = delete;
+
+ basic_iostream&
+ operator=(basic_iostream&& __rhs)
+ {
+ swap(__rhs);
+ return *this;
+ }
+
+ void
+ swap(basic_iostream& __rhs)
+ { __istream_type::swap(__rhs); }
+#endif
};
- // [27.6.1.4] standard basic_istream manipulators
/**
* @brief Quick and easy way to eat whitespace
*
* std::basic_istream::sentry inside your definition of operator>>.
*/
template<typename _CharT, typename _Traits>
- basic_istream<_CharT, _Traits>&
+ basic_istream<_CharT, _Traits>&
ws(basic_istream<_CharT, _Traits>& __is);
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
+ template<typename _Ch, typename _Up>
+ basic_istream<_Ch, _Up>&
+ __is_convertible_to_basic_istream_test(basic_istream<_Ch, _Up>*);
+
+ template<typename _Tp, typename = void>
+ struct __is_convertible_to_basic_istream_impl
+ {
+ using __istream_type = void;
+ };
+
+ template<typename _Tp>
+ using __do_is_convertible_to_basic_istream_impl =
+ decltype(__is_convertible_to_basic_istream_test
+ (declval<typename remove_reference<_Tp>::type*>()));
+
+ template<typename _Tp>
+ struct __is_convertible_to_basic_istream_impl
+ <_Tp,
+ __void_t<__do_is_convertible_to_basic_istream_impl<_Tp>>>
+ {
+ using __istream_type =
+ __do_is_convertible_to_basic_istream_impl<_Tp>;
+ };
+
+ template<typename _Tp>
+ struct __is_convertible_to_basic_istream
+ : __is_convertible_to_basic_istream_impl<_Tp>
+ {
+ public:
+ using type = __not_<is_void<
+ typename __is_convertible_to_basic_istream_impl<_Tp>::__istream_type>>;
+ constexpr static bool value = type::value;
+ };
+
+ template<typename _Istream, typename _Tp, typename = void>
+ struct __is_extractable : false_type {};
+
+ template<typename _Istream, typename _Tp>
+ struct __is_extractable<_Istream, _Tp,
+ __void_t<decltype(declval<_Istream&>()
+ >> declval<_Tp>())>>
+ : true_type {};
+
+ template<typename _Istream>
+ using __rvalue_istream_type =
+ typename __is_convertible_to_basic_istream<
+ _Istream>::__istream_type;
+
// [27.7.1.6] Rvalue stream extraction
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 2328. Rvalue stream extraction should use perfect forwarding
/**
* @brief Generic extractor for rvalue stream
* @param __is An input stream.
* rvalue streams since they won't bind to the extractor functions
* that take an lvalue reference.
*/
- template<typename _CharT, typename _Traits, typename _Tp>
- inline basic_istream<_CharT, _Traits>&
- operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
- { return (__is >> __x); }
-#endif // __GXX_EXPERIMENTAL_CXX0X__
+ template<typename _Istream, typename _Tp>
+ inline
+ typename enable_if<__and_<__not_<is_lvalue_reference<_Istream>>,
+ __is_convertible_to_basic_istream<_Istream>,
+ __is_extractable<
+ __rvalue_istream_type<_Istream>,
+ _Tp&&>>::value,
+ __rvalue_istream_type<_Istream>>::type
+ operator>>(_Istream&& __is, _Tp&& __x)
+ {
+ __rvalue_istream_type<_Istream> __ret_is = __is;
+ __ret_is >> std::forward<_Tp>(__x);
+ return __ret_is;
+ }
+#endif // C++11
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace