1 #ifndef LIBFASTER_FASTCOMMBUFFER_H 2 #define LIBFASTER_FASTCOMMBUFFER_H 14 const int BUFFER_INITIAL_SIZE = 512*1024;
20 size_t _allocatedSize;
28 void setBuffer(
void * buffer,
size_t s);
33 char * pos(
size_t pos);
36 void advance(
size_t pos);
44 void write(T &v,
size_t s){
46 std::copy_n( (
char*)&v, s , _data+_size );
51 void writePos(
const T &v,
size_t s,
size_t pos){
53 std::copy_n((
char*) &v, s, _data + pos);
59 void writePos(
const T &v,
size_t pos){
60 writePos(v,
sizeof(T), pos);
64 inline void writeSafe(T * v,
size_t s){
65 std::copy_n( (
char*)v, s, &_data[_size]);
70 inline void write(T * v,
size_t s){
81 write( v,
sizeof(T) );
83 void write(std::string i){
84 size_t s = i.length();
85 grow(_size +
sizeof(
size_t) + s );
86 writeSafe( &s,
sizeof(
size_t) );
87 writeSafe( i.data(), s );
89 void write(std::vector<std::string> v){
91 grow(_size +
sizeof(
size_t) + s );
92 writeSafe( &s,
sizeof(
size_t) );
98 void write(std::vector<T> v){
100 grow(_size +
sizeof(
size_t) + s *
sizeof(T));
101 writeSafe( &s,
sizeof(
size_t) );
102 writeSafe( v.data(), s *
sizeof(T) );
104 template <
typename K,
typename T>
105 void write(std::pair<K,T> p){
109 template <
typename K,
typename T>
110 void write(std::tuple<K,T, size_t> t){
111 write(std::get<0>(t));
112 write(std::get<1>(t));
113 write(std::get<2>(t));
116 void writePos(
procstat &s,
size_t pos);
121 template <
typename T>
122 void read(T & v,
size_t s){
123 std::copy_n(_data + _size, s, (
char*) &v);
126 template <
typename T>
127 void read(T * v,
size_t s){
128 std::copy_n(_data + _size, s, (
char*) v);
131 template <
typename T>
133 read( &v,
sizeof(T) );
135 template <
typename T>
136 void readVec(std::vector<T> & v,
size_t s){
138 v.assign((T*) &_data[_size], (T*) &_data[_size + s] );
141 void read(std::vector<std::string> & v){
145 for (
size_t i = 0 ; i < s ; i++){
149 void readString(std::string & v,
size_t s){
151 std::copy_n( _data + _size, s, (
char*)v.data() );
154 template <
typename T>
155 void read(std::vector<T> & v){
158 readVec(v, size*
sizeof(T));
160 void read(std::string & s){
165 template <
typename K,
typename T>
166 void read(std::pair<K,T> & p){
170 template <
typename K,
typename T>
171 void read(std::tuple<K,T, size_t> & t){
178 t = std::make_tuple (k,type,s);
183 template <
typename T>
189 template <
typename T>