libstdc++
strstream
Go to the documentation of this file.
00001 // Backward-compat support -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  * Copyright (c) 1998
00027  * Silicon Graphics Computer Systems, Inc.
00028  *
00029  * Permission to use, copy, modify, distribute and sell this software
00030  * and its documentation for any purpose is hereby granted without fee,
00031  * provided that the above copyright notice appear in all copies and
00032  * that both that copyright notice and this permission notice appear
00033  * in supporting documentation.  Silicon Graphics makes no
00034  * representations about the suitability of this software for any
00035  * purpose.  It is provided "as is" without express or implied warranty.
00036  */
00037 
00038 // WARNING: The classes defined in this header are DEPRECATED.  This
00039 // header is defined in section D.7.1 of the C++ standard, and it
00040 // MAY BE REMOVED in a future standard revision.  One should use the
00041 // header <sstream> instead.
00042 
00043 /** @file strstream
00044  *  This is a Standard C++ Library header.
00045  */
00046 
00047 #ifndef _BACKWARD_STRSTREAM
00048 #define _BACKWARD_STRSTREAM
00049 
00050 #include <backward/backward_warning.h>
00051 #include <iosfwd>
00052 #include <ios>
00053 #include <istream>
00054 #include <ostream>
00055 
00056 namespace std _GLIBCXX_VISIBILITY(default)
00057 {
00058 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00059 
00060   // Class strstreambuf, a streambuf class that manages an array of char.
00061   // Note that this class is not a template.
00062   class strstreambuf : public basic_streambuf<char, char_traits<char> >
00063   {
00064   public:
00065     // Types.
00066     typedef char_traits<char>              _Traits;
00067     typedef basic_streambuf<char, _Traits> _Base;
00068 
00069   public:
00070     // Constructor, destructor
00071 #if __cplusplus >= 201103L
00072     strstreambuf() : strstreambuf(0) { }
00073     explicit strstreambuf(streamsize __initial_capacity);
00074 #else
00075     explicit strstreambuf(streamsize __initial_capacity = 0);
00076 #endif
00077     strstreambuf(void* (*__alloc)(size_t), void (*__free)(void*));
00078 
00079     strstreambuf(char* __get, streamsize __n, char* __put = 0) throw ();
00080     strstreambuf(signed char* __get, streamsize __n, signed char* __put = 0) throw ();
00081     strstreambuf(unsigned char* __get, streamsize __n, unsigned char* __put=0) throw ();
00082 
00083     strstreambuf(const char* __get, streamsize __n) throw ();
00084     strstreambuf(const signed char* __get, streamsize __n) throw ();
00085     strstreambuf(const unsigned char* __get, streamsize __n) throw ();
00086 
00087     virtual ~strstreambuf();
00088 
00089 #if __cplusplus >= 201103L
00090     strstreambuf(strstreambuf&& __rhs) noexcept
00091     : _Base(__rhs), _M_alloc_fun(__rhs._M_alloc_fun),
00092       _M_free_fun(__rhs._M_free_fun), _M_dynamic(__rhs._M_dynamic),
00093       _M_frozen(__rhs._M_frozen), _M_constant(__rhs._M_constant)
00094     {
00095       __rhs.setg(nullptr, nullptr, nullptr);
00096       __rhs.setp(nullptr, nullptr);
00097     }
00098 
00099     strstreambuf&
00100     operator=(strstreambuf&& __rhs) noexcept
00101     {
00102       if (_M_dynamic && !_M_frozen)
00103         _M_free(eback());
00104       _Base::operator=(static_cast<const _Base&>(__rhs));
00105       _M_alloc_fun = __rhs._M_alloc_fun;
00106       _M_free_fun = __rhs._M_free_fun;
00107       _M_dynamic = __rhs._M_dynamic;
00108       _M_frozen = __rhs._M_frozen;
00109       _M_constant = __rhs._M_constant;
00110       __rhs.setg(nullptr, nullptr, nullptr);
00111       __rhs.setp(nullptr, nullptr);
00112       return *this;
00113     }
00114 #endif
00115 
00116   public:
00117     void freeze(bool = true) throw ();
00118     char* str() throw ();
00119     _GLIBCXX_PURE int pcount() const throw ();
00120 
00121   protected:
00122     virtual int_type overflow(int_type __c  = _Traits::eof());
00123     virtual int_type pbackfail(int_type __c = _Traits::eof());
00124     virtual int_type underflow();
00125     virtual _Base* setbuf(char* __buf, streamsize __n);
00126     virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
00127                              ios_base::openmode __mode
00128                              = ios_base::in | ios_base::out);
00129     virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode
00130                              = ios_base::in | ios_base::out);
00131 
00132   private:
00133 #if __cplusplus < 201103L
00134     strstreambuf&
00135     operator=(const strstreambuf&);
00136 
00137     strstreambuf(const strstreambuf&);
00138 #endif
00139 
00140     // Dynamic allocation, possibly using _M_alloc_fun and _M_free_fun.
00141     char* _M_alloc(size_t);
00142     void  _M_free(char*);
00143 
00144     // Helper function used in constructors.
00145     void _M_setup(char* __get, char* __put, streamsize __n) throw ();
00146 
00147     // Data members.
00148     void* (*_M_alloc_fun)(size_t);
00149     void  (*_M_free_fun)(void*);
00150 
00151     bool _M_dynamic  : 1;
00152     bool _M_frozen   : 1;
00153     bool _M_constant : 1;
00154   };
00155 
00156   // Class istrstream, an istream that manages a strstreambuf.
00157   class istrstream : public basic_istream<char>
00158   {
00159   public:
00160     explicit istrstream(char*);
00161     explicit istrstream(const char*);
00162     istrstream(char* , streamsize);
00163     istrstream(const char*, streamsize);
00164     virtual ~istrstream();
00165 
00166 #if __cplusplus >= 201103L
00167     istrstream(istrstream&& __rhs)
00168     : istream(std::move(__rhs)), _M_buf(std::move(__rhs._M_buf))
00169     { set_rdbuf(&_M_buf); }
00170 
00171     istrstream& operator=(istrstream&&) = default;
00172 #endif
00173 
00174     _GLIBCXX_CONST strstreambuf* rdbuf() const throw ();
00175     char* str() throw ();
00176 
00177   private:
00178     strstreambuf _M_buf;
00179   };
00180 
00181   // Class ostrstream
00182   class ostrstream : public basic_ostream<char>
00183   {
00184   public:
00185     ostrstream();
00186     ostrstream(char*, int, ios_base::openmode = ios_base::out);
00187     virtual ~ostrstream();
00188 
00189 #if __cplusplus >= 201103L
00190     ostrstream(ostrstream&& __rhs)
00191     : ostream(std::move(__rhs)), _M_buf(std::move(__rhs._M_buf))
00192     { set_rdbuf(&_M_buf); }
00193 
00194     ostrstream& operator=(ostrstream&&) = default;
00195 #endif
00196 
00197     _GLIBCXX_CONST strstreambuf* rdbuf() const throw ();
00198     void freeze(bool = true) throw();
00199     char* str() throw ();
00200     _GLIBCXX_PURE int pcount() const throw ();
00201 
00202   private:
00203     strstreambuf _M_buf;
00204   };
00205 
00206   // Class strstream
00207   class strstream : public basic_iostream<char>
00208   {
00209   public:
00210     typedef char                        char_type;
00211     typedef char_traits<char>::int_type int_type;
00212     typedef char_traits<char>::pos_type pos_type;
00213     typedef char_traits<char>::off_type off_type;
00214 
00215     strstream();
00216     strstream(char*, int, ios_base::openmode = ios_base::in | ios_base::out);
00217     virtual ~strstream();
00218 
00219 #if __cplusplus >= 201103L
00220     strstream(strstream&& __rhs)
00221     : iostream(std::move(__rhs)), _M_buf(std::move(__rhs._M_buf))
00222     { set_rdbuf(&_M_buf); }
00223 
00224     strstream& operator=(strstream&&) = default;
00225 #endif
00226 
00227     _GLIBCXX_CONST strstreambuf* rdbuf() const throw ();
00228     void freeze(bool = true) throw ();
00229     _GLIBCXX_PURE int pcount() const throw ();
00230     char* str() throw ();
00231 
00232   private:
00233     strstreambuf _M_buf;
00234   };
00235 
00236 _GLIBCXX_END_NAMESPACE_VERSION
00237 } // namespace
00238 
00239 #endif