MongoDB  2.7.0
bufreader.h
1 // @file bufreader.h parse a memory region into usable pieces
2 
31 #pragma once
32 
33 #include <boost/noncopyable.hpp>
34 
35 #include "mongo/bson/util/builder.h"
36 #include "mongo/util/assert_util.h"
37 
38 namespace mongo {
39 
44  class BufReader : boost::noncopyable {
45  public:
46  class eof : public std::exception {
47  public:
48  eof() { }
49  virtual const char * what() const throw() { return "BufReader eof"; }
50  };
51 
52  BufReader(const void *p, unsigned len) : _start(p), _pos(p), _end(((char *)_pos)+len) { }
53 
54  bool atEof() const { return _pos == _end; }
55 
57  template <typename T>
58  void read(T &t) {
59  T* cur = (T*) _pos;
60  T *next = cur + 1;
61  if( _end < next ) throw eof();
62  t = *cur;
63  _pos = next;
64  }
65 
67  template <typename T>
68  T read() {
69  T out;
70  read(out);
71  return out;
72  }
73 
75  template <typename T>
76  void peek(T &t) const {
77  T* cur = (T*) _pos;
78  T *next = cur + 1;
79  if( _end < next ) throw eof();
80  t = *cur;
81  }
82 
84  template <typename T>
85  T peek() const {
86  T out;
87  peek(out);
88  return out;
89  }
90 
92  unsigned offset() const { return (char*)_pos - (char*)_start; }
93 
95  unsigned remaining() const { return (char*)_end -(char*)_pos; }
96 
98  void rewind(unsigned nbytes) {
99  _pos = ((char *) _pos) - nbytes;
100  verify( _pos >= _start );
101  }
102 
104  const void* skip(unsigned len) {
105  const char *nxt = ((char *) _pos) + len;
106  if( _end < nxt ) throw eof();
107  const void *p = _pos;
108  _pos = nxt;
109  return p;
110  }
111 
113  StringData readCStr() {
114  const char* start = static_cast<const char*>(pos());
115  size_t len = strnlen(start, remaining()-1);
116  if (start[len] != '\0') throw eof(); // no NUL byte in remaining bytes
117  skip(len + 1/*NUL byte*/);
118  return StringData(start, len);
119  }
120 
121  void readStr(string& s) {
122  s = readCStr().toString();
123  }
124 
125  const void* pos() { return _pos; }
126  const void* start() { return _start; }
127 
128  private:
129  const void *_start;
130  const void *_pos;
131  const void *_end;
132  };
133 
134 }
StringData readCStr()
reads a NUL terminated string
Definition: bufreader.h:113
unsigned offset() const
return current offset into buffer
Definition: bufreader.h:92
LogstreamBuilder out()
Synonym for log().
Definition: log.h:79
helper to read and parse a block of memory methods throw the eof exception if the operation would pas...
Definition: bufreader.h:44
void peek(T &t) const
read in the object specified, but do not advance buffer pointer
Definition: bufreader.h:76
const void * skip(unsigned len)
return current position pointer, and advance by len
Definition: bufreader.h:104
T read()
read in and return an object of the specified type, and advance buffer pointer
Definition: bufreader.h:68
void read(T &t)
read in the object specified, and advance buffer pointer
Definition: bufreader.h:58
unsigned remaining() const
return remaining bytes
Definition: bufreader.h:95
void rewind(unsigned nbytes)
back up by nbytes
Definition: bufreader.h:98
Definition: bufreader.h:46
T peek() const
read in and return an object of the specified type, but do not advance buffer pointer ...
Definition: bufreader.h:85