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 "orcus/env.hpp"
12 #include "orcus/exception.hpp"
13 
14 #include <string>
15 #include <memory>
16 #include <vector>
17 
18 namespace orcus {
19 
20 class pstring;
21 struct json_config;
22 
23 namespace json {
24 
25 struct json_value;
26 struct document_resource;
27 class document_tree;
28 
32 class ORCUS_DLLPUBLIC document_error : public general_error
33 {
34 public:
35  document_error(const std::string& msg);
36  virtual ~document_error() throw();
37 };
38 
44 class ORCUS_DLLPUBLIC key_value_error : public document_error
45 {
46 public:
47  key_value_error(const std::string& msg);
48  virtual ~key_value_error() throw();
49 };
50 
51 enum class node_t : uint8_t
52 {
54  unset = 0,
56  string = 1,
58  number = 2,
63  object = 3,
67  array = 4,
71  boolean_true = 5,
75  boolean_false = 6,
79  null = 7,
80 };
81 
82 namespace detail { namespace init { class node; }}
83 
84 class const_node;
85 class document_tree;
86 
87 class ORCUS_DLLPUBLIC const_node_iterator
88 {
89  friend class const_node;
90 
91  struct impl;
92  std::unique_ptr<impl> mp_impl;
93 
94  const_node_iterator(const document_tree* doc, const const_node& v, bool begin);
95 
96 public:
100 
101  const const_node& operator*() const;
102  const const_node* operator->() const;
103 
104  const_node_iterator& operator++();
105  const_node_iterator operator++(int);
106 
107  const_node_iterator& operator--();
108  const_node_iterator operator--(int);
109 
110  bool operator== (const const_node_iterator& other) const;
111  bool operator!= (const const_node_iterator& other) const;
112 
113  const_node_iterator& operator= (const const_node_iterator& other);
114 };
115 
120 class ORCUS_DLLPUBLIC const_node
121 {
122  friend class document_tree;
123  friend class const_node_iterator;
124 
125 protected:
126  struct impl;
127  std::unique_ptr<impl> mp_impl;
128 
129  const_node(const document_tree* doc, json_value* jv);
130  const_node(std::unique_ptr<impl>&& p);
131 public:
132  const_node() = delete;
133 
134  const_node(const const_node& other);
135  const_node(const_node&& rhs);
136  ~const_node();
137 
143  node_t type() const;
144 
150  size_t child_count() const;
151 
159  std::vector<pstring> keys() const;
160 
175  pstring key(size_t index) const;
176 
186  bool has_key(const pstring& key) const;
200  const_node child(size_t index) const;
201 
212  const_node child(const pstring& key) const;
213 
223 
232  const_node back() const;
233 
243 
252  double numeric_value() const;
253 
254  const_node& operator=(const 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[](const pstring& key);
290 
304  node child(size_t index);
305 
316  node child(const pstring& 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(const std::string& strm, const json_config& config);
454 
463  void load(const char* p, size_t n, const json_config& config);
464 
471 
478 
484  std::string dump() const;
485 
492  std::string dump_xml() const;
493 
499  void swap(document_tree& other);
500 };
501 
502 }}
503 
504 #endif
505 
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
orcus::json::object
Definition: json_document_tree.hpp:371
orcus::json::const_node_iterator
Definition: json_document_tree.hpp:88
orcus::json::document_tree::dump_xml
std::string dump_xml() const
orcus::json::document_tree::load
void load(const std::string &strm, const json_config &config)
orcus::json::document_tree::dump
std::string dump() const
orcus::json::detail::init::node
Definition: json_document_tree.hpp:387
orcus::json::document_tree::get_document_root
json::node get_document_root()
orcus::json::array
Definition: json_document_tree.hpp:353
orcus::json::node::child
node child(size_t index)
orcus::json::node
Definition: json_document_tree.hpp:274
orcus::general_error
Definition: exception.hpp:19
orcus::json::const_node::string_value
pstring string_value() const
orcus::json::const_node::keys
std::vector< pstring > keys() const
orcus::config
Definition: config.hpp:19
orcus::json::const_node::numeric_value
double numeric_value() const
orcus::json::document_tree
Definition: json_document_tree.hpp:423
orcus::json_config
Definition: config.hpp:61
orcus::json::node::push_back
void push_back(const detail::init::node &v)
orcus::json::const_node::key
pstring key(size_t index) const
orcus::json::document_tree::swap
void swap(document_tree &other)
orcus::json::const_node::child
const_node child(size_t index) const
orcus::json::const_node::back
const_node back() const
orcus::pstring
Definition: pstring.hpp:28
orcus::json::document_tree::get_document_root
json::const_node get_document_root() const
orcus::json::const_node
Definition: json_document_tree.hpp:121
orcus::json::node::child
node child(const pstring &key)
orcus::json::document_tree::load
void load(const char *p, size_t n, const json_config &config)
orcus::json::const_node::type
node_t type() const
orcus::json::node::parent
node parent()
orcus::json::const_node::child
const_node child(const pstring &key) const
orcus::json::const_node::parent
const_node parent() const
orcus::json::document_error
Definition: json_document_tree.hpp:33
orcus::json::key_value_error
Definition: json_document_tree.hpp:45
orcus::json::const_node::has_key
bool has_key(const pstring &key) const
orcus::json::const_node::child_count
size_t child_count() const
orcus::json::node::back
node back()
orcus::json::const_node::identity
uintptr_t identity() const