Orcus
json_document_tree.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 
8 #ifndef INCLUDED_ORCUS_JSON_DOCUMENT_TREE_HPP
9 #define INCLUDED_ORCUS_JSON_DOCUMENT_TREE_HPP
10 
11 #include "env.hpp"
12 #include "exception.hpp"
13 
14 #include <string>
15 #include <memory>
16 #include <vector>
17 
18 namespace orcus {
19 
20 struct json_config;
21 
22 namespace json {
23 
24 struct json_value;
25 struct document_resource;
26 class document_tree;
27 
31 class ORCUS_DLLPUBLIC document_error : public general_error
32 {
33 public:
34  document_error(const std::string& msg);
35  virtual ~document_error();
36 };
37 
43 class ORCUS_DLLPUBLIC key_value_error : public document_error
44 {
45 public:
46  key_value_error(const std::string& msg);
47  virtual ~key_value_error();
48 };
49 
50 enum class node_t : uint8_t
51 {
53  unset = 0,
55  string = 1,
57  number = 2,
62  object = 3,
66  array = 4,
70  boolean_true = 5,
74  boolean_false = 6,
78  null = 7,
79 };
80 
81 namespace detail { namespace init { class node; }}
82 
83 class const_node;
84 class document_tree;
85 
86 class ORCUS_DLLPUBLIC const_node_iterator
87 {
88  friend class const_node;
89 
90  struct impl;
91  std::unique_ptr<impl> mp_impl;
92 
93  const_node_iterator(const document_tree* doc, const const_node& v, bool begin);
94 
95 public:
99 
100  const const_node& operator*() const;
101  const const_node* operator->() const;
102 
103  const_node_iterator& operator++();
104  const_node_iterator operator++(int);
105 
106  const_node_iterator& operator--();
107  const_node_iterator operator--(int);
108 
109  bool operator== (const const_node_iterator& other) const;
110  bool operator!= (const const_node_iterator& other) const;
111 
112  const_node_iterator& operator= (const const_node_iterator& other);
113 };
114 
119 class ORCUS_DLLPUBLIC const_node
120 {
121  friend class document_tree;
122  friend class const_node_iterator;
123 
124 protected:
125  struct impl;
126  std::unique_ptr<impl> mp_impl;
127 
128  const_node(const document_tree* doc, json_value* jv);
129  const_node(std::unique_ptr<impl>&& p);
130 public:
131  const_node() = delete;
132 
133  const_node(const const_node& other);
134  const_node(const_node&& rhs);
135  ~const_node();
136 
142  node_t type() const;
143 
149  size_t child_count() const;
150 
158  std::vector<std::string_view> keys() const;
159 
174  std::string_view key(size_t index) const;
175 
185  bool has_key(std::string_view key) const;
199  const_node child(size_t index) const;
200 
211  const_node child(std::string_view key) const;
212 
222 
231  const_node back() const;
232 
241  std::string_view string_value() const;
242 
251  double numeric_value() const;
252 
253  const_node& operator=(const const_node& other);
254  const_node& operator=(const_node&& other);
255 
263  uintptr_t identity() const;
264 
265  const_node_iterator begin() const;
266  const_node_iterator end() const;
267 };
268 
273 class ORCUS_DLLPUBLIC node : public const_node
274 {
275  friend class document_tree;
276 
277  node(const document_tree* doc, json_value* jv);
278  node(const_node&& rhs);
279 
280 public:
281  node() = delete;
282 
283  node(const node& other);
284  node(node&& rhs);
285  ~node();
286 
287  node& operator=(const node& other);
288  node& operator=(const detail::init::node& v);
289  node operator[](std::string_view key);
290 
304  node child(size_t index);
305 
316  node child(std::string_view key);
317 
327 
337 
346 };
347 
352 class ORCUS_DLLPUBLIC array
353 {
354  friend class detail::init::node;
355  friend class document_tree;
356 
357  std::vector<detail::init::node> m_vs;
358 public:
359  array();
360  array(const array&) = delete;
361  array(array&& other);
362  array(std::initializer_list<detail::init::node> vs);
363  ~array();
364 };
365 
370 class ORCUS_DLLPUBLIC object
371 {
372 public:
373  object();
374  object(const object&) = delete;
375  object(object&& other);
376  ~object();
377 };
378 
379 namespace detail { namespace init {
380 
386 class ORCUS_DLLPUBLIC node
387 {
388  friend class ::orcus::json::document_tree;
389  friend class ::orcus::json::node;
390 
391  struct impl;
392  std::unique_ptr<impl> mp_impl;
393 
394 public:
395  node(double v);
396  node(int v);
397  node(bool b);
398  node(std::nullptr_t);
399  node(const char* p);
400  node(const std::string& s);
401  node(std::initializer_list<detail::init::node> vs);
403  node(json::object obj);
404 
405  node(const node& other) = delete;
406  node(node&& other);
407  ~node();
408 
409  node& operator= (node other) = delete;
410 
411 private:
412  node_t type() const;
413  json_value* to_json_value(document_resource& res) const;
414  void store_to_node(document_resource& res, json_value* parent) const;
415 };
416 
417 }}
418 
422 class ORCUS_DLLPUBLIC document_tree
423 {
424  friend class const_node;
425  friend class node;
426 
427  struct impl;
428  std::unique_ptr<impl> mp_impl;
429 
430  const document_resource& get_resource() const;
431 
432 public:
433  document_tree();
434  document_tree(const document_tree&) = delete;
435  document_tree(document_tree&& other);
436  document_tree(document_resource& res);
437  document_tree(std::initializer_list<detail::init::node> vs);
438  document_tree(array vs);
439  document_tree(object obj);
440  ~document_tree();
441 
442  document_tree& operator= (std::initializer_list<detail::init::node> vs);
443  document_tree& operator= (array vs);
444  document_tree& operator= (object obj);
445 
453  void load(std::string_view stream, const json_config& config);
454 
461 
468 
474  std::string dump() const;
475 
482  std::string dump_xml() const;
483 
490  std::string dump_yaml() const;
491 
497  void swap(document_tree& other);
498 };
499 
500 }}
501 
502 #endif
503 
504 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: exception.hpp:19
Definition: json_document_tree.hpp:353
Definition: json_document_tree.hpp:87
Definition: json_document_tree.hpp:120
bool has_key(std::string_view key) const
std::vector< std::string_view > keys() const
const_node back() const
std::string_view key(size_t index) const
const_node child(size_t index) const
std::string_view string_value() const
double numeric_value() const
size_t child_count() const
uintptr_t identity() const
const_node parent() const
const_node child(std::string_view key) const
Definition: json_document_tree.hpp:387
Definition: json_document_tree.hpp:32
Definition: json_document_tree.hpp:423
json::node get_document_root()
std::string dump() const
std::string dump_yaml() const
void swap(document_tree &other)
json::const_node get_document_root() const
void load(std::string_view stream, const json_config &config)
std::string dump_xml() const
Definition: json_document_tree.hpp:44
Definition: json_document_tree.hpp:274
node child(size_t index)
void push_back(const detail::init::node &v)
node child(std::string_view key)
Definition: json_document_tree.hpp:371
Definition: config.hpp:20
Definition: config.hpp:60