/mordor/streams/buffer.h

http://github.com/mozy/mordor · C Header · 136 lines · 108 code · 26 blank · 2 comment · 0 complexity · cd12fbf938b735756dadde36246fef5f MD5 · raw file

  1. #ifndef __MORDOR_BUFFER_H__
  2. #define __MORDOR_BUFFER_H__
  3. #include <list>
  4. #include <vector>
  5. #include <boost/shared_array.hpp>
  6. #include <boost/function.hpp>
  7. #include "mordor/socket.h"
  8. namespace Mordor {
  9. struct Buffer
  10. {
  11. private:
  12. struct SegmentData
  13. {
  14. friend struct Buffer;
  15. public:
  16. SegmentData();
  17. SegmentData(size_t length);
  18. SegmentData(void *buffer, size_t length);
  19. SegmentData slice(size_t start, size_t length = ~0);
  20. const SegmentData slice(size_t start, size_t length = ~0) const;
  21. void extend(size_t len);
  22. public:
  23. void *start() { return m_start; }
  24. const void *start() const { return m_start; }
  25. size_t length() const { return m_length; }
  26. private:
  27. void start(void *p) { m_start = p; }
  28. void length(size_t l) { m_length = l; }
  29. void *m_start;
  30. size_t m_length;
  31. private:
  32. boost::shared_array<unsigned char> m_array;
  33. };
  34. struct Segment
  35. {
  36. friend struct Buffer;
  37. public:
  38. Segment(size_t len);
  39. Segment(SegmentData);
  40. Segment(void *buffer, size_t length);
  41. size_t readAvailable() const;
  42. size_t writeAvailable() const;
  43. size_t length() const;
  44. void produce(size_t length);
  45. void consume(size_t length);
  46. void truncate(size_t length);
  47. void extend(size_t length);
  48. const SegmentData readBuffer() const;
  49. const SegmentData writeBuffer() const;
  50. SegmentData writeBuffer();
  51. private:
  52. size_t m_writeIndex;
  53. SegmentData m_data;
  54. void invariant() const;
  55. };
  56. public:
  57. Buffer();
  58. Buffer(const Buffer &copy);
  59. Buffer(const char *string);
  60. Buffer(const std::string &string);
  61. Buffer(const void *data, size_t length);
  62. Buffer &operator =(const Buffer &copy);
  63. size_t readAvailable() const;
  64. size_t writeAvailable() const;
  65. // Primarily for unit tests
  66. size_t segments() const;
  67. void adopt(void *buffer, size_t length);
  68. void reserve(size_t length);
  69. void compact();
  70. void clear(bool clearWriteAvailableAsWell = true);
  71. void produce(size_t length);
  72. void consume(size_t length);
  73. void truncate(size_t length);
  74. const std::vector<iovec> readBuffers(size_t length = ~0) const;
  75. const iovec readBuffer(size_t length, bool reallocate) const;
  76. std::vector<iovec> writeBuffers(size_t length = ~0);
  77. iovec writeBuffer(size_t length, bool reallocate);
  78. void copyIn(const Buffer& buf, size_t length = ~0, size_t pos = 0);
  79. void copyIn(const char* string);
  80. void copyIn(const std::string &string);
  81. void copyIn(const void* data, size_t length);
  82. void copyOut(Buffer &buffer, size_t length, size_t pos = 0) const
  83. { buffer.copyIn(*this, length, pos); }
  84. void copyOut(void* buffer, size_t length, size_t pos = 0) const;
  85. ptrdiff_t find(char delimiter, size_t length = ~0) const;
  86. ptrdiff_t find(const std::string &string, size_t length = ~0) const;
  87. /// convert current readAvailable data to std::string
  88. std::string toString() const;
  89. std::string getDelimited(char delimiter, bool eofIsDelimiter = true,
  90. bool includeDelimiter = true);
  91. std::string getDelimited(const std::string &delimiter,
  92. bool eofIsDelimiter = true, bool includeDelimiter = true);
  93. void visit(boost::function<void (const void *, size_t)> dg, size_t length = ~0) const;
  94. bool operator== (const Buffer &rhs) const;
  95. bool operator!= (const Buffer &rhs) const;
  96. bool operator== (const std::string &str) const;
  97. bool operator!= (const std::string &str) const;
  98. bool operator== (const char *str) const;
  99. bool operator!= (const char *str) const;
  100. private:
  101. std::list<Segment> m_segments;
  102. size_t m_readAvailable;
  103. size_t m_writeAvailable;
  104. std::list<Segment>::iterator m_writeIt;
  105. int opCmp(const Buffer &rhs) const;
  106. int opCmp(const char *string, size_t length) const;
  107. void invariant() const;
  108. };
  109. }
  110. #endif