Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2021 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/Double.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 Double::valid() const
32 {
33 return is_double();
34 }
35
36 status_t Double::create()
37 {
38 return Double::create(0.0);
39 }
40
41 status_t Double::create(double 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_DOUBLE;
49 node->fValue = value;
50
51 release_ref(pNode);
52 pNode = node;
53
54 return STATUS_OK;
55 }
56
57 Double *Double::allocate(double value)
58 {
59 Double *res = new Double();
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 Double Double::build(double value)
70 {
71 Double res;
72 res.create(value);
73 return res;
74 }
75
76 double Double::get() const
77 {
78 if (pNode == NULL)
79 return 0.0;
80
81 switch (pNode->type)
82 {
83 case JN_INT: return pNode->nValue;
84 case JN_DOUBLE: return pNode->fValue;
85 case JN_BOOL: return (pNode->bValue) ? 1.0 : 0.0;
86 case JN_STRING: {
87 io::InStringSequence is(pNode->sValue);
88 expr::Tokenizer t(&is);
89 double fvalue;
90
91 switch (t.get_token(expr::TF_GET))
92 {
93 case expr::TT_IVALUE: fvalue = t.int_value(); break;
94 case expr::TT_FVALUE: fvalue = t.float_value(); break;
95 case expr::TT_TRUE: fvalue = 1.0; break;
96 case expr::TT_FALSE: fvalue = 0.0; break;
97 default:
98 return 0.0;
99 }
100
101 if (t.get_token(expr::TF_GET) != expr::TT_EOF)
102 return 0.0;
103
104 return fvalue;
105 }
106
107 default:
108 break;
109 }
110
111 return 0.0;
112 }
113
114 double Double::sget() const
115 {
116 return (is_double()) ? pNode->fValue : 0;
117 }
118
119 status_t Double::cast()
120 {
121 return cast_to_int();
122 }
123
124 status_t Double::set(double 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_DOUBLE;
132 pNode->fValue = value;
133
134 return STATUS_OK;
135 }
136
137 status_t Double::sset(double value)
138 {
139 if (!is_double())
140 return STATUS_BAD_TYPE;
141
142 pNode->fValue = value;
143 return STATUS_OK;
144 }
145
146 } /* namespace json */
147 } /* namespace lsp */
148
149
150