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