Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2024 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 16 сент. 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/Resolver.h>
23 #include <lsp-plug.in/expr/functions.h>
24 #include <lsp-plug.in/stdlib/string.h>
25
26 namespace lsp
27 {
28 namespace expr
29 {
30 typedef struct builtin_func_t
31 {
32 const char *name;
33 const stdfunc_t func;
34 } builtin_func_t;
35
36 static const builtin_func_t builtin_functions[] =
37 {
38 { "avg", stdfunc_avg },
39 { "max", stdfunc_max },
40 { "min", stdfunc_min },
41 { "rms", stdfunc_rms },
42 };
43
44 stdfunc_t Resolver::find_std_func(const char *name)
45 {
46 ssize_t first = 0, last = sizeof(builtin_functions)/sizeof(builtin_func_t);
47 while (first <= last)
48 {
49 ssize_t mid = (first + last) >> 1;
50 const builtin_func_t *f = &builtin_functions[mid];
51
52 int res = strcmp(name, f->name);
53 if (res == 0)
54 return f->func;
55 else if (res < 0)
56 last = mid - 1;
57 else // if (res < 0)
58 first = mid + 1;
59 }
60
61 return NULL;
62 }
63
64 Resolver::Resolver()
65 {
66 }
67
68 Resolver::~Resolver()
69 {
70 }
71
72 status_t Resolver::resolve(value_t *value, const char *name, size_t num_indexes, const ssize_t *indexes)
73 {
74 set_value_null(value);
75 return STATUS_OK;
76 }
77
78 status_t Resolver::resolve(value_t *value, const LSPString *name, size_t num_indexes, const ssize_t *indexes)
79 {
80 return resolve(value, name->get_utf8(), num_indexes, indexes);
81 }
82
83 status_t Resolver::call(value_t *value, const char *name, size_t num_args, const value_t *args)
84 {
85 stdfunc_t func = find_std_func(name);
86 if (func != NULL)
87 return func(value, num_args, args);
88
89 return STATUS_NOT_FOUND;
90 }
91
92 status_t Resolver::call(value_t *value, const LSPString *name, size_t num_args, const value_t *args)
93 {
94 return Resolver::call(value, name->get_utf8(), num_args, args);
95 }
96
97 } /* namespace expr */
98 } /* namespace lsp */