tree-gen
C++ code generator for tree structures
tree-cbor.hpp.inc
Go to the documentation of this file.
1 
5 #include <memory>
6 #include <string>
7 #include <iostream>
8 #include <map>
9 #include <vector>
10 #include <stack>
11 
12 TREE_NAMESPACE_BEGIN
13 
17 namespace cbor {
18 
19 // Forward declaration to Reader so we can do the ArrayReader and MapReader
20 // typedefs.
21 class Reader;
22 
27 using ArrayReader = TREE_VECTOR(Reader);
28 
33 using MapReader = TREE_MAP(std::string, Reader);
34 
38 class Reader {
39 private:
40 
44  std::shared_ptr<std::string> data;
45 
49  size_t slice_offset;
50 
54  size_t slice_length;
55 
56 public:
57 
62  explicit Reader(const std::string &data);
63 
68  explicit Reader(std::string &&data);
69 
70 private:
71 
75  Reader(const Reader &parent, size_t offset, size_t length);
76 
80  Reader slice(size_t offset, size_t length) const;
81 
85  uint8_t read_at(size_t offset) const;
86 
93  uint64_t read_intlike(uint8_t info, size_t &offset) const;
94 
101  void read_stringlike(size_t &offset, std::ostream &s) const;
102 
107  void check_and_seek(size_t &offset) const;
108 
113  void check() const;
114 
128  const char *get_type_name() const;
129 
130 public:
131 
135  bool is_null() const;
136 
141  void as_null() const;
142 
146  bool is_bool() const;
147 
152  bool as_bool() const;
153 
157  bool is_int() const;
158 
164  int64_t as_int() const;
165 
170  bool is_float() const;
171 
177  double as_float() const;
178 
182  bool is_string() const;
183 
189  std::string as_string() const;
190 
194  bool is_binary() const;
195 
201  std::string as_binary() const;
202 
206  bool is_array() const;
207 
208 private:
209 
214  void read_array_item(size_t &offset, ArrayReader &ar) const;
215 
216 public:
217 
222  ArrayReader as_array() const;
223 
227  bool is_map() const;
228 
229 private:
230 
235  void read_map_item(size_t &offset, MapReader &map) const;
236 
237 public:
238 
243  MapReader as_map() const;
244 
248  std::string get_contents() const;
249 
250 };
251 
252 // Forward declarations for the writer classes, so we can use them in friend
253 // declarations.
254 class Writer;
255 class ArrayWriter;
256 class MapWriter;
257 
261 class StructureWriter {
262 private:
263 
267  Writer *writer;
268 
273  size_t id;
274 
275 protected:
276 
280  explicit StructureWriter(Writer &writer);
281 
286  std::ostream &stream();
287 
291  void write_null();
292 
296  void write_bool(bool value);
297 
303  void write_int(int64_t value, uint8_t major=0);
304 
308  void write_float(double value);
309 
313  void write_string(const std::string &value);
314 
318  void write_binary(const std::string &value);
319 
325  ArrayWriter write_array();
326 
332  MapWriter write_map();
333 
334 public:
335 
340  virtual ~StructureWriter();
341 
342  // Delete copy constructor; deletion of one of these objects closes the
343  // structure, so any copies would become invalid.
344  StructureWriter(StructureWriter &src) = delete;
345  StructureWriter &operator=(const StructureWriter &src) = delete;
346 
347  // Move constructor/assignment is fine though. The original will be made
348  // invalid by clearing the ID field, so it doesn't close the structure upon
349  // deletion.
350  StructureWriter(StructureWriter &&src);
351  StructureWriter &operator=(StructureWriter &&src);
352 
357  void close();
358 
359 };
360 
364 class ArrayWriter : public StructureWriter {
365 protected:
366 
370  friend class StructureWriter;
371 
376  explicit ArrayWriter(Writer &writer);
377 
378 public:
379 
383  void append_null();
384 
388  void append_bool(bool value);
389 
393  void append_int(int64_t value);
394 
398  void append_float(double value);
399 
403  void append_string(const std::string &value);
404 
408  void append_binary(const std::string &value);
409 
415  ArrayWriter append_array();
416 
422  MapWriter append_map();
423 
424 };
425 
429 class MapWriter : public StructureWriter {
430 protected:
431 
435  friend class Writer;
436 
440  friend class StructureWriter;
441 
446  explicit MapWriter(Writer &writer);
447 
448 public:
449 
453  void append_null(const std::string &key);
454 
458  void append_bool(const std::string &key, bool value);
459 
463  void append_int(const std::string &key, int64_t value);
464 
469  void append_float(const std::string &key, double value);
470 
474  void append_string(const std::string &key, const std::string &value);
475 
479  void append_binary(const std::string &key, const std::string &value);
480 
487  ArrayWriter append_array(const std::string &key);
488 
495  MapWriter append_map(const std::string &key);
496 
497 };
498 
502 class Writer {
503 private:
504 
508  friend StructureWriter;
509 
513  std::ostream &stream;
514 
520  std::stack<size_t> stack;
521 
525  size_t id_counter;
526 
527 public:
528 
532  Writer(std::ostream &stream);
533 
540  MapWriter start();
541 
542 };
543 
544 } // namespace cbor
545 TREE_NAMESPACE_END