PageRenderTime 93ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 37ms app.codeStats 1ms

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