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 class string_pool;
22 struct json_config;
23 
24 namespace json {
25 
26 struct json_value;
27 struct json_value_store;
28 class document_tree;
29 
33 class ORCUS_DLLPUBLIC document_error : public general_error
34 {
35 public:
36  document_error(const std::string& msg);
37  virtual ~document_error() throw();
38 };
39 
45 class ORCUS_DLLPUBLIC key_value_error : public document_error
46 {
47 public:
48  key_value_error(const std::string& msg);
49  virtual ~key_value_error() throw();
50 };
51 
52 enum class node_t : uint8_t
53 {
55  unset = 0,
57  string = 1,
59  number = 2,
64  object = 3,
68  array = 4,
72  boolean_true = 5,
76  boolean_false = 6,
80  null = 7,
81 };
82 
83 namespace detail { namespace init { class node; }}
84 
89 class ORCUS_DLLPUBLIC const_node
90 {
91  friend class document_tree;
92 
93 protected:
94  struct impl;
95  std::unique_ptr<impl> mp_impl;
96 
97  const_node(const document_tree* doc, json_value* jv);
98  const_node(std::unique_ptr<impl>&& p);
99 public:
100  const_node() = delete;
101 
102  const_node(const const_node& other);
103  const_node(const_node&& rhs);
104  ~const_node();
105 
111  node_t type() const;
112 
118  size_t child_count() const;
119 
127  std::vector<pstring> keys() const;
128 
143  pstring key(size_t index) const;
144 
158  const_node child(size_t index) const;
159 
170  const_node child(const pstring& key) const;
171 
180  const_node parent() const;
181 
190  pstring string_value() const;
191 
200  double numeric_value() const;
201 
202  const_node& operator=(const const_node& other);
203 
211  uintptr_t identity() const;
212 };
213 
218 class ORCUS_DLLPUBLIC node : public const_node
219 {
220  friend class document_tree;
221 
222  node(const document_tree* doc, json_value* jv);
223  node(const_node&& rhs);
224 
225 public:
226  node() = delete;
227 
228  node(const node& other);
229  node(node&& rhs);
230  ~node();
231 
232  node& operator=(const node& other);
233  node& operator=(const detail::init::node& v);
234  node operator[](const pstring& key);
235 
249  node child(size_t index);
250 
261  node child(const pstring& key);
262 
271  node parent();
272 
280  void push_back(const detail::init::node& v);
281 };
282 
287 class ORCUS_DLLPUBLIC array
288 {
289  friend class detail::init::node;
290  friend class document_tree;
291 
292  std::initializer_list<detail::init::node> m_vs;
293 public:
294  array();
295  array(const array&) = delete;
296  array(array&& other);
297  array(std::initializer_list<detail::init::node> vs);
298  ~array();
299 };
300 
305 class ORCUS_DLLPUBLIC object
306 {
307 public:
308  object();
309  object(const object&) = delete;
310  object(object&& other);
311  ~object();
312 };
313 
314 namespace detail { namespace init {
315 
321 class ORCUS_DLLPUBLIC node
322 {
323  friend class ::orcus::json::document_tree;
324  friend class ::orcus::json::node;
325 
326  struct impl;
327  std::unique_ptr<impl> mp_impl;
328 
329 public:
330  node(double v);
331  node(int v);
332  node(bool b);
333  node(std::nullptr_t);
334  node(const char* p);
335  node(std::initializer_list<detail::init::node> vs);
337  node(json::object obj);
338 
339  node(const node&) = delete;
340  node(node&& other);
341  ~node();
342 
343  node& operator= (node) = delete;
344 
345 private:
346  node_t type() const;
347  std::unique_ptr<json_value> to_json_value(string_pool& pool) const;
348  void store_to_node(string_pool& pool, json_value* parent) const;
349 };
350 
351 }}
352 
356 class ORCUS_DLLPUBLIC document_tree
357 {
358  friend class const_node;
359  friend class node;
360 
361  struct impl;
362  std::unique_ptr<impl> mp_impl;
363 
364  const string_pool& get_string_pool() const;
365 
366 public:
367  document_tree();
368  document_tree(const document_tree&) = delete;
369  document_tree(document_tree&& other);
370  document_tree(string_pool& pool);
371  document_tree(std::initializer_list<detail::init::node> vs);
372  document_tree(array vs);
373  document_tree(object obj);
374  ~document_tree();
375 
376  document_tree& operator= (std::initializer_list<detail::init::node> vs);
377  document_tree& operator= (array vs);
378  document_tree& operator= (object obj);
379 
387  void load(const std::string& strm, const json_config& config);
388 
397  void load(const char* p, size_t n, const json_config& config);
398 
404  json::const_node get_document_root() const;
405 
411  json::node get_document_root();
412 
418  std::string dump() const;
419 
426  std::string dump_xml() const;
427 
433  void swap(document_tree& other);
434 };
435 
436 }}
437 
438 #endif
439 
440 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: pstring.hpp:24
Definition: exception.hpp:18
Definition: json_document_tree.hpp:45
Definition: json_document_tree.hpp:305
Definition: config.hpp:18
Definition: json_document_tree.hpp:356
Definition: json_document_tree.hpp:33
Definition: string_pool.hpp:22
Definition: config.hpp:60
Definition: json_document_tree.hpp:89
Definition: json_document_tree.hpp:287
Definition: json_document_tree.hpp:218
Definition: json_document_tree.hpp:321
Definition: base64.hpp:15