scclib
Stable Cloud Computing C++ Library
Files | Classes
input/output stream utilities

Allows wrapping of generic streaming objects into std::iostream. More...

Collaboration diagram for input/output stream utilities:

Files

file  iobase.h
 Input/output stream base reader/writer interface classes.
 
file  iopipeline.h
 Input/output streaming pipeline.
 
file  iostream.h
 Base input/output stream classes.
 
file  rwcounter.h
 Read/write counter.
 
file  rwloopbuf.h
 Loopback read/write buffer.
 
file  rwtimer.h
 Read/write timer.
 
file  iostream.cc
 input/output stream utilities implementation
 
file  iohelper.cc
 Tests for input/output stream utilities.
 
file  iostream.cc
 Tests for input/output stream utilities.
 

Classes

struct  scc::util::Reader
 Interface class for objects which can be read. More...
 
struct  scc::util::PipelineReader
 Pipeline reader to carry out processing in a pipeline (chain of readers). More...
 
struct  scc::util::Writer
 Interface class for objects which can be written. More...
 
struct  scc::util::PipelineWriter
 Pipeline writer to carry out processing in a pipeline (chain of writers). More...
 
struct  scc::util::FlowThroughPipelineReader
 Pipeline reader which flows through all data. More...
 
struct  scc::util::FlowThroughPipelineWriter
 Pipeline writer which flows through all data. More...
 
struct  scc::util::InChain
 Chain of readers base class. More...
 
struct  scc::util::OutChain
 Chain of writers base class. More...
 
struct  scc::util::InPipeline
 Input stream with pipeline of readers. More...
 
struct  scc::util::OutPipeline
 Output stream pipeline of writers. More...
 
struct  scc::util::IoPipeline
 Input/output stream with pipeline of readers and writers. More...
 
class  scc::util::InStream
 Input stream wrapper for reader. More...
 
class  scc::util::OutStream
 Output stream wrapper for writer. More...
 
class  scc::util::IoStream
 Input/output stream wrapper for reader/writer. More...
 
class  scc::util::ReadCounter
 Adds byte count to a read stream. More...
 
class  scc::util::WriteCounter
 Adds byte count to a write stream. More...
 
class  scc::util::RwCounter
 Adds byte count to a read/write stream. More...
 
class  scc::util::RwLoopBuffer
 Loopback read/write stream buffer. More...
 
class  scc::util::ReadTimer
 Adds timer to a read stream. More...
 
class  scc::util::WriteTimer
 Adds timer to a write stream. More...
 
class  scc::util::RwTimer
 Adds byte count to a read/write stream. More...
 

Detailed Description

Allows wrapping of generic streaming objects into std::iostream.

Can create pipeline objects that add behavior to the stream.

Example from scclib/util/unittest/iostream.cc of basic stream:

Example from scclib/util/unittest/iohelper.cc showing chaining of reader/writers:

shared_ptr<RwLoopBuffer> rbuf(new RwLoopBuffer);
shared_ptr<ReadCounter> rc(new ReadCounter(rbuf));
shared_ptr<RwLoopBuffer> wbuf(new RwLoopBuffer);
shared_ptr<WriteCounter> wc(new WriteCounter(wbuf));
InStream in(rc);
OutStream out(wc);
ASSERT_EQ(rc.use_count(), 2);
ASSERT_EQ(wc.use_count(), 2);
ASSERT_EQ(rbuf.use_count(), 2);
ASSERT_EQ(wbuf.use_count(), 2);
string test("this is a test!");
for (char ch : test) out.put(ch); // write to wbuf
out.flush();
cout << "wc count=" << wc->write_count() << endl;
cout << "wbuf size=" << wbuf->size() << " idx=" << wbuf->idx() << " str=" << wbuf->str() << endl;
ASSERT_EQ(test, wbuf->str());
ASSERT_EQ(wc->write_count(), test.size());
rbuf->set(test);
string got;
for (char ch; in.get(ch);) got.push_back(ch); // read from rbuf
cout << "rc count=" << rc->read_count() << endl;
cout << "rbuf size=" << rbuf->size() << " idx=" << rbuf->idx() << " str=" << rbuf->str() << endl;
ASSERT_EQ(test, got);
ASSERT_EQ(rc->read_count(), test.size());
shared_ptr<RwLoopBuffer> wbuf2(new RwLoopBuffer);
wc->write_reset(wbuf2); // reset the write counter to send to a new buffer
ASSERT_EQ(wc->write_count(), 0);
ASSERT_EQ(wbuf2.use_count(), 2);
wbuf.reset(); // safe to get rid of this now
ASSERT_EQ(wbuf.use_count(), 0);
for (char ch : test) out.put(ch); // write to rbuf2
out.flush();
cout << "wc count=" << wc->write_count() << endl;
cout << "wbuf2 size=" << wbuf2->size() << " idx=" << wbuf2->idx() << " str=" << wbuf2->str() << endl;
ASSERT_EQ(test, wbuf2->str());
ASSERT_EQ(wc->write_count(), test.size());

Example with counter and timer:

using std::chrono::milliseconds;
using std::chrono::duration_cast;
struct Slow : public Reader, public Writer
{
Slow() {}
virtual ~Slow() {}
size_t read(void*, size_t l)
{
std::this_thread::sleep_for(milliseconds(50));
return l;
}
size_t write(const void*, size_t l)
{
std::this_thread::sleep_for(milliseconds(100));
return l;
}
};
Slow base;
RwCounter c(base, base);
RwTimer t(c, c);
t.read(0, 50);
t.write(0, 100);
cout << "read timer=" << t.read_dur().count() << endl;
cout << "write timer=" << t.write_dur().count() << endl;
cout << "read count=" << c.read_count() << endl;
cout << "write count=" << c.write_count() << endl;
ASSERT_EQ(duration_cast<milliseconds>(t.read_dur()).count(), 50);
ASSERT_EQ(duration_cast<milliseconds>(t.write_dur()).count(), 100);
ASSERT_EQ(c.read_count(), 50);
ASSERT_EQ(c.write_count(), 100);