Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/json/dom/Boolean.cpp

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: 13 февр. 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 #include <lsp-plug.in/fmt/json/dom/Boolean.h>
23 #include <lsp-plug.in/io/InStringSequence.h>
24 #include <lsp-plug.in/expr/Tokenizer.h>
25 #include <lsp-plug.in/expr/types.h>
26
27 namespace lsp
28 {
29 namespace json
30 {
31 bool Boolean::valid() const
32 {
33 return is_bool();
34 }
35
36 status_t Boolean::create()
37 {
38 return Boolean::create(false);
39 }
40
41 status_t Boolean::create(bool value)
42 {
43 node_t *node = new node_t();
44 if (node == NULL)
45 return STATUS_NO_MEM;
46
47 node->refs = 1;
48 node->type = JN_BOOL;
49 node->bValue = value;
50
51 release_ref(pNode);
52 pNode = node;
53
54 return STATUS_OK;
55 }
56
57 Boolean *Boolean::allocate(bool value)
58 {
59 Boolean *res = new Boolean();
60 if (res == NULL)
61 return NULL;
62 else if (res->create(value) == STATUS_OK)
63 return res;
64
65 delete res;
66 return NULL;
67 }
68
69 Boolean Boolean::build(bool value)
70 {
71 Boolean res;
72 res.create(value);
73 return res;
74 }
75
76 bool Boolean::get() const
77 {
78 if (pNode == NULL)
79 return false;
80
81 switch (pNode->type)
82 {
83 case JN_INT: return pNode->nValue != 0;
84 case JN_DOUBLE: return pNode->fValue != 0.0f;
85 case JN_BOOL: return pNode->bValue;
86 case JN_STRING: {
87 io::InStringSequence is(pNode->sValue);
88 expr::Tokenizer t(&is);
89 bool bvalue;
90
91 switch (t.get_token(expr::TF_GET))
92 {
93 case expr::TT_IVALUE: bvalue = t.int_value() != 0; break;
94 case expr::TT_FVALUE: bvalue = t.float_value() != 0.0f; break;
95 case expr::TT_TRUE: bvalue = true; break;
96 case expr::TT_FALSE: bvalue = false; break;
97 default:
98 return false;
99 }
100
101 if (t.get_token(expr::TF_GET) != expr::TT_EOF)
102 return false;
103
104 return bvalue;
105 }
106
107 default:
108 break;
109 }
110
111 return false;
112 }
113
114 bool Boolean::sget() const
115 {
116 return (is_bool()) ? pNode->bValue : false;
117 }
118
119 status_t Boolean::cast()
120 {
121 return cast_to_int();
122 }
123
124 status_t Boolean::set(bool value)
125 {
126 node_t *node = clear_node(pNode);
127 if (node == NULL)
128 return STATUS_NO_MEM;
129
130 pNode = node;
131 pNode->type = JN_BOOL;
132 pNode->bValue = value;
133
134 return STATUS_OK;
135 }
136
137 status_t Boolean::sset(bool value)
138 {
139 if (!is_bool())
140 return STATUS_BAD_TYPE;
141
142 pNode->bValue = value;
143 return STATUS_OK;
144 }
145
146 } /* namespace json */
147 } /* namespace lsp */
148
149