Debian Codegraph @

modules/lsp-runtime-lib/src/main/expr/Variables.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 19 сент. 2019 г.
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/expr/types.h>
23 #include <lsp-plug.in/expr/Variables.h>
24 #include <lsp-plug.in/common/debug.h>
25
26 namespace lsp
27 {
28 namespace expr
29 {
30 Variables::Variables()
31 {
32 pResolver = NULL;
33 }
34
35 Variables::Variables(Resolver *r)
36 {
37 pResolver = r;
38 }
39
40 Variables::~Variables()
41 {
42 clear();
43 }
44
45 status_t Variables::set_int(const char *name, ssize_t value)
46 {
47 if (name == NULL)
48 return STATUS_BAD_ARGUMENTS;
49 LSPString key;
50 if (!key.set_utf8(name))
51 return STATUS_NO_MEM;
52
53 value_t v;
54 v.type = VT_INT;
55 v.v_int = value;
56 return set(&key, &v);
57 }
58
59 status_t Variables::set_float(const char *name, double value)
60 {
61 if (name == NULL)
62 return STATUS_BAD_ARGUMENTS;
63 LSPString key;
64 if (!key.set_utf8(name))
65 return STATUS_NO_MEM;
66
67 value_t v;
68 v.type = VT_FLOAT;
69 v.v_float = value;
70 return set(&key, &v);
71 }
72
73 status_t Variables::set_bool(const char *name, bool value)
74 {
75 if (name == NULL)
76 return STATUS_BAD_ARGUMENTS;
77 LSPString key;
78 if (!key.set_utf8(name))
79 return STATUS_NO_MEM;
80
81 value_t v;
82 v.type = VT_BOOL;
83 v.v_bool = value;
84 return set(&key, &v);
85 }
86
87 status_t Variables::set_string(const char *name, const char *value, const char *charset)
88 {
89 if (name == NULL)
90 return STATUS_BAD_ARGUMENTS;
91
92 LSPString key;
93 if (!key.set_utf8(name))
94 return STATUS_NO_MEM;
95
96 value_t v;
97 if (value == NULL)
98 {
99 v.type = VT_NULL;
100 v.v_str = NULL;
101 return set(&key, &v);
102 }
103
104 status_t res = init_value_string(&v, value, charset);
105 if (res == STATUS_OK)
106 res = set_move(&key, &v);
107
108 destroy_value(&v);
109 return res;
110 }
111
112 status_t Variables::set_string(const char *name, const LSPString *value)
113 {
114 if (name == NULL)
115 return STATUS_BAD_ARGUMENTS;
116
117 LSPString key;
118 if (!key.set_utf8(name))
119 return STATUS_NO_MEM;
120
121 value_t v;
122 if (value == NULL)
123 {
124 v.type = VT_NULL;
125 v.v_str = NULL;
126 return set(&key, &v);
127 }
128
129 status_t res = init_value_string(&v, value);
130 if (res == STATUS_OK)
131 res = set_move(&key, &v);
132
133 destroy_value(&v);
134 return res;
135 }
136
137 status_t Variables::set_null(const char *name)
138 {
139 if (name == NULL)
140 return STATUS_BAD_ARGUMENTS;
141 LSPString key;
142 if (!key.set_utf8(name))
143 return STATUS_NO_MEM;
144
145 value_t v;
146 v.type = VT_NULL;
147 v.v_int = 0;
148 return set(&key, &v);
149 }
150
151 status_t Variables::set_int(const LSPString *name, ssize_t value)
152 {
153 value_t v;
154 v.type = VT_INT;
155 v.v_int = value;
156 return set(name, &v);
157 }
158
159 status_t Variables::set_float(const LSPString *name, double value)
160 {
161 value_t v;
162 v.type = VT_FLOAT;
163 v.v_float = value;
164 return set(name, &v);
165 }
166
167 status_t Variables::set_bool(const LSPString *name, bool value)
168 {
169 value_t v;
170 v.type = VT_BOOL;
171 v.v_bool = value;
172 return set(name, &v);
173 }
174
175 status_t Variables::set_string(const LSPString *name, const char *value, const char *charset)
176 {
177 value_t v;
178
179 if (value == NULL)
180 {
181 v.type = VT_NULL;
182 v.v_str = NULL;
183 return set(name, &v);
184 }
185
186 status_t res = init_value_string(&v, value, charset);
187 if (res == STATUS_OK)
188 res = set_move(name, &v);
189
190 destroy_value(&v);
191 return res;
192 }
193
194 status_t Variables::set_string(const LSPString *name, const LSPString *value)
195 {
196 value_t v;
197 v.type = VT_STRING;
198 v.v_str = const_cast<LSPString *>(value);
199
200 return set(name, &v);
201 }
202
203 status_t Variables::set_null(const LSPString *name)
204 {
205 value_t v;
206 v.type = VT_NULL;
207 v.v_int = 0;
208 return set(name, &v);
209 }
210
211 status_t Variables::set(const char *name, const value_t *value)
212 {
213 if (name == NULL)
214 return STATUS_BAD_ARGUMENTS;
215 LSPString key;
216 if (!key.set_utf8(name))
217 return STATUS_NO_MEM;
218
219 return set(&key, value);
220 }
221
222 status_t Variables::unset(const char *name, value_t *value)
223 {
224 if (name == NULL)
225 return STATUS_BAD_ARGUMENTS;
226 LSPString key;
227 if (!key.set_utf8(name))
228 return STATUS_NO_MEM;
229
230 return unset(&key, value);
231 }
232
233 status_t Variables::resolve(value_t *value, const char *name, size_t num_indexes, const ssize_t *indexes)
234 {
235 if (name == NULL)
236 return STATUS_BAD_ARGUMENTS;
237
238 LSPString key;
239 if (!key.set_utf8(name))
240 return STATUS_NO_MEM;
241
242 return resolve(value, &key, num_indexes, indexes);
243 }
244
245 status_t Variables::resolve(value_t *value, const LSPString *name, size_t num_indexes, const ssize_t *indexes)
246 {
247 // Need to form indexes?
248 LSPString tmp;
249 const LSPString *search;
250
251 if (num_indexes > 0)
252 {
253 if (!tmp.set(name))
254 return STATUS_NO_MEM;
255 for (size_t i=0; i<num_indexes; ++i)
256 {
257 if (!tmp.fmt_append_ascii("_%ld", long(indexes[i])))
258 return STATUS_NO_MEM;
259 }
260 search = &tmp;
261 }
262 else
263 search = name;
264
265 // Lookup the cache
266 ssize_t idx = index_of_var(search);
267 if (idx >= 0)
268 {
269 variable_t *var = vVars.uget(idx);
270 int cmp = search->compare_to(&var->name);
271 if (cmp == 0)
272 {
273 if (value != NULL)
274 return copy_value(value, &var->value);
275 return STATUS_OK;
276 }
277 else if (cmp > 0)
278 ++idx;
279 }
280 else
281 idx = 0;
282
283 // No Resolver?
284 if (pResolver == NULL)
285 return STATUS_NOT_FOUND;
286
287 // Resolve from underlying resolver
288 value_t v;
289 init_value(&v);
290 lsp_finally { destroy_value(&v); };
291 status_t res = pResolver->resolve(&v, name, num_indexes, indexes);
292 if (res != STATUS_OK)
293 return res;
294
295 // Save variable to cache
296 res = insert_var(search, &v, idx);
297 if ((res == STATUS_OK) && (value != NULL))
298 res = copy_value(value, &v);
299
300 return res;
301 }
302
303 status_t Variables::call(value_t *value, const char *name, size_t num_args, const value_t *args)
304 {
305 if (name == NULL)
306 return STATUS_BAD_ARGUMENTS;
307
308 LSPString key;
309 if (!key.set_utf8(name))
310 return STATUS_NO_MEM;
311
312 return call(value, &key, num_args, args);
313 }
314
315 status_t Variables::call(value_t *value, const LSPString *name, size_t num_args, const value_t *args)
316 {
317 // Lookup the cache
318 ssize_t idx = index_of_func(name);
319 if (idx < 0)
320 {
321 if (pResolver != NULL)
322 return pResolver->call(value, name, num_args, args);
323
324 return Resolver::call(value, name, num_args, args);
325 }
326
327 // Ensure than name of function matches
328 user_func_t *func = vFunc.uget(idx);
329 if (!name->equals(&func->name))
330 {
331 if (pResolver != NULL)
332 return pResolver->call(value, name, num_args, args);
333
334 return Resolver::call(value, name, num_args, args);
335 }
336
337 // Execute function
338 return func->func(func->context, value, num_args, args);
339 }
340
341 ssize_t Variables::index_of_var(const LSPString *name)
342 {
343 const variable_t *var;
344 ssize_t first = 0, last = vVars.size() - 1, mid;
345 if (last < 0)
346 return -1;
347
348 while (first < last)
349 {
350 mid = (first + last) >> 1;
351 var = vVars.uget(mid);
352 int cmp = name->compare_to(&var->name);
353
354 if (cmp < 0)
355 last = mid - 1;
356 else if (cmp > 0)
357 first = mid + 1;
358 else
359 return mid;
360 }
361
362 return first;
363 }
364
365 ssize_t Variables::index_of_func(const LSPString *name)
366 {
367 const user_func_t *f;
368 ssize_t first = 0, last = vFunc.size() - 1, mid;
369 if (last < 0)
370 return -1;
371
372 while (first < last)
373 {
374 mid = (first + last) >> 1;
375 f = vFunc.uget(mid);
376 int cmp = name->compare_to(&f->name);
377
378 if (cmp < 0)
379 last = mid - 1;
380 else if (cmp > 0)
381 first = mid + 1;
382 else
383 return mid;
384 }
385
386 return first;
387 }
388
389 status_t Variables::insert_var(const LSPString *name, const value_t *value, size_t idx)
390 {
391 variable_t *var = new variable_t;
392 if (var == NULL)
393 return STATUS_NO_MEM;
394 if (!var->name.set(name))
395 {
396 delete var;
397 return STATUS_NO_MEM;
398 }
399
400 status_t res = init_value(&var->value, value);
401 if (res == STATUS_OK)
402 res = (vVars.insert(idx, var)) ? STATUS_OK : STATUS_NO_MEM;
403 if (res == STATUS_OK)
404 return res;
405
406 destroy_value(&var->value);
407 delete var;
408 return res;
409 }
410
411 status_t Variables::insert_var_move(const LSPString *name, value_t *value, size_t idx)
412 {
413 variable_t *var = new variable_t;
414 if (var == NULL)
415 return STATUS_NO_MEM;
416 if (!var->name.set(name))
417 {
418 delete var;
419 return STATUS_NO_MEM;
420 }
421
422 init_value_move(&var->value, value);
423 status_t res = (vVars.insert(idx, var)) ? STATUS_OK : STATUS_NO_MEM;
424 if (res == STATUS_OK)
425 return res;
426
427 destroy_value(&var->value);
428 delete var;
429 return res;
430 }
431
432 status_t Variables::insert_func(const LSPString *name, function_t func, void *context, size_t idx)
433 {
434 user_func_t *f = new user_func_t;
435 if (f == NULL)
436 return STATUS_NO_MEM;
437 if (!f->name.set(name))
438 {
439 delete f;
440 return STATUS_NO_MEM;
441 }
442
443 f->func = func;
444 f->context = context;
445
446 if (vFunc.insert(idx, f))
447 return STATUS_OK;
448
449 delete f;
450 return STATUS_NO_MEM;
451 }
452
453 status_t Variables::set(const LSPString *name, const value_t *value)
454 {
455 if (name == NULL)
456 return STATUS_BAD_ARGUMENTS;
457
458 ssize_t idx = index_of_var(name);
459 if (idx >= 0)
460 {
461 variable_t *var = vVars.uget(idx);
462 int cmp = name->compare_to(&var->name);
463 if (cmp == 0)
464 return copy_value(&var->value, value);
465 else if (cmp > 0)
466 ++idx;
467 }
468 else
469 idx = 0;
470
471 // Add non-existing value
472 return insert_var(name, value, idx);
473 }
474
475 status_t Variables::set_move(const LSPString *name, value_t *value)
476 {
477 if (name == NULL)
478 return STATUS_BAD_ARGUMENTS;
479
480 ssize_t idx = index_of_var(name);
481 if (idx >= 0)
482 {
483 variable_t *var = vVars.uget(idx);
484 int cmp = name->compare_to(&var->name);
485 if (cmp == 0)
486 {
487 move_value(&var->value, value);
488 return STATUS_OK;
489 }
490 else if (cmp > 0)
491 ++idx;
492 }
493 else
494 idx = 0;
495
496 // Add non-existing value
497 return insert_var_move(name, value, idx);
498 }
499
500 status_t Variables::unset(const LSPString *name, value_t *value)
501 {
502 if (name == NULL)
503 return STATUS_BAD_ARGUMENTS;
504
505 // Lookup for variable
506 ssize_t idx = index_of_var(name);
507 if (idx < 0)
508 return STATUS_NOT_FOUND;
509
510 variable_t *var = vVars.uget(idx);
511 if (!name->equals(&var->name))
512 return STATUS_NOT_FOUND;
513
514 // Need to copy value?
515 if (value != NULL)
516 {
517 status_t res = copy_value(value, &var->value);
518 if (res != STATUS_OK)
519 return res;
520 }
521
522 // Remove stored value
523 vVars.remove(idx);
524 destroy_value(&var->value);
525 delete var;
526
527 return STATUS_OK;
528 }
529
530 void Variables::clear_vars()
531 {
532 for (size_t i=0, n=vVars.size(); i<n; ++i)
533 {
534 variable_t *var = vVars.uget(i);
535 if (var != NULL)
536 {
537 destroy_value(&var->value);
538 delete var;
539 }
540 }
541 vVars.flush();
542 }
543
544 void Variables::clear_func()
545 {
546 for (size_t i=0, n=vFunc.size(); i<n; ++i)
547 {
548 user_func_t *func = vFunc.uget(i);
549 if (func != NULL)
550 delete func;
551 }
552 vFunc.flush();
553 }
554
555 void Variables::clear()
556 {
557 clear_vars();
558 clear_func();
559 }
560
561 status_t Variables::bind_func(const char *name, function_t func, void *context)
562 {
563 if (name == NULL)
564 return STATUS_BAD_ARGUMENTS;
565 LSPString key;
566 if (!key.set_utf8(name))
567 return STATUS_NO_MEM;
568
569 return bind_func(&key, func, context);
570 }
571
572 status_t Variables::bind_func(const LSPString *name, function_t func, void *context)
573 {
574 if (name == NULL)
575 return STATUS_BAD_ARGUMENTS;
576
577 ssize_t idx = index_of_func(name);
578 if (idx >= 0)
579 {
580 user_func_t *f = vFunc.uget(idx);
581 int cmp = name->compare_to(&f->name);
582 if (cmp == 0)
583 {
584 if (!f->name.set(name))
585 return STATUS_NO_MEM;
586 f->func = func;
587 f->context = context;
588 }
589 else if (cmp > 0)
590 ++idx;
591 }
592 else
593 idx = 0;
594
595 // Add non-existing value
596 return insert_func(name, func, context, idx);
597 }
598
599 status_t Variables::unbind_func(const char *name)
600 {
601 if (name == NULL)
602 return STATUS_BAD_ARGUMENTS;
603 LSPString key;
604 if (!key.set_utf8(name))
605 return STATUS_NO_MEM;
606
607 return unbind_func(&key);
608 }
609
610 status_t Variables::unbind_func(const LSPString *name)
611 {
612 if (name == NULL)
613 return STATUS_BAD_ARGUMENTS;
614
615 // Lookup for data
616 ssize_t idx = index_of_func(name);
617 if (idx < 0)
618 return STATUS_NOT_FOUND;
619
620 user_func_t *func = vFunc.uget(idx);
621 if (!name->equals(&func->name))
622 return STATUS_NOT_FOUND;
623
624 // Remove function
625 vFunc.remove(idx);
626 delete func;
627
628 return STATUS_OK;
629 }
630
631 } /* namespace expr */
632 } /* namespace lsp */