PageRenderTime 39ms CodeModel.GetById 25ms app.highlight 3ms RepoModel.GetById 10ms app.codeStats 0ms

/mordor/streams/test.h

http://github.com/mozy/mordor
C Header | 58 lines | 44 code | 12 blank | 2 comment | 0 complexity | 82db45307fbaea5a82649aea0ac8d1e4 MD5 | raw file
 1#ifndef __MORDOR_TEST_STREAM_H__
 2#define __MORDOR_TEST_STREAM_H__
 3// Copyright (c) 2009 - Mozy, Inc.
 4
 5#include <boost/function.hpp>
 6
 7#include "filter.h"
 8
 9namespace Mordor {
10
11// This stream is for use in unit tests to force
12class TestStream : public FilterStream
13{
14public:
15    typedef boost::shared_ptr<TestStream> ptr;
16
17public:
18    TestStream(Stream::ptr parent)
19        : FilterStream(parent, true),
20          m_maxReadSize(~0),
21          m_maxWriteSize(~0),
22          m_onReadBytes(0),
23          m_onWriteBytes(0)
24    {}
25
26    size_t maxReadSize() const { return m_maxReadSize; }
27    void maxReadSize(size_t max) { m_maxReadSize = max; }
28
29    size_t maxWriteSize() const { return m_maxWriteSize; }
30    void maxWriteSize(size_t max) { m_maxWriteSize = max; }
31
32    void onClose(boost::function<void (CloseType)> dg)
33    { m_onClose = dg; }
34    void onRead(boost::function<void ()> dg, long long bytes = 0)
35    { m_onRead = dg; m_onReadBytes = bytes; }
36    void onWrite(boost::function<void ()> dg, long long bytes = 0)
37    { m_onWrite = dg; m_onWriteBytes = bytes; }
38    void onFlush(boost::function<void (bool)> dg)
39    { m_onFlush = dg; }
40
41    void close(CloseType type = BOTH);
42    using FilterStream::read;
43    size_t read(Buffer &b, size_t len);
44    using FilterStream::write;
45    size_t write(const Buffer &b, size_t len);
46    void flush(bool flushParent = true);
47
48private:
49    size_t m_maxReadSize, m_maxWriteSize;
50    boost::function<void (CloseType)> m_onClose;
51    boost::function<void ()> m_onRead, m_onWrite;
52    boost::function<void (bool)> m_onFlush;
53    long long m_onReadBytes, m_onWriteBytes;
54};
55
56}
57
58#endif