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
18namespace orcus {
19
20class pstring;
21struct json_config;
22
23namespace json {
24
25struct json_value;
26struct document_resource;
27class document_tree;
28
32class ORCUS_DLLPUBLIC document_error : public general_error
33{
34public:
35 document_error(const std::string& msg);
36 virtual ~document_error() throw();
37};
38
44class ORCUS_DLLPUBLIC key_value_error : public document_error
45{
46public:
47 key_value_error(const std::string& msg);
48 virtual ~key_value_error() throw();
49};
50
51enum 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
82namespace detail { namespace init { class node; }}
83
84class const_node;
85class document_tree;
86
87class 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
96public:
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
120class ORCUS_DLLPUBLIC const_node
121{
122 friend class document_tree;
123 friend class const_node_iterator;
124
125protected:
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);
131public:
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
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
273class 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
280public:
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
352class ORCUS_DLLPUBLIC array
353{
354 friend class detail::init::node;
355 friend class document_tree;
356
357 std::vector<detail::init::node> m_vs;
358public:
359 array();
360 array(const array&) = delete;
361 array(array&& other);
362 array(std::initializer_list<detail::init::node> vs);
363 ~array();
364};
365
370class ORCUS_DLLPUBLIC object
371{
372public:
373 object();
374 object(const object&) = delete;
375 object(object&& other);
376 ~object();
377};
378
379namespace detail { namespace init {
380
386class 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
394public:
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
411private:
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
422class 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
432public:
434 document_tree(const document_tree&) = delete;
436 document_tree(document_resource& res);
437 document_tree(std::initializer_list<detail::init::node> vs);
439 document_tree(object obj);
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: */
Definition: exception.hpp:19
Definition: json_document_tree.hpp:353
Definition: json_document_tree.hpp:88
Definition: json_document_tree.hpp:121
pstring key(size_t index) const
const_node back() const
const_node child(size_t index) const
double numeric_value() const
bool has_key(const pstring &key) const
size_t child_count() const
uintptr_t identity() const
pstring string_value() const
const_node parent() const
const_node child(const pstring &key) const
std::vector< pstring > keys() const
Definition: json_document_tree.hpp:387
Definition: json_document_tree.hpp:33
Definition: json_document_tree.hpp:423
json::node get_document_root()
std::string dump() const
void load(const char *p, size_t n, const json_config &config)
void load(const std::string &strm, const json_config &config)
void swap(document_tree &other)
json::const_node get_document_root() const
std::string dump_xml() const
Definition: json_document_tree.hpp:45
Definition: json_document_tree.hpp:274
node child(size_t index)
void push_back(const detail::init::node &v)
node child(const pstring &key)
Definition: json_document_tree.hpp:371
Definition: pstring.hpp:28
Definition: config.hpp:19
Definition: config.hpp:61