PR libstdc++/82917 add missing returns in <fstream>
[official-gcc.git] / libstdc++-v3 / include / std / fstream
blob26176afccd0192eb4a8075e7092fa473efb48eca
1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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 include/fstream
26  *  This is a Standard C++ Library header.
27  */
30 // ISO C++ 14882: 27.8  File-based streams
33 #ifndef _GLIBCXX_FSTREAM
34 #define _GLIBCXX_FSTREAM 1
36 #pragma GCC system_header
38 #include <istream>
39 #include <ostream>
40 #include <bits/codecvt.h>
41 #include <cstdio>             // For BUFSIZ
42 #include <bits/basic_file.h>  // For __basic_file, __c_lock
43 #if __cplusplus >= 201103L
44 #include <string>             // For std::string overloads.
45 #endif
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51   // [27.8.1.1] template class basic_filebuf
52   /**
53    *  @brief  The actual work of input and output (for files).
54    *  @ingroup io
55    *
56    *  @tparam _CharT  Type of character stream.
57    *  @tparam _Traits  Traits for character type, defaults to
58    *                   char_traits<_CharT>.
59    *
60    *  This class associates both its input and output sequence with an
61    *  external disk file, and maintains a joint file position for both
62    *  sequences.  Many of its semantics are described in terms of similar
63    *  behavior in the Standard C Library's @c FILE streams.
64    *
65    *  Requirements on traits_type, specific to this class:
66    *  - traits_type::pos_type must be fpos<traits_type::state_type>
67    *  - traits_type::off_type must be streamoff
68    *  - traits_type::state_type must be Assignable and DefaultConstructible,
69    *  - traits_type::state_type() must be the initial state for codecvt.
70    */
71   template<typename _CharT, typename _Traits>
72     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
73     {
74 #if __cplusplus >= 201103L
75       template<typename _Tp>
76         using __chk_state = __and_<is_copy_assignable<_Tp>,
77                                    is_copy_constructible<_Tp>,
78                                    is_default_constructible<_Tp>>;
80       static_assert(__chk_state<typename _Traits::state_type>::value,
81                     "state_type must be CopyAssignable, CopyConstructible"
82                     " and DefaultConstructible");
84       static_assert(is_same<typename _Traits::pos_type,
85                             fpos<typename _Traits::state_type>>::value,
86                     "pos_type must be fpos<state_type>");
87 #endif
88     public:
89       // Types:
90       typedef _CharT                                    char_type;
91       typedef _Traits                                   traits_type;
92       typedef typename traits_type::int_type            int_type;
93       typedef typename traits_type::pos_type            pos_type;
94       typedef typename traits_type::off_type            off_type;
96       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
97       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
98       typedef __basic_file<char>                        __file_type;
99       typedef typename traits_type::state_type          __state_type;
100       typedef codecvt<char_type, char, __state_type>    __codecvt_type;
102       friend class ios_base; // For sync_with_stdio.
104     protected:
105       // Data Members:
106       // MT lock inherited from libio or other low-level io library.
107       __c_lock                  _M_lock;
109       // External buffer.
110       __file_type               _M_file;
112       /// Place to stash in || out || in | out settings for current filebuf.
113       ios_base::openmode        _M_mode;
115       // Beginning state type for codecvt.
116       __state_type              _M_state_beg;
118       // During output, the state that corresponds to pptr(),
119       // during input, the state that corresponds to egptr() and
120       // _M_ext_next.
121       __state_type              _M_state_cur;
123       // Not used for output. During input, the state that corresponds
124       // to eback() and _M_ext_buf.
125       __state_type              _M_state_last;
127       /// Pointer to the beginning of internal buffer.
128       char_type*                _M_buf;
130       /**
131        *  Actual size of internal buffer. This number is equal to the size
132        *  of the put area + 1 position, reserved for the overflow char of
133        *  a full area.
134        */
135       size_t                    _M_buf_size;
137       // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
138       bool                      _M_buf_allocated;
140       /**
141        *  _M_reading == false && _M_writing == false for @b uncommitted mode;
142        *  _M_reading == true for @b read mode;
143        *  _M_writing == true for @b write mode;
144        *
145        *  NB: _M_reading == true && _M_writing == true is unused.
146        */
147       bool                      _M_reading;
148       bool                      _M_writing;
150       //@{
151       /**
152        *  Necessary bits for putback buffer management.
153        *
154        *  @note pbacks of over one character are not currently supported.
155        */
156       char_type                 _M_pback;
157       char_type*                _M_pback_cur_save;
158       char_type*                _M_pback_end_save;
159       bool                      _M_pback_init;
160       //@}
162       // Cached codecvt facet.
163       const __codecvt_type*     _M_codecvt;
165       /**
166        *  Buffer for external characters. Used for input when
167        *  codecvt::always_noconv() == false. When valid, this corresponds
168        *  to eback().
169        */
170       char*                     _M_ext_buf;
172       /**
173        *  Size of buffer held by _M_ext_buf.
174        */
175       streamsize                _M_ext_buf_size;
177       /**
178        *  Pointers into the buffer held by _M_ext_buf that delimit a
179        *  subsequence of bytes that have been read but not yet converted.
180        *  When valid, _M_ext_next corresponds to egptr().
181        */
182       const char*               _M_ext_next;
183       char*                     _M_ext_end;
185       /**
186        *  Initializes pback buffers, and moves normal buffers to safety.
187        *  Assumptions:
188        *  _M_in_cur has already been moved back
189        */
190       void
191       _M_create_pback()
192       {
193         if (!_M_pback_init)
194           {
195             _M_pback_cur_save = this->gptr();
196             _M_pback_end_save = this->egptr();
197             this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
198             _M_pback_init = true;
199           }
200       }
202       /**
203        *  Deactivates pback buffer contents, and restores normal buffer.
204        *  Assumptions:
205        *  The pback buffer has only moved forward.
206        */
207       void
208       _M_destroy_pback() throw()
209       {
210         if (_M_pback_init)
211           {
212             // Length _M_in_cur moved in the pback buffer.
213             _M_pback_cur_save += this->gptr() != this->eback();
214             this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
215             _M_pback_init = false;
216           }
217       }
219 #if __cplusplus >= 201703L
220       template<typename _Path, typename _Result = _Path, typename _Path2
221                = decltype(std::declval<_Path&>().make_preferred().native())>
222         using _If_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>;
223 #endif // C++17
225     public:
226       // Constructors/destructor:
227       /**
228        *  @brief  Does not open any files.
229        *
230        *  The default constructor initializes the parent class using its
231        *  own default ctor.
232        */
233       basic_filebuf();
235 #if __cplusplus >= 201103L
236       basic_filebuf(const basic_filebuf&) = delete;
237       basic_filebuf(basic_filebuf&&);
238 #endif
240       /**
241        *  @brief  The destructor closes the file first.
242        */
243       virtual
244       ~basic_filebuf()
245       { this->close(); }
247 #if __cplusplus >= 201103L
248       basic_filebuf& operator=(const basic_filebuf&) = delete;
249       basic_filebuf& operator=(basic_filebuf&&);
250       void swap(basic_filebuf&);
251 #endif
253       // Members:
254       /**
255        *  @brief  Returns true if the external file is open.
256        */
257       bool
258       is_open() const throw()
259       { return _M_file.is_open(); }
261       /**
262        *  @brief  Opens an external file.
263        *  @param  __s  The name of the file.
264        *  @param  __mode  The open mode flags.
265        *  @return  @c this on success, NULL on failure
266        *
267        *  If a file is already open, this function immediately fails.
268        *  Otherwise it tries to open the file named @a __s using the flags
269        *  given in @a __mode.
270        *
271        *  Table 92, adapted here, gives the relation between openmode
272        *  combinations and the equivalent @c fopen() flags.
273        *  (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
274        *  and binary|in|app per DR 596)
275        *  <pre>
276        *  +---------------------------------------------------------+
277        *  | ios_base Flag combination            stdio equivalent   |
278        *  |binary  in  out  trunc  app                              |
279        *  +---------------------------------------------------------+
280        *  |             +                        w                  |
281        *  |             +           +            a                  |
282        *  |                         +            a                  |
283        *  |             +     +                  w                  |
284        *  |         +                            r                  |
285        *  |         +   +                        r+                 |
286        *  |         +   +     +                  w+                 |
287        *  |         +   +           +            a+                 |
288        *  |         +               +            a+                 |
289        *  +---------------------------------------------------------+
290        *  |   +         +                        wb                 |
291        *  |   +         +           +            ab                 |
292        *  |   +                     +            ab                 |
293        *  |   +         +     +                  wb                 |
294        *  |   +     +                            rb                 |
295        *  |   +     +   +                        r+b                |
296        *  |   +     +   +     +                  w+b                |
297        *  |   +     +   +           +            a+b                |
298        *  |   +     +               +            a+b                |
299        *  +---------------------------------------------------------+
300        *  </pre>
301        */
302       __filebuf_type*
303       open(const char* __s, ios_base::openmode __mode);
305 #if __cplusplus >= 201103L
306       /**
307        *  @brief  Opens an external file.
308        *  @param  __s  The name of the file.
309        *  @param  __mode  The open mode flags.
310        *  @return  @c this on success, NULL on failure
311        */
312       __filebuf_type*
313       open(const std::string& __s, ios_base::openmode __mode)
314       { return open(__s.c_str(), __mode); }
316 #if __cplusplus >= 201703L
317       /**
318        *  @brief  Opens an external file.
319        *  @param  __s  The name of the file, as a filesystem::path.
320        *  @param  __mode  The open mode flags.
321        *  @return  @c this on success, NULL on failure
322        */
323       template<typename _Path>
324         _If_path<_Path, __filebuf_type*>
325         open(const _Path& __s, ios_base::openmode __mode)
326         { return open(__s.c_str(), __mode); }
327 #endif // C++17
328 #endif // C++11
330       /**
331        *  @brief  Closes the currently associated file.
332        *  @return  @c this on success, NULL on failure
333        *
334        *  If no file is currently open, this function immediately fails.
335        *
336        *  If a <em>put buffer area</em> exists, @c overflow(eof) is
337        *  called to flush all the characters.  The file is then
338        *  closed.
339        *
340        *  If any operations fail, this function also fails.
341        */
342       __filebuf_type*
343       close();
345     protected:
346       void
347       _M_allocate_internal_buffer();
349       void
350       _M_destroy_internal_buffer() throw();
352       // [27.8.1.4] overridden virtual functions
353       virtual streamsize
354       showmanyc();
356       // Stroustrup, 1998, p. 628
357       // underflow() and uflow() functions are called to get the next
358       // character from the real input source when the buffer is empty.
359       // Buffered input uses underflow()
361       virtual int_type
362       underflow();
364       virtual int_type
365       pbackfail(int_type __c = _Traits::eof());
367       // Stroustrup, 1998, p 648
368       // The overflow() function is called to transfer characters to the
369       // real output destination when the buffer is full. A call to
370       // overflow(c) outputs the contents of the buffer plus the
371       // character c.
372       // 27.5.2.4.5
373       // Consume some sequence of the characters in the pending sequence.
374       virtual int_type
375       overflow(int_type __c = _Traits::eof());
377       // Convert internal byte sequence to external, char-based
378       // sequence via codecvt.
379       bool
380       _M_convert_to_external(char_type*, streamsize);
382       /**
383        *  @brief  Manipulates the buffer.
384        *  @param  __s  Pointer to a buffer area.
385        *  @param  __n  Size of @a __s.
386        *  @return  @c this
387        *
388        *  If no file has been opened, and both @a __s and @a __n are zero, then
389        *  the stream becomes unbuffered.  Otherwise, @c __s is used as a
390        *  buffer; see
391        *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
392        *  for more.
393        */
394       virtual __streambuf_type*
395       setbuf(char_type* __s, streamsize __n);
397       virtual pos_type
398       seekoff(off_type __off, ios_base::seekdir __way,
399               ios_base::openmode __mode = ios_base::in | ios_base::out);
401       virtual pos_type
402       seekpos(pos_type __pos,
403               ios_base::openmode __mode = ios_base::in | ios_base::out);
405       // Common code for seekoff, seekpos, and overflow
406       pos_type
407       _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
409       int
410       _M_get_ext_pos(__state_type &__state);
412       virtual int
413       sync();
415       virtual void
416       imbue(const locale& __loc);
418       virtual streamsize
419       xsgetn(char_type* __s, streamsize __n);
421       virtual streamsize
422       xsputn(const char_type* __s, streamsize __n);
424       // Flushes output buffer, then writes unshift sequence.
425       bool
426       _M_terminate_output();
428       /**
429        *  This function sets the pointers of the internal buffer, both get
430        *  and put areas. Typically:
431        *
432        *   __off == egptr() - eback() upon underflow/uflow (@b read mode);
433        *   __off == 0 upon overflow (@b write mode);
434        *   __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
435        *
436        *  NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
437        *  reflects the actual allocated memory and the last cell is reserved
438        *  for the overflow char of a full put area.
439        */
440       void
441       _M_set_buffer(streamsize __off)
442       {
443         const bool __testin = _M_mode & ios_base::in;
444         const bool __testout = (_M_mode & ios_base::out
445                                 || _M_mode & ios_base::app);
447         if (__testin && __off > 0)
448           this->setg(_M_buf, _M_buf, _M_buf + __off);
449         else
450           this->setg(_M_buf, _M_buf, _M_buf);
452         if (__testout && __off == 0 && _M_buf_size > 1 )
453           this->setp(_M_buf, _M_buf + _M_buf_size - 1);
454         else
455           this->setp(0, 0);
456       }
457     };
459   // [27.8.1.5] Template class basic_ifstream
460   /**
461    *  @brief  Controlling input for files.
462    *  @ingroup io
463    *
464    *  @tparam _CharT  Type of character stream.
465    *  @tparam _Traits  Traits for character type, defaults to
466    *                   char_traits<_CharT>.
467    *
468    *  This class supports reading from named files, using the inherited
469    *  functions from std::basic_istream.  To control the associated
470    *  sequence, an instance of std::basic_filebuf is used, which this page
471    *  refers to as @c sb.
472    */
473   template<typename _CharT, typename _Traits>
474     class basic_ifstream : public basic_istream<_CharT, _Traits>
475     {
476     public:
477       // Types:
478       typedef _CharT                                    char_type;
479       typedef _Traits                                   traits_type;
480       typedef typename traits_type::int_type            int_type;
481       typedef typename traits_type::pos_type            pos_type;
482       typedef typename traits_type::off_type            off_type;
484       // Non-standard types:
485       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
486       typedef basic_istream<char_type, traits_type>     __istream_type;
488     private:
489       __filebuf_type    _M_filebuf;
491     public:
492       // Constructors/Destructors:
493       /**
494        *  @brief  Default constructor.
495        *
496        *  Initializes @c sb using its default constructor, and passes
497        *  @c &sb to the base class initializer.  Does not open any files
498        *  (you haven't given it a filename to open).
499        */
500       basic_ifstream() : __istream_type(), _M_filebuf()
501       { this->init(&_M_filebuf); }
503       /**
504        *  @brief  Create an input file stream.
505        *  @param  __s  Null terminated string specifying the filename.
506        *  @param  __mode  Open file in specified mode (see std::ios_base).
507        *
508        *  @c ios_base::in is automatically included in @a __mode.
509        */
510       explicit
511       basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
512       : __istream_type(), _M_filebuf()
513       {
514         this->init(&_M_filebuf);
515         this->open(__s, __mode);
516       }
518 #if __cplusplus >= 201103L
519       /**
520        *  @brief  Create an input file stream.
521        *  @param  __s  std::string specifying the filename.
522        *  @param  __mode  Open file in specified mode (see std::ios_base).
523        *
524        *  @c ios_base::in is automatically included in @a __mode.
525        */
526       explicit
527       basic_ifstream(const std::string& __s,
528                      ios_base::openmode __mode = ios_base::in)
529       : __istream_type(), _M_filebuf()
530       {
531         this->init(&_M_filebuf);
532         this->open(__s, __mode);
533       }
535 #if __cplusplus >= 201703L
536       /**
537        *  @param  Create an input file stream.
538        *  @param  __s  filesystem::path specifying the filename.
539        *  @param  __mode  Open file in specified mode (see std::ios_base).
540        *
541        *  @c ios_base::in is automatically included in @a __mode.
542        */
543       template<typename _Path, typename = _Require<
544           is_constructible<__filebuf_type, const _Path&, ios_base::openmode>>>
545         basic_ifstream(const _Path& __s,
546                        ios_base::openmode __mode = ios_base::in)
547         : basic_ifstream(__s.c_str(), __mode)
548         { }
549 #endif // C++17
551       basic_ifstream(const basic_ifstream&) = delete;
553       basic_ifstream(basic_ifstream&& __rhs)
554       : __istream_type(std::move(__rhs)),
555       _M_filebuf(std::move(__rhs._M_filebuf))
556       { __istream_type::set_rdbuf(&_M_filebuf); }
557 #endif // C++11
559       /**
560        *  @brief  The destructor does nothing.
561        *
562        *  The file is closed by the filebuf object, not the formatting
563        *  stream.
564        */
565       ~basic_ifstream()
566       { }
568 #if __cplusplus >= 201103L
569       // 27.8.3.2 Assign and swap:
571       basic_ifstream&
572       operator=(const basic_ifstream&) = delete;
574       basic_ifstream&
575       operator=(basic_ifstream&& __rhs)
576       {
577         __istream_type::operator=(std::move(__rhs));
578         _M_filebuf = std::move(__rhs._M_filebuf);
579         return *this;
580       }
582       void
583       swap(basic_ifstream& __rhs)
584       {
585         __istream_type::swap(__rhs);
586         _M_filebuf.swap(__rhs._M_filebuf);
587       }
588 #endif
590       // Members:
591       /**
592        *  @brief  Accessing the underlying buffer.
593        *  @return  The current basic_filebuf buffer.
594        *
595        *  This hides both signatures of std::basic_ios::rdbuf().
596        */
597       __filebuf_type*
598       rdbuf() const
599       { return const_cast<__filebuf_type*>(&_M_filebuf); }
601       /**
602        *  @brief  Wrapper to test for an open file.
603        *  @return  @c rdbuf()->is_open()
604        */
605       bool
606       is_open()
607       { return _M_filebuf.is_open(); }
609       // _GLIBCXX_RESOLVE_LIB_DEFECTS
610       // 365. Lack of const-qualification in clause 27
611       bool
612       is_open() const
613       { return _M_filebuf.is_open(); }
615       /**
616        *  @brief  Opens an external file.
617        *  @param  __s  The name of the file.
618        *  @param  __mode  The open mode flags.
619        *
620        *  Calls @c std::basic_filebuf::open(s,__mode|in).  If that function
621        *  fails, @c failbit is set in the stream's error state.
622        */
623       void
624       open(const char* __s, ios_base::openmode __mode = ios_base::in)
625       {
626         if (!_M_filebuf.open(__s, __mode | ios_base::in))
627           this->setstate(ios_base::failbit);
628         else
629           // _GLIBCXX_RESOLVE_LIB_DEFECTS
630           // 409. Closing an fstream should clear error state
631           this->clear();
632       }
634 #if __cplusplus >= 201103L
635       /**
636        *  @brief  Opens an external file.
637        *  @param  __s  The name of the file.
638        *  @param  __mode  The open mode flags.
639        *
640        *  Calls @c std::basic_filebuf::open(__s,__mode|in).  If that function
641        *  fails, @c failbit is set in the stream's error state.
642        */
643       void
644       open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
645       {
646         if (!_M_filebuf.open(__s, __mode | ios_base::in))
647           this->setstate(ios_base::failbit);
648         else
649           // _GLIBCXX_RESOLVE_LIB_DEFECTS
650           // 409. Closing an fstream should clear error state
651           this->clear();
652       }
654 #if __cplusplus >= 201703L
655       /**
656        *  @brief  Opens an external file.
657        *  @param  __s  The name of the file, as a filesystem::path.
658        *  @param  __mode  The open mode flags.
659        *
660        *  Calls @c std::basic_filebuf::open(__s,__mode|in).  If that function
661        *  fails, @c failbit is set in the stream's error state.
662        */
663       template<typename _Path>
664         auto
665         open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
666         -> decltype((void)_M_filebuf.open(__s, __mode))
667         { open(__s.c_str(), __mode); }
668 #endif // C++17
669 #endif // C++11
671       /**
672        *  @brief  Close the file.
673        *
674        *  Calls @c std::basic_filebuf::close().  If that function
675        *  fails, @c failbit is set in the stream's error state.
676        */
677       void
678       close()
679       {
680         if (!_M_filebuf.close())
681           this->setstate(ios_base::failbit);
682       }
683     };
686   // [27.8.1.8] Template class basic_ofstream
687   /**
688    *  @brief  Controlling output for files.
689    *  @ingroup io
690    *
691    *  @tparam _CharT  Type of character stream.
692    *  @tparam _Traits  Traits for character type, defaults to
693    *                   char_traits<_CharT>.
694    *
695    *  This class supports reading from named files, using the inherited
696    *  functions from std::basic_ostream.  To control the associated
697    *  sequence, an instance of std::basic_filebuf is used, which this page
698    *  refers to as @c sb.
699    */
700   template<typename _CharT, typename _Traits>
701     class basic_ofstream : public basic_ostream<_CharT,_Traits>
702     {
703     public:
704       // Types:
705       typedef _CharT                                    char_type;
706       typedef _Traits                                   traits_type;
707       typedef typename traits_type::int_type            int_type;
708       typedef typename traits_type::pos_type            pos_type;
709       typedef typename traits_type::off_type            off_type;
711       // Non-standard types:
712       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
713       typedef basic_ostream<char_type, traits_type>     __ostream_type;
715     private:
716       __filebuf_type    _M_filebuf;
718     public:
719       // Constructors:
720       /**
721        *  @brief  Default constructor.
722        *
723        *  Initializes @c sb using its default constructor, and passes
724        *  @c &sb to the base class initializer.  Does not open any files
725        *  (you haven't given it a filename to open).
726        */
727       basic_ofstream(): __ostream_type(), _M_filebuf()
728       { this->init(&_M_filebuf); }
730       /**
731        *  @brief  Create an output file stream.
732        *  @param  __s  Null terminated string specifying the filename.
733        *  @param  __mode  Open file in specified mode (see std::ios_base).
734        *
735        *  @c ios_base::out is automatically included in @a __mode.
736        */
737       explicit
738       basic_ofstream(const char* __s,
739                      ios_base::openmode __mode = ios_base::out)
740       : __ostream_type(), _M_filebuf()
741       {
742         this->init(&_M_filebuf);
743         this->open(__s, __mode);
744       }
746 #if __cplusplus >= 201103L
747       /**
748        *  @brief  Create an output file stream.
749        *  @param  __s  std::string specifying the filename.
750        *  @param  __mode  Open file in specified mode (see std::ios_base).
751        *
752        *  @c ios_base::out is automatically included in @a __mode.
753        */
754       explicit
755       basic_ofstream(const std::string& __s,
756                      ios_base::openmode __mode = ios_base::out)
757       : __ostream_type(), _M_filebuf()
758       {
759         this->init(&_M_filebuf);
760         this->open(__s, __mode);
761       }
763 #if __cplusplus >= 201703L
764       /**
765        *  @param  Create an output file stream.
766        *  @param  __s  filesystem::path specifying the filename.
767        *  @param  __mode  Open file in specified mode (see std::ios_base).
768        *
769        *  @c ios_base::out is automatically included in @a __mode.
770        */
771       template<typename _Path, typename = _Require<
772           is_constructible<__filebuf_type, const _Path&, ios_base::openmode>>>
773         basic_ofstream(const _Path& __s,
774                        ios_base::openmode __mode = ios_base::out)
775         : basic_ofstream(__s.c_str(), __mode)
776         { }
777 #endif // C++17
779       basic_ofstream(const basic_ofstream&) = delete;
781       basic_ofstream(basic_ofstream&& __rhs)
782       : __ostream_type(std::move(__rhs)),
783       _M_filebuf(std::move(__rhs._M_filebuf))
784       { __ostream_type::set_rdbuf(&_M_filebuf); }
785 #endif
787       /**
788        *  @brief  The destructor does nothing.
789        *
790        *  The file is closed by the filebuf object, not the formatting
791        *  stream.
792        */
793       ~basic_ofstream()
794       { }
796 #if __cplusplus >= 201103L
797       // 27.8.3.2 Assign and swap:
799       basic_ofstream&
800       operator=(const basic_ofstream&) = delete;
802       basic_ofstream&
803       operator=(basic_ofstream&& __rhs)
804       {
805         __ostream_type::operator=(std::move(__rhs));
806         _M_filebuf = std::move(__rhs._M_filebuf);
807         return *this;
808       }
810       void
811       swap(basic_ofstream& __rhs)
812       {
813         __ostream_type::swap(__rhs);
814         _M_filebuf.swap(__rhs._M_filebuf);
815       }
816 #endif
818       // Members:
819       /**
820        *  @brief  Accessing the underlying buffer.
821        *  @return  The current basic_filebuf buffer.
822        *
823        *  This hides both signatures of std::basic_ios::rdbuf().
824        */
825       __filebuf_type*
826       rdbuf() const
827       { return const_cast<__filebuf_type*>(&_M_filebuf); }
829       /**
830        *  @brief  Wrapper to test for an open file.
831        *  @return  @c rdbuf()->is_open()
832        */
833       bool
834       is_open()
835       { return _M_filebuf.is_open(); }
837       // _GLIBCXX_RESOLVE_LIB_DEFECTS
838       // 365. Lack of const-qualification in clause 27
839       bool
840       is_open() const
841       { return _M_filebuf.is_open(); }
843       /**
844        *  @brief  Opens an external file.
845        *  @param  __s  The name of the file.
846        *  @param  __mode  The open mode flags.
847        *
848        *  Calls @c std::basic_filebuf::open(__s,__mode|out).  If that
849        *  function fails, @c failbit is set in the stream's error state.
850        */
851       void
852       open(const char* __s, ios_base::openmode __mode = ios_base::out)
853       {
854         if (!_M_filebuf.open(__s, __mode | ios_base::out))
855           this->setstate(ios_base::failbit);
856         else
857           // _GLIBCXX_RESOLVE_LIB_DEFECTS
858           // 409. Closing an fstream should clear error state
859           this->clear();
860       }
862 #if __cplusplus >= 201103L
863       /**
864        *  @brief  Opens an external file.
865        *  @param  __s  The name of the file.
866        *  @param  __mode  The open mode flags.
867        *
868        *  Calls @c std::basic_filebuf::open(s,mode|out).  If that
869        *  function fails, @c failbit is set in the stream's error state.
870        */
871       void
872       open(const std::string& __s, ios_base::openmode __mode = ios_base::out)
873       {
874         if (!_M_filebuf.open(__s, __mode | ios_base::out))
875           this->setstate(ios_base::failbit);
876         else
877           // _GLIBCXX_RESOLVE_LIB_DEFECTS
878           // 409. Closing an fstream should clear error state
879           this->clear();
880       }
882 #if __cplusplus >= 201703L
883       /**
884        *  @brief  Opens an external file.
885        *  @param  __s  The name of the file, as a filesystem::path.
886        *  @param  __mode  The open mode flags.
887        *
888        *  Calls @c std::basic_filebuf::open(__s,__mode|out).  If that
889        *  function fails, @c failbit is set in the stream's error state.
890        */
891       template<typename _Path>
892         auto
893         open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
894         -> decltype((void)_M_filebuf.open(__s, __mode))
895         { open(__s.c_str(), __mode); }
896 #endif // C++17
897 #endif // C++11
899       /**
900        *  @brief  Close the file.
901        *
902        *  Calls @c std::basic_filebuf::close().  If that function
903        *  fails, @c failbit is set in the stream's error state.
904        */
905       void
906       close()
907       {
908         if (!_M_filebuf.close())
909           this->setstate(ios_base::failbit);
910       }
911     };
914   // [27.8.1.11] Template class basic_fstream
915   /**
916    *  @brief  Controlling input and output for files.
917    *  @ingroup io
918    *
919    *  @tparam _CharT  Type of character stream.
920    *  @tparam _Traits  Traits for character type, defaults to
921    *                   char_traits<_CharT>.
922    *
923    *  This class supports reading from and writing to named files, using
924    *  the inherited functions from std::basic_iostream.  To control the
925    *  associated sequence, an instance of std::basic_filebuf is used, which
926    *  this page refers to as @c sb.
927    */
928   template<typename _CharT, typename _Traits>
929     class basic_fstream : public basic_iostream<_CharT, _Traits>
930     {
931     public:
932       // Types:
933       typedef _CharT                                    char_type;
934       typedef _Traits                                   traits_type;
935       typedef typename traits_type::int_type            int_type;
936       typedef typename traits_type::pos_type            pos_type;
937       typedef typename traits_type::off_type            off_type;
939       // Non-standard types:
940       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
941       typedef basic_ios<char_type, traits_type>         __ios_type;
942       typedef basic_iostream<char_type, traits_type>    __iostream_type;
944     private:
945       __filebuf_type    _M_filebuf;
947     public:
948       // Constructors/destructor:
949       /**
950        *  @brief  Default constructor.
951        *
952        *  Initializes @c sb using its default constructor, and passes
953        *  @c &sb to the base class initializer.  Does not open any files
954        *  (you haven't given it a filename to open).
955        */
956       basic_fstream()
957       : __iostream_type(), _M_filebuf()
958       { this->init(&_M_filebuf); }
960       /**
961        *  @brief  Create an input/output file stream.
962        *  @param  __s  Null terminated string specifying the filename.
963        *  @param  __mode  Open file in specified mode (see std::ios_base).
964        */
965       explicit
966       basic_fstream(const char* __s,
967                     ios_base::openmode __mode = ios_base::in | ios_base::out)
968       : __iostream_type(0), _M_filebuf()
969       {
970         this->init(&_M_filebuf);
971         this->open(__s, __mode);
972       }
974 #if __cplusplus >= 201103L
975       /**
976        *  @brief  Create an input/output file stream.
977        *  @param  __s  Null terminated string specifying the filename.
978        *  @param  __mode  Open file in specified mode (see std::ios_base).
979        */
980       explicit
981       basic_fstream(const std::string& __s,
982                     ios_base::openmode __mode = ios_base::in | ios_base::out)
983       : __iostream_type(0), _M_filebuf()
984       {
985         this->init(&_M_filebuf);
986         this->open(__s, __mode);
987       }
989 #if __cplusplus >= 201703L
990       /**
991        *  @param  Create an input/output file stream.
992        *  @param  __s  filesystem::path specifying the filename.
993        *  @param  __mode  Open file in specified mode (see std::ios_base).
994        */
995       template<typename _Path, typename = _Require<
996           is_constructible<__filebuf_type, const _Path&, ios_base::openmode>>>
997         basic_fstream(const _Path& __s,
998                       ios_base::openmode __mode = ios_base::in | ios_base::out)
999         : basic_fstream(__s.c_str(), __mode)
1000         { }
1001 #endif // C++17
1003       basic_fstream(const basic_fstream&) = delete;
1005       basic_fstream(basic_fstream&& __rhs)
1006       : __iostream_type(std::move(__rhs)),
1007       _M_filebuf(std::move(__rhs._M_filebuf))
1008       { __iostream_type::set_rdbuf(&_M_filebuf); }
1009 #endif
1011       /**
1012        *  @brief  The destructor does nothing.
1013        *
1014        *  The file is closed by the filebuf object, not the formatting
1015        *  stream.
1016        */
1017       ~basic_fstream()
1018       { }
1020 #if __cplusplus >= 201103L
1021       // 27.8.3.2 Assign and swap:
1023       basic_fstream&
1024       operator=(const basic_fstream&) = delete;
1026       basic_fstream&
1027       operator=(basic_fstream&& __rhs)
1028       {
1029         __iostream_type::operator=(std::move(__rhs));
1030         _M_filebuf = std::move(__rhs._M_filebuf);
1031         return *this;
1032       }
1034       void
1035       swap(basic_fstream& __rhs)
1036       {
1037         __iostream_type::swap(__rhs);
1038         _M_filebuf.swap(__rhs._M_filebuf);
1039       }
1040 #endif
1042       // Members:
1043       /**
1044        *  @brief  Accessing the underlying buffer.
1045        *  @return  The current basic_filebuf buffer.
1046        *
1047        *  This hides both signatures of std::basic_ios::rdbuf().
1048        */
1049       __filebuf_type*
1050       rdbuf() const
1051       { return const_cast<__filebuf_type*>(&_M_filebuf); }
1053       /**
1054        *  @brief  Wrapper to test for an open file.
1055        *  @return  @c rdbuf()->is_open()
1056        */
1057       bool
1058       is_open()
1059       { return _M_filebuf.is_open(); }
1061       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1062       // 365. Lack of const-qualification in clause 27
1063       bool
1064       is_open() const
1065       { return _M_filebuf.is_open(); }
1067       /**
1068        *  @brief  Opens an external file.
1069        *  @param  __s  The name of the file.
1070        *  @param  __mode  The open mode flags.
1071        *
1072        *  Calls @c std::basic_filebuf::open(__s,__mode).  If that
1073        *  function fails, @c failbit is set in the stream's error state.
1074        */
1075       void
1076       open(const char* __s,
1077            ios_base::openmode __mode = ios_base::in | ios_base::out)
1078       {
1079         if (!_M_filebuf.open(__s, __mode))
1080           this->setstate(ios_base::failbit);
1081         else
1082           // _GLIBCXX_RESOLVE_LIB_DEFECTS
1083           // 409. Closing an fstream should clear error state
1084           this->clear();
1085       }
1087 #if __cplusplus >= 201103L
1088       /**
1089        *  @brief  Opens an external file.
1090        *  @param  __s  The name of the file.
1091        *  @param  __mode  The open mode flags.
1092        *
1093        *  Calls @c std::basic_filebuf::open(__s,__mode).  If that
1094        *  function fails, @c failbit is set in the stream's error state.
1095        */
1096       void
1097       open(const std::string& __s,
1098            ios_base::openmode __mode = ios_base::in | ios_base::out)
1099       {
1100         if (!_M_filebuf.open(__s, __mode))
1101           this->setstate(ios_base::failbit);
1102         else
1103           // _GLIBCXX_RESOLVE_LIB_DEFECTS
1104           // 409. Closing an fstream should clear error state
1105           this->clear();
1106       }
1108 #if __cplusplus >= 201703L
1109       /**
1110        *  @brief  Opens an external file.
1111        *  @param  __s  The name of the file, as a filesystem::path.
1112        *  @param  __mode  The open mode flags.
1113        *
1114        *  Calls @c std::basic_filebuf::open(__s,__mode).  If that
1115        *  function fails, @c failbit is set in the stream's error state.
1116        */
1117       template<typename _Path>
1118         auto
1119         open(const _Path& __s,
1120              ios_base::openmode __mode = ios_base::in | ios_base::out)
1121         -> decltype((void)_M_filebuf.open(__s, __mode))
1122         { open(__s.c_str(), __mode); }
1123 #endif // C++17
1124 #endif // C++11
1126       /**
1127        *  @brief  Close the file.
1128        *
1129        *  Calls @c std::basic_filebuf::close().  If that function
1130        *  fails, @c failbit is set in the stream's error state.
1131        */
1132       void
1133       close()
1134       {
1135         if (!_M_filebuf.close())
1136           this->setstate(ios_base::failbit);
1137       }
1138     };
1140 #if __cplusplus >= 201103L
1141   /// Swap specialization for filebufs.
1142   template <class _CharT, class _Traits>
1143     inline void
1144     swap(basic_filebuf<_CharT, _Traits>& __x,
1145          basic_filebuf<_CharT, _Traits>& __y)
1146     { __x.swap(__y); }
1148   /// Swap specialization for ifstreams.
1149   template <class _CharT, class _Traits>
1150     inline void
1151     swap(basic_ifstream<_CharT, _Traits>& __x,
1152          basic_ifstream<_CharT, _Traits>& __y)
1153     { __x.swap(__y); }
1155   /// Swap specialization for ofstreams.
1156   template <class _CharT, class _Traits>
1157     inline void
1158     swap(basic_ofstream<_CharT, _Traits>& __x,
1159          basic_ofstream<_CharT, _Traits>& __y)
1160     { __x.swap(__y); }
1162   /// Swap specialization for fstreams.
1163   template <class _CharT, class _Traits>
1164     inline void
1165     swap(basic_fstream<_CharT, _Traits>& __x,
1166          basic_fstream<_CharT, _Traits>& __y)
1167     { __x.swap(__y); }
1168 #endif
1170 _GLIBCXX_END_NAMESPACE_VERSION
1171 } // namespace
1173 #include <bits/fstream.tcc>
1175 #endif /* _GLIBCXX_FSTREAM */