~zorba-coders/zorba/bug1123835

« back to all changes in this revision

Viewing changes to include/zorba/util/base64_stream.h

  • Committer: sorin.marian.nasoi
  • Date: 2013-08-13 09:07:33 UTC
  • mfrom: (11278.1.318 lp_zorba)
  • Revision ID: spungi@gmail.com-20130813090733-vjl1znp7np45y12z
- merge lp:zorba trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
///////////////////////////////////////////////////////////////////////////////
30
30
 
31
31
/**
32
 
 * A %base64::streambuf is-a std::streambuf for encoding to and decoding from
33
 
 * Base64 on-the-fly.
 
32
 * A %base64::streambuf is-a std::streambuf for decoding from and encoding to 
 
33
 * Base64 on-the-fly while reading or writing, respectively.
34
34
 *
35
35
 * To use it, replace a stream's streambuf:
36
36
 * \code
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
151
151
 * nothing.
 
152
 * @return \c true only if a base64::streambuf was attached.
152
153
 */
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 );
157
158
  if ( !pword ) {
160
161
    ios.rdbuf( buf );
161
162
    pword = buf;
162
163
    ios.register_callback( internal::stream_callback, index );
 
164
    return true;
163
165
  }
 
166
  return false;
164
167
}
165
168
 
166
169
/**
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
172
175
 * does nothing.
 
176
 * @return \c true only if a base64::streambuf was detached.
173
177
 */
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 );
 
185
    return true;
181
186
  }
 
187
  return false;
182
188
}
183
189
 
184
190
/**
210
216
class auto_attach {
211
217
public:
212
218
  /**
 
219
   * Default constructor; does nothing.
 
220
   */
 
221
  auto_attach() : stream_( nullptr ) {
 
222
  }
 
223
 
 
224
  /**
213
225
   * Constructs an %auto_attach object calling attach() on the given stream.
214
226
   *
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
217
229
   * does nothing.
218
230
   */
219
 
  auto_attach( StreamType &stream ) : stream_( stream ) {
220
 
    attach( stream );
 
231
  auto_attach( StreamType &stream ) : stream_( &stream ) {
 
232
    base64::attach( stream );
 
233
  }
 
234
 
 
235
  /**
 
236
   * Copy constructor that takes ownership of the stream.
 
237
   *
 
238
   * @param from The %auto_attach to take ownership from.
 
239
   */
 
240
  auto_attach( auto_attach &from ) : stream_( from.stream_ ) {
 
241
    from.stream_ = nullptr;
221
242
  }
222
243
 
223
244
  /**
224
245
   * Destroys this %auto_attach object calling detach() on the previously
225
 
   * attached stream.
 
246
   * attached stream, if any.
226
247
   */
227
248
  ~auto_attach() {
228
 
    detach( stream_ );
 
249
    detach();
 
250
  }
 
251
 
 
252
  /**
 
253
   * Assignment operator that takes ownership of the stream.
 
254
   *
 
255
   * @param from The %auto_attach to take ownership from.
 
256
   * @return \c *this.
 
257
   */
 
258
  auto_attach& operator=( auto_attach &from ) {
 
259
    if ( &from != this ) {
 
260
      stream_ = from.stream_;
 
261
      from.stream_ = nullptr;
 
262
    }
 
263
    return *this;
 
264
  }
 
265
 
 
266
  /**
 
267
   * Calls base64::attach() on the given stream.
 
268
   *
 
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
 
271
   * does nothing.
 
272
   * @param charset The name of the character encoding to convert from/to.
 
273
   * @return \c true only if a base64::streambuf was attached.
 
274
   */
 
275
  bool attach( StreamType &stream, char const *charset ) {
 
276
    if ( base64::attach( stream, charset ) ) {
 
277
      stream_ = &stream;
 
278
      return true;
 
279
    }
 
280
    return false;
 
281
  }
 
282
 
 
283
  /**
 
284
   * Calls base64::detach().
 
285
   */
 
286
  void detach() {
 
287
    if ( stream_ ) {
 
288
      base64::detach( *stream_ );
 
289
      stream_ = nullptr;
 
290
    }
229
291
  }
230
292
 
231
293
private:
232
 
  StreamType &stream_;
 
294
  StreamType *stream_;
233
295
};
234
296
 
235
297
///////////////////////////////////////////////////////////////////////////////