]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/std/istream
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / istream
index 19bc42bab117fa97e0c9c269433dca37b836d562..407c1ccda499ea0ca00138586525193af1878f1a 100644 (file)
@@ -1,8 +1,6 @@
 // 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
@@ -44,11 +42,14 @@ namespace std _GLIBCXX_VISIBILITY(default)
 {
 _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.
@@ -58,19 +59,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
     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:
@@ -81,7 +82,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       streamsize               _M_gcount;
 
     public:
-      // [27.6.1.1.1] constructor/destructor
       /**
        *  @brief  Base constructor.
        *
@@ -99,23 +99,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  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&
@@ -124,7 +122,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       __istream_type&
       operator>>(__ios_type& (*__pf)(__ios_type&))
-      { 
+      {
        __pf(*this);
        return *this;
       }
@@ -136,10 +134,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        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
@@ -152,68 +150,88 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  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); }
 
@@ -225,10 +243,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  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
-       *  @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
@@ -237,20 +255,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  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
        *
@@ -267,11 +286,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  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().
@@ -279,7 +298,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  Tries to extract a character.  If none are available, sets failbit
        *  and returns traits::eof().
       */
-      int_type 
+      int_type
       get();
 
       /**
@@ -293,7 +312,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  @note  This function is not overloaded on signed char and
        *         unsigned char.
       */
-      __istream_type& 
+      __istream_type&
       get(char_type& __c);
 
       /**
@@ -320,7 +339,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  @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);
 
       /**
@@ -331,7 +350,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  Returns @c get(__s,__n,widen(&apos;\\n&apos;)).
       */
-      __istream_type& 
+      __istream_type&
       get(char_type* __s, streamsize __n)
       { return this->get(__s, __n, this->widen('\n')); }
 
@@ -393,7 +412,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  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);
 
       /**
@@ -404,7 +423,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *
        *  Returns @c getline(__s,__n,widen(&apos;\\n&apos;)).
       */
-      __istream_type& 
+      __istream_type&
       getline(char_type* __s, streamsize __n)
       { return this->getline(__s, __n, this->widen('\n')); }
 
@@ -428,13 +447,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  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();
 
       /**
@@ -445,9 +464,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  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.
@@ -463,7 +482,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  @note  This function is not overloaded on signed char and
        *         unsigned char.
       */
-      __istream_type& 
+      __istream_type&
       read(char_type* __s, streamsize __n);
 
       /**
@@ -482,9 +501,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  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.
@@ -499,7 +518,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *         are extracted, the next call to @c gcount() will return 0,
        *         as required by DR 60.
       */
-      __istream_type& 
+      __istream_type&
       putback(char_type __c);
 
       /**
@@ -515,7 +534,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *         are extracted, the next call to @c gcount() will return 0,
        *         as required by DR 60.
       */
-      __istream_type& 
+      __istream_type&
       unget();
 
       /**
@@ -533,7 +552,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *         extracted, if any, and therefore does not affect the next
        *         call to @c gcount().
       */
-      int 
+      int
       sync();
 
       /**
@@ -579,7 +598,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *         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);
       //@}
 
@@ -588,30 +607,59 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       : _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);
 
@@ -619,7 +667,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     basic_istream<wchar_t>&
     basic_istream<wchar_t>::
     ignore(streamsize __n);
-  
+
   template<>
     basic_istream<wchar_t>&
     basic_istream<wchar_t>::
@@ -641,7 +689,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       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;
@@ -680,14 +728,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  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
@@ -762,28 +809,31 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     { 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;
@@ -802,15 +852,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       /**
        *  @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
    *
@@ -832,11 +904,61 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  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.
@@ -847,11 +969,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  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