commore  1.0.6-SNAPSHOT
All Classes Namespaces Functions Variables Typedefs Enumerations Pages
CommBuffer.h
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 
6 #ifndef CMR_COMM_BUFFER_INCLUDED
7 #define CMR_COMM_BUFFER_INCLUDED
8 
9 #include "Commore.h"
10 #include "List.h"
11 
12 namespace commore
13 {
28  class CMREXD CommBuffer
29  {
30 
31  typedef CommBuffer* PCommBuffer;
32 
33  public:
34  class Page;
35  typedef Page* PPage;
36 
37  private :
41  class DictionaryManager
42  {
43  friend class CommBuffer;
44 
45  private :
46  ListAString attributes;
47  public :
48  DictionaryManager();
49  private:
50  DictionaryManager(const DictionaryManager&);//forbidden=>not implemented
51 
52  public :
53  int get_element_pos(const AString& elmt) const;
54  AString get_element_at(int pos) const;
55  bool add_element(const AString& elmt);
56  size_t size() const
57  {
58  return attributes.size();
59  }
60 
61  private:
62  void clear();
63  void splice(DictionaryManager& src);
64 
65  };
66 
67  mutable DictionaryManager dictionary_manager_;
68 
69  public:
70  CommBuffer();
71  CommBuffer(size_t page_size);
72  CommBuffer(const CommBuffer& C);
73  CommBuffer& operator=(const CommBuffer& C);
74  ~CommBuffer();
75 
76  public:
77 
78  class Page
79  {
80  public:
81  enum
82  {
83  HEADER_SIZE = 21,
84  NB_FLAGS = 4,
85  VERSION = 1
86  };
87 
88  enum Flags
89  {
90  NO_FLAG = 'x',
91  COMPRESSED = 'c',
92  CRYPTED = 'i',
93  ASCII = 'a'
94  };
95 
96  public :
97  //HEADER
98  bool is_last_page_;
99  int version_;
100  Byte flags_[NB_FLAGS];
101 
102  PCommBuffer cont_;
103  size_t size_;
104  size_t page_size_;
105  PPage next_;
106  enum { DECLARED_DATA_SIZE = 4 };
107  Byte s_[DECLARED_DATA_SIZE];
108  Page() : next_(0) {}
109 
110  public :
115  static bool decodeHeader(Byte* header , int* version, Byte flag[], size_t* size, int* chunkTag);
116 
117  void update_header();
118  bool update_from_header();
119  void set_is_last_page(bool value)
120  {
121  is_last_page_ = value;
122  };
123  bool is_last_page() const
124  {
125  return is_last_page_;
126  }
127 
128  bool push_flag(Byte flag);
129  void clear_flag(Byte flag);
130  bool has_flag(Byte flag);
131  void clear_flags();
132 
133  size_t get_size() const
134  {
135  return size_;
136  }
137  Byte* get() const
138  {
139  return (Byte*)s_;
140  }
141  };
142 
143  struct Pos
144  {
145  PPage current_page_;
146  size_t current_pos_;
147  bool on_first_page_;
148  Pos() : current_page_(0), current_pos_(0), on_first_page_(true) {}
149  };
150 
151  class CMREXD PageIterator
152  {
153  public:
154  PageIterator(const CommBuffer& buf);
155  public:
156  Page* next();
157  bool hasNext() const
158  {
159  return p_ != 0;
160  }
161  private:
162  Page* p_;
163  };
164  friend class PageIterator;
165 
166  public:
167  bool operator== (const CommBuffer& C) const;
168  bool operator!= (const CommBuffer& C) const;
169  void set_size(size_t size)
170  {
171  total_size_ = size;
172  }
173  size_t size() const
174  {
175  return total_size_;
176  }
177  void clear();
178  bool is_ascii() const
179  {
180  return is_ascii_;
181  }
182  void set_is_ascii(bool is_ascii)
183  {
184  is_ascii_ = is_ascii;
185  }
186  void update_ascii_flags();
187 
188 
189  void update_headers();
190 
191 
198  Byte* allocate(size_t& size);
199 
205  Byte* allocate_in_new_page(size_t size, bool reserveHeader);
206 
211  size_t add_size(size_t size);
212 
217  void splice(CommBuffer& x);
221  void dump() const;
225  void rewind();
226 
230  long write_byte(const Byte* b, size_t size);
231  long write_raw(const Byte* b, size_t size);
232  long read_byte(Byte* b, size_t size) const;
233 
234  long write_using_dictionary(const AString& v);
235  long write(const AString& v);
236  long write(const String& v);
237  long write(const StringBuffer& v);
238  long write(const Blob& v);
239  long write(const TimePeriod& v);
240  long write(const TimeDate& v);
241  long write(const Symbol& v);
242  long write(const Tuple& v);
243  long write(const Bool& v);
244  long write(const Int& v);
245  long write(const UInt& v);
246  long write(const long& v);
247  long write(const unsigned long& v);
248  long write(const Long& v);
249  long write(const ULong& v);
250  long write(const Short& v);
251  long write(const UShort& v);
252  long write(const Float& v);
253  long write(const Double& v);
254  long write(const ArrayInt& v);
255  long write(const ArrayLong& v);
256  long write(const ArrayFloat& v);
257  long write(const ArrayDouble& v);
258  long write(const ListBool& v);
259  long write(const ListInt& v);
260  long write(const ListUInt& v);
261  long write(const ListLong& v);
262  long write(const ListULong& v);
263  long write(const ListShort& v);
264  long write(const ListUShort& v);
265  long write(const ListFloat& v);
266  long write(const ListDouble& v);
267  long write(const ListAString& v);
268  long write(const ListString& v);
269  long write(const ListStringBuffer& v);
270  long write(const ListTimePeriod& v);
271  long write(const ListTimeDate& v);
272  long write(const ListBlob& v);
273  long write(const ListSymbol& v);
274  long write(const ListTuple& v);
275  long write(const ListArrayInt& v);
276  long write(const ListArrayLong& v);
277  long write(const ListArrayFloat& v);
278  long write(const ListArrayDouble& v);
279 
280 
281  long read_using_dictionary(AString& v) const;
282  long read(AString& v) const;
283  long read(String& v) const;
284  long read(StringBuffer& v) const;
285  long read(Blob& v) const;
286  long read(TimePeriod& v) const;
287  long read(TimeDate& v) const;
288  long read(Symbol& v) const;
289  long read(Tuple& v) const;
290  long read(Bool& v) const;
291  long read(Int& v) const;
292  long read(UInt& v) const;
293  long read(long& v) const;
294  long read(unsigned long& v) const;
295  long read(Long& v) const;
296  long read(ULong& v) const;
297  long read(Short& v) const;
298  long read(UShort& v) const;
299  long read(Float& v) const;
300  long read(Double& v) const;
301  long read(ArrayInt& v) const;
302  long read(ArrayLong& v) const;
303  long read(ArrayFloat& v) const;
304  long read(ArrayDouble& v) const;
305  long read(ListBool& v) const;
306  long read(ListInt& v) const;
307  long read(ListUInt& v) const;
308  long read(ListLong& v) const;
309  long read(ListULong& v) const;
310  long read(ListShort& v) const;
311  long read(ListUShort& v) const;
312  long read(ListFloat& v) const;
313  long read(ListDouble& v) const;
314  long read(ListAString& v) const;
315  long read(ListString& v) const;
316  long read(ListStringBuffer& v) const;
317  long read(ListTimePeriod& v) const;
318  long read(ListTimeDate& v) const;
319  long read(ListBlob& v) const;
320  long read(ListSymbol& v) const;
321  long read(ListTuple& v) const;
322  long read(ListArrayInt& v) const;
323  long read(ListArrayLong& v) const;
324  long read(ListArrayFloat& v) const;
325  long read(ListArrayDouble& v) const;
326  Pos get_pos() const;
327  void set_pos(const Pos& pos);
328  long write(OBStream& o) const;
329  long read(IBStream& o);
330  long write_file(const Path& file_name) const;
331  long read_file(const Path& file_name);
332 
333  AString to_string() const;
334 
335  private:
336  void add_page_(bool reserveHeader);
337  void add_page_(size_t size, bool reserveHeader);
338 
339  private:
340  size_t default_page_size_;
341  size_t total_size_;
342  PPage first_;
343  PPage last_;
344  mutable Pos pos_;
345  bool is_ascii_;
346 
347  public :
348 
349  PPage get_last()
350  {
351  return last_;
352  }
353 
354  enum
355  {
356  DEFAULT_PAGE_SIZE = 4096, /* bigger than HEADER_SIZE */
357  };
358  };
359 };
360 
361 #endif
Definition: CommBuffer.h:151
Definition: AString.h:39
Definition: CommBuffer.h:78
Definition: CommBuffer.h:28
Definition: CommBuffer.h:143
unsigned char Byte
Definition: Type.h:64