Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/fmt/json/dom/Node.h

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 30 янв. 2021 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_FMT_JSON_DOM_NODE_H_
23 #define LSP_PLUG_IN_FMT_JSON_DOM_NODE_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/common/types.h>
27 #include <lsp-plug.in/common/status.h>
28 #include <lsp-plug.in/runtime/LSPString.h>
29 #include <lsp-plug.in/lltl/parray.h>
30 #include <lsp-plug.in/lltl/pphash.h>
31
32 namespace lsp
33 {
34 namespace json
35 {
36 class Array;
37 class Boolean;
38 class Double;
39 class Integer;
40 class Object;
41 class String;
42
43 /**
44 * Generalized JSON node
45 */
46 class Node
47 {
48 public:
49 enum node_type_t
50 {
51 JN_NULL,
52 JN_INT,
53 JN_DOUBLE,
54 JN_BOOL,
55 JN_STRING,
56 JN_ARRAY,
57 JN_OBJECT
58 };
59
60 protected:
61 typedef struct node_t
62 {
63 size_t refs;
64 node_type_t type;
65 union
66 {
67 ssize_t nValue;
68 double fValue;
69 bool bValue;
70 LSPString *sValue;
71 lltl::parray<node_t> *pArray;
72 lltl::pphash<LSPString, node_t> *pObject;
73 void *pData;
74 };
75 } node_t;
76
77 protected:
78 friend class Integer;
79 friend class Double;
80 friend class Boolean;
81 friend class Array;
82 friend class Object;
83
84 protected:
85 mutable node_t *pNode;
86
87 protected:
88 void copy_ref(const Node *src);
89 node_t *make_ref() const;
90 static void release_ref(node_t *node);
91 static void undef_node(node_t *node);
92 static node_t *clear_node(node_t *node);
93 static node_t *create_node(node_t *node);
94 static void move_data(node_t *dst, node_t *src);
95
96 status_t cast_to(node_type_t type);
97 status_t cast_to_int();
98 status_t cast_to_double();
99 status_t cast_to_bool();
100 status_t cast_to_string();
101 status_t cast_to_array();
102 status_t cast_to_object();
103
104 protected:
105 Node(node_t *node);
106
107 public:
108 inline Node() { pNode = NULL; }
109 Node(const Node &src);
110 Node(const Node *src);
111 virtual ~Node();
112
113 public:
114 inline Node &operator = (const Node &src) { copy_ref(&src); return *this; }
115 inline Node &operator = (const Node *src) { copy_ref(src); return *this; }
116
117 inline Node &assign(const Node &src) { copy_ref(&src); return *this; }
118 inline Node &assign(const Node *src) { copy_ref(src); return *this; }
119
120 public:
121 inline bool is_null() const { return (pNode == NULL) || (pNode->type == JN_NULL); }
122 inline bool is_int() const { return (pNode != NULL) && (pNode->type == JN_INT); }
123 inline bool is_double() const { return (pNode != NULL) && (pNode->type == JN_DOUBLE); }
124 inline bool is_bool() const { return (pNode != NULL) && (pNode->type == JN_BOOL); }
125 inline bool is_string() const { return (pNode != NULL) && (pNode->type == JN_STRING); }
126 inline bool is_object() const { return (pNode != NULL) && (pNode->type == JN_OBJECT); }
127 inline bool is_array() const { return (pNode != NULL) && (pNode->type == JN_ARRAY); }
128 inline node_type_t type() const { return (pNode != NULL) ? pNode->type : JN_NULL; }
129 const char *stype() const;
130 bool of_type(const Node *src) const;
131 bool of_type(const Node &src) const;
132 bool of_type(node_type_t type) const;
133
134 public:
135 Integer as_int() const;
136 Double as_double() const;
137 Boolean as_bool() const;
138 String as_string() const;
139 Array as_array() const;
140 Object as_object() const;
141
142 public:
143 virtual bool valid() const { return true; }
144
145 virtual status_t create();
146
147 static Node build();
148
149 static Node *allocate();
150
151 inline void undef() { undef_node(pNode); }
152 inline void set_null() { undef_node(pNode); }
153 };
154 } /* namespace json */
155 } /* namespace lsp */
156
157 #include <lsp-plug.in/fmt/json/dom/Integer.h>
158 #include <lsp-plug.in/fmt/json/dom/Double.h>
159 #include <lsp-plug.in/fmt/json/dom/Boolean.h>
160 #include <lsp-plug.in/fmt/json/dom/String.h>
161 #include <lsp-plug.in/fmt/json/dom/Array.h>
162 #include <lsp-plug.in/fmt/json/dom/Object.h>
163
164 #endif /* LSP_PLUG_IN_FMT_JSON_DOM_NODE_H_ */