PageRenderTime 21ms CodeModel.GetById 8ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/streams/filter.h

http://github.com/mozy/mordor
C Header | 99 lines | 73 code | 14 blank | 12 comment | 2 complexity | 17d7235973380d75e1790c70eaac3814 MD5 | raw file
 1#ifndef __MORDOR_FILTER_STREAM_H__
 2#define __MORDOR_FILTER_STREAM_H__
 3// Copyright (c) 2009 - Mozy, Inc.
 4
 5#include "stream.h"
 6
 7namespace Mordor {
 8
 9// When inheriting from FilterStream, use parent()->xxx to call
10// method xxx on the parent stream.
11// FilterStreams *must* implement one of the possible overloads for read()
12// and write(), even if it's just calling the parent version.  Otherwise
13// the adapter functions in the base Stream will just call each other
14// and blow the stack
15class FilterStream : public Stream
16{
17public:
18    typedef boost::shared_ptr<FilterStream> ptr;
19
20public:
21    FilterStream(Stream::ptr parent, bool own = true)
22        : m_parent(parent), m_own(own)
23    {}
24
25    Stream::ptr parent() { return m_parent; }
26    void parent(Stream::ptr parent) { m_parent = parent; }
27    bool ownsParent() { return m_own; }
28    void ownsParent(bool own) { m_own = own; }
29
30    bool supportsHalfClose() { return m_parent->supportsHalfClose(); }
31    bool supportsRead() { return m_parent->supportsRead(); }
32    bool supportsWrite() { return m_parent->supportsWrite(); }
33    bool supportsSeek() { return m_parent->supportsSeek(); }
34    bool supportsTell() { return m_parent->supportsTell(); }
35    bool supportsSize() { return m_parent->supportsSize(); }
36    bool supportsTruncate() { return m_parent->supportsTruncate(); }
37    bool supportsFind() { return m_parent->supportsFind(); }
38    bool supportsUnread() { return m_parent->supportsUnread(); }
39
40    void close(CloseType type = BOTH) { if (m_own) m_parent->close(type); }
41    void cancelRead() { m_parent->cancelRead(); }
42    void cancelWrite() { m_parent->cancelWrite(); }
43    long long seek(long long offset, Anchor anchor = BEGIN)
44    { return m_parent->seek(offset, anchor); }
45    long long size() { return m_parent->size(); }
46    void truncate(long long size) { m_parent->truncate(size); }
47    void flush(bool flushParent = true)
48    { if (flushParent) m_parent->flush(true); }
49    ptrdiff_t find(char delim, size_t sanitySize = ~0,
50        bool throwIfNotFound = true)
51    { return m_parent->find(delim, sanitySize, throwIfNotFound); }
52    ptrdiff_t find(const std::string &str, size_t sanitySize = ~0,
53        bool throwIfNotFound = true)
54    { return m_parent->find(str, sanitySize, throwIfNotFound); }
55    void unread(const Buffer &b, size_t len)
56    { return m_parent->unread(b, len); }
57
58    boost::signals2::connection onRemoteClose(
59        const boost::signals2::slot<void ()> &slot)
60    { return m_parent->onRemoteClose(slot); }
61
62private:
63    Stream::ptr m_parent;
64    bool m_own;
65};
66
67/// @details
68/// A mutating filter stream is one that declares that it changes the data
69/// as it flows through it.  It implicitly turns off and asserts features
70/// that would need to be implemented by the inheritor, instead of defaulting
71/// to the parent streams implementation.
72class MutatingFilterStream : public FilterStream
73{
74protected:
75    MutatingFilterStream(Stream::ptr parent, bool owns = true)
76        : FilterStream(parent, owns)
77    {}
78
79public:
80    bool supportsSeek() { return false; }
81    bool supportsTell() { return supportsSeek(); }
82    bool supportsSize() { return false; }
83    bool supportsTruncate() { return false; }
84    bool supportsFind() { return false; }
85    bool supportsUnread() { return false; }
86
87    long long seek(long long offset, Anchor anchor = BEGIN);
88    long long size();
89    void truncate(long long size);
90    ptrdiff_t find(char delim, size_t sanitySize = ~0,
91        bool throwIfNotFound = true);
92    ptrdiff_t find(const std::string &str, size_t sanitySize = ~0,
93        bool throwIfNotFound = true);
94    void unread(const Buffer &b, size_t len);
95};
96
97}
98
99#endif