/mordor/streams/singleplex.cpp

http://github.com/mozy/mordor · C++ · 98 lines · 82 code · 15 blank · 1 comment · 25 complexity · 7c25e4b959d1c094f11db07ad729a6a0 MD5 · raw file

  1. // Copyright (c) 2010 - Mozy, Inc.
  2. #include "singleplex.h"
  3. #include "mordor/assert.h"
  4. namespace Mordor {
  5. SingleplexStream::SingleplexStream(Stream::ptr parent, Type type, bool own)
  6. : FilterStream(parent, own),
  7. m_type(type)
  8. {
  9. MORDOR_ASSERT(type == READ || type == WRITE);
  10. if (type == READ) MORDOR_ASSERT(parent->supportsRead());
  11. if (type == WRITE) MORDOR_ASSERT(parent->supportsWrite());
  12. }
  13. void
  14. SingleplexStream::close(CloseType type)
  15. {
  16. if (ownsParent()) {
  17. if (m_type == READ && (type & Stream::READ)) {
  18. parent()->close(parent()->supportsHalfClose() ?
  19. Stream::READ : BOTH);
  20. } else if (m_type == WRITE && (type & Stream::WRITE)) {
  21. parent()->close(parent()->supportsHalfClose() ?
  22. Stream::WRITE : BOTH);
  23. }
  24. }
  25. }
  26. size_t
  27. SingleplexStream::read(Buffer &buffer, size_t length)
  28. {
  29. MORDOR_ASSERT(m_type == READ);
  30. return parent()->read(buffer, length);
  31. }
  32. size_t
  33. SingleplexStream::read(void * buffer, size_t length)
  34. {
  35. MORDOR_ASSERT(m_type == READ);
  36. return parent()->read(buffer, length);
  37. }
  38. size_t
  39. SingleplexStream::write(const Buffer &buffer, size_t length)
  40. {
  41. MORDOR_ASSERT(m_type == WRITE);
  42. return parent()->write(buffer, length);
  43. }
  44. size_t
  45. SingleplexStream::write(const void *buffer, size_t length)
  46. {
  47. MORDOR_ASSERT(m_type == WRITE);
  48. return parent()->write(buffer, length);
  49. }
  50. void
  51. SingleplexStream::truncate(long long size)
  52. {
  53. MORDOR_ASSERT(m_type == WRITE);
  54. return parent()->truncate(size);
  55. }
  56. void
  57. SingleplexStream::flush(bool flushParent)
  58. {
  59. if (m_type == READ)
  60. return;
  61. return parent()->flush(flushParent);
  62. }
  63. ptrdiff_t
  64. SingleplexStream::find(char delimiter, size_t sanitySize,
  65. bool throwIfNotFound)
  66. {
  67. MORDOR_ASSERT(m_type == READ);
  68. return parent()->find(delimiter, sanitySize, throwIfNotFound);
  69. }
  70. ptrdiff_t
  71. SingleplexStream::find(const std::string &delimiter, size_t sanitySize,
  72. bool throwIfNotFound)
  73. {
  74. MORDOR_ASSERT(m_type == READ);
  75. return parent()->find(delimiter, sanitySize, throwIfNotFound);
  76. }
  77. void
  78. SingleplexStream::unread(const Buffer &buffer, size_t length)
  79. {
  80. MORDOR_ASSERT(m_type == READ);
  81. return parent()->unread(buffer, length);
  82. }
  83. }