ders::buf_writer Class Reference

Provides buffered write functions for classes that implement writable interface. More...

#include <buf_rdwr.hpp>

List of all members.

Public Member Functions

 buf_writer (mem_pool &m, writable &wrbl, int bufsz=4 *1024)
 ~buf_writer ()
int write (const void *b, int n)
int write (const ch_rng &r)
int write (char ch)
bool flush ()
void reset ()
const char * begin () const
const char * end () const


Detailed Description

Provides buffered write functions for classes that implement writable interface.

Definition at line 77 of file buf_rdwr.hpp.


Constructor & Destructor Documentation

ders::buf_writer::buf_writer ( mem_pool m,
writable wrbl,
int  bufsz = 4*1024 
)

Definition at line 138 of file buf_rdwr.cpp.

00138                                                              :
00139   mp(m), wr(wrbl), bsz(bufsz)
00140 {
00141  assert(bsz>0);
00142 
00143  buf=static_cast<unsigned char*>(mp.allocate(bsz));
00144  reset();
00145 }

ders::buf_writer::~buf_writer (  ) 

Definition at line 147 of file buf_rdwr.cpp.

00148 {
00149  flush();
00150  mp.deallocate(buf, bsz);
00151 }


Member Function Documentation

int ders::buf_writer::write ( const void *  b,
int  n 
)

Definition at line 153 of file buf_rdwr.cpp.

00154 {
00155  assert(n>=0);
00156 
00157  if (n==0) return 0;
00158 
00159  int wrn=bsz-it;
00160  if (n<=wrn) {
00161     memcpy(buf+it, b, n);
00162     it+=n;
00163 
00164     return n;
00165  }
00166 
00167  // n>wrn
00168  memcpy(buf+it, b, wrn);
00169  it=bsz;
00170 
00171  if (!flush()) return wrn;
00172 
00173  b=static_cast<const char*>(b)+wrn;
00174  n-=wrn;
00175 
00176  int rc=wr.write(b, n);
00177  return (rc!=-1) ? wrn+rc : wrn;
00178 }

int ders::buf_writer::write ( const ch_rng r  )  [inline]

Definition at line 94 of file buf_rdwr.hpp.

00094 { return write(r.beg, r.size()); }

int ders::buf_writer::write ( char  ch  )  [inline]

Definition at line 95 of file buf_rdwr.hpp.

00095 { return (it<bsz) ? buf[it++]=ch : write_impl(ch); }

bool ders::buf_writer::flush (  ) 

Definition at line 180 of file buf_rdwr.cpp.

00181 {
00182  if (it==0) return true;
00183 
00184  int rc=wr.write(buf, it);
00185  if (rc==-1) return false;
00186 
00187  memmove(buf, buf+rc, it-rc);
00188  it-=rc;
00189 
00190  return it==0;
00191 }

void ders::buf_writer::reset (  )  [inline]

Definition at line 98 of file buf_rdwr.hpp.

00098 { it=0; }

const char* ders::buf_writer::begin (  )  const [inline]

Definition at line 100 of file buf_rdwr.hpp.

00100 { return reinterpret_cast<char*>(buf); }

const char* ders::buf_writer::end (  )  const [inline]

Definition at line 101 of file buf_rdwr.hpp.

00101 { return reinterpret_cast<char*>(buf)+it; }


The documentation for this class was generated from the following files:

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5