29
29
///////////////////////////////////////////////////////////////////////////////
32
* A %base64::streambuf is-a std::streambuf for encoding to and decoding from
32
* A %base64::streambuf is-a std::streambuf for decoding from and encoding to
33
* Base64 on-the-fly while reading or writing, respectively.
35
35
* To use it, replace a stream's streambuf:
149
149
* @param ios The stream to attach the base64::streambuf to. If the stream
150
150
* already has a base64::streambuf attached to it, this function does
152
* @return \c true only if a base64::streambuf was attached.
153
154
template<typename charT,class Traits> inline
154
void attach( std::basic_ios<charT,Traits> &ios ) {
155
bool attach( std::basic_ios<charT,Traits> &ios ) {
155
156
int const index = internal::base64::get_streambuf_index();
156
157
void *&pword = ios.pword( index );
170
173
* @param ios The stream to detach the base64::streambuf from. If the
171
174
* stream doesn't have a base64::streambuf attached to it, this function
176
* @return \c true only if a base64::streambuf was detached.
174
178
template<typename charT,class Traits> inline
175
void detach( std::basic_ios<charT,Traits> &ios ) {
179
bool detach( std::basic_ios<charT,Traits> &ios ) {
176
180
int const index = internal::base64::get_streambuf_index();
177
181
if ( streambuf *const buf = static_cast<streambuf*>( ios.pword( index ) ) ) {
178
182
ios.pword( index ) = nullptr;
179
183
ios.rdbuf( buf->orig_streambuf() );
180
184
internal::dealloc_streambuf( buf );
210
216
class auto_attach {
219
* Default constructor; does nothing.
221
auto_attach() : stream_( nullptr ) {
213
225
* Constructs an %auto_attach object calling attach() on the given stream.
215
227
* @param stream The stream to attach the base64::streambuf to. If the
216
228
* stream already has a base64::streambuf attached to it, this contructor
219
auto_attach( StreamType &stream ) : stream_( stream ) {
231
auto_attach( StreamType &stream ) : stream_( &stream ) {
232
base64::attach( stream );
236
* Copy constructor that takes ownership of the stream.
238
* @param from The %auto_attach to take ownership from.
240
auto_attach( auto_attach &from ) : stream_( from.stream_ ) {
241
from.stream_ = nullptr;
224
245
* Destroys this %auto_attach object calling detach() on the previously
246
* attached stream, if any.
253
* Assignment operator that takes ownership of the stream.
255
* @param from The %auto_attach to take ownership from.
258
auto_attach& operator=( auto_attach &from ) {
259
if ( &from != this ) {
260
stream_ = from.stream_;
261
from.stream_ = nullptr;
267
* Calls base64::attach() on the given stream.
269
* @param stream The stream to attach the base64::streambuf to. If the
270
* stream already has a base64::streambuf attached to it, this contructor
272
* @param charset The name of the character encoding to convert from/to.
273
* @return \c true only if a base64::streambuf was attached.
275
bool attach( StreamType &stream, char const *charset ) {
276
if ( base64::attach( stream, charset ) ) {
284
* Calls base64::detach().
288
base64::detach( *stream_ );
235
297
///////////////////////////////////////////////////////////////////////////////