24 #ifndef GRAPHLAB_CIRCULAR_CHAR_BUFFER_HPP
25 #define GRAPHLAB_CIRCULAR_CHAR_BUFFER_HPP
28 #include <graphlab/logger/assertions.hpp>
29 #include <boost/iostreams/stream.hpp>
30 #include <boost/iostreams/categories.hpp>
38 class circular_char_buffer {
42 circular_char_buffer(std::streamsize initialsize = 1024);
44 circular_char_buffer(
const circular_char_buffer &src);
46 circular_char_buffer& operator=(
const circular_char_buffer& src);
49 ~circular_char_buffer();
52 std::streamsize write(
const char* c, std::streamsize clen);
57 std::streamsize peek(
char* c, std::streamsize clen)
const;
62 std::streamsize read(
char* c, std::streamsize clen);
68 std::streamsize peek(std::string &s, std::streamsize clen)
const;
73 std::streamsize read(std::string &s, std::streamsize clen);
77 std::streamsize skip(std::streamsize clen);
82 void reserve(std::streamsize s);
95 bool align_requires_alloc();
107 std::streamsize introspective_read(
char* &s);
119 std::streamsize introspective_read(
char* &s, std::streamsize clen);
127 std::streamsize introspective_write(
char* &s);
129 void advance_write(std::streamsize bytes);
131 inline void consistency_check()
const {
143 inline std::streamsize size()
const {
149 inline std::streamsize reserved_size()
const {
154 inline bool buffer_full()
const {
155 return len == bufsize;
163 std::streamsize head;
169 std::streamsize tail;
170 std::streamsize bufsize;
179 size_t maxlen =
size_t(-1)):buf(buf), maxlen(maxlen) { }
181 circular_char_buffer &buf;
183 typedef char char_type;
184 struct category :
public boost::iostreams::source_tag { };
191 inline std::streamsize read(
char* s, std::streamsize n) {
192 if ((
size_t)(n) > maxlen) n = (std::streamsize)maxlen;
194 if (n == 0)
return -1;
195 else return buf.read(s, n);
205 circular_char_buffer &buf;
206 typedef char char_type;
207 struct category:
public boost::iostreams::sink_tag,
208 public boost::iostreams::multichar_tag { };
214 inline std::streamsize write(
const char* s, std::streamsize n) {
215 return buf.write(s, n);
219 struct circular_char_buffer_device {
220 circular_char_buffer_device(circular_char_buffer &buf):buf(buf) { }
222 circular_char_buffer &buf;
223 typedef char char_type;
224 struct category :
public boost::iostreams::bidirectional_device_tag,
225 public boost::iostreams::optimally_buffered_tag{ };
229 inline std::streamsize optimal_buffer_size()
const {
return 0; }
231 inline std::streamsize write(
const char* s, std::streamsize n) {
232 return buf.write(s, n);
235 inline std::streamsize read(
char* s, std::streamsize n) {
236 return buf.read(s, n);