Skip to content

Commit 1e9d328

Browse files
committed
Added possibility to perform casted fetch from expr::value_t
1 parent c9ec836 commit 1e9d328

File tree

3 files changed

+185
-5
lines changed

3 files changed

+185
-5
lines changed

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*******************************************************************************
44

55
=== 1.0.30 ===
6-
6+
* Added possibility to perform casted fetch from expr::value_t.
77

88
=== 1.0.29 ===
99
* Implemented simple compressor for Wavefront OBJ files.

include/lsp-plug.in/expr/types.h

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
2+
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
44
*
55
* This file is part of lsp-runtime-lib
66
* Created on: 16 сент. 2019 г.
@@ -141,11 +141,86 @@ namespace lsp
141141
*/
142142
void destroy_value(value_t *value);
143143

144+
/**
145+
* Ensure that value is integer and read it
146+
* @param dst pointer to store value
147+
* @param v value to read
148+
* @return status STATUS_BAD_TYPE error code on type mismatch
149+
*/
144150
status_t fetch_int(ssize_t *dst, const value_t *v);
151+
152+
/**
153+
* Ensure that value is floating-point and read it
154+
* @param dst pointer to store value
155+
* @param v value to read
156+
* @return status STATUS_BAD_TYPE error code on type mismatch
157+
*/
145158
status_t fetch_float(float *dst, const value_t *v);
159+
160+
/**
161+
* Ensure that value is floating-point and read it
162+
* @param dst pointer to store value
163+
* @param v value to read
164+
* @return status STATUS_BAD_TYPE error code on type mismatch
165+
*/
166+
status_t fetch_float(double *dst, const value_t *v);
167+
168+
/**
169+
* Ensure that value is boolean and read it
170+
* @param dst pointer to store value
171+
* @param v value to read
172+
* @return status STATUS_BAD_TYPE error code on type mismatch
173+
*/
146174
status_t fetch_bool(bool *dst, const value_t *v);
175+
176+
/**
177+
* Ensure that value is string and read it
178+
* @param dst pointer to store value
179+
* @param v value to read
180+
* @return status STATUS_BAD_TYPE error code on type mismatch
181+
*/
147182
status_t fetch_string(LSPString *dst, const value_t *v);
148183

184+
/**
185+
* Try to cast value to int and fetch it
186+
* @param dst pointer to store value
187+
* @param v value to read
188+
* @return status of operation
189+
*/
190+
status_t fetch_as_int(ssize_t *dst, const value_t *v);
191+
192+
/**
193+
* Try to cast value to floating-pont value and fetch it
194+
* @param dst pointer to store value
195+
* @param v value to read
196+
* @return status of operation
197+
*/
198+
status_t fetch_as_float(float *dst, const value_t *v);
199+
200+
/**
201+
* Try to cast value to floating-pont value and fetch it
202+
* @param dst pointer to store value
203+
* @param v value to read
204+
* @return status of operation
205+
*/
206+
status_t fetch_as_float(double *dst, const value_t *v);
207+
208+
/**
209+
* Try to cast value to boolean value and fetch it
210+
* @param dst pointer to store value
211+
* @param v value to read
212+
* @return status of operation
213+
*/
214+
status_t fetch_as_bool(bool *dst, const value_t *v);
215+
216+
/**
217+
* Try to cast value to string and fetch it
218+
* @param dst pointer to store value
219+
* @param v value to read
220+
* @return status of operation
221+
*/
222+
status_t fetch_as_string(LSPString *dst, const value_t *v);
223+
149224
status_t cast_value(value_t *v, value_type_t type);
150225
status_t cast_value(value_t *dst, const value_t *v, value_type_t type);
151226

src/main/expr/types.cpp

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2023 Vladimir Sadovnikov <sadko4u@gmail.com>
2+
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
44
*
55
* This file is part of lsp-runtime-lib
66
* Created on: 19 сент. 2019 г.
@@ -210,6 +210,14 @@ namespace lsp
210210
return STATUS_OK;
211211
}
212212

213+
status_t fetch_float(double *dst, const value_t *v)
214+
{
215+
if (v->type != VT_FLOAT)
216+
return STATUS_BAD_TYPE;
217+
*dst = v->v_float;
218+
return STATUS_OK;
219+
}
220+
213221
status_t fetch_bool(bool *dst, const value_t *v)
214222
{
215223
if (v->type != VT_BOOL)
@@ -225,6 +233,103 @@ namespace lsp
225233
return (dst->set(v->v_str)) ? STATUS_OK : STATUS_NO_MEM;
226234
}
227235

236+
status_t fetch_as_int(ssize_t *dst, const value_t *v)
237+
{
238+
if (v->type == VT_INT)
239+
{
240+
*dst = v->v_int;
241+
return STATUS_OK;
242+
}
243+
244+
// Perform cast
245+
value_t tmp;
246+
init_value(&tmp);
247+
lsp_finally { destroy_value(&tmp); };
248+
249+
status_t res = cast_int(&tmp, v);
250+
if (res == STATUS_OK)
251+
*dst = tmp.v_int;
252+
253+
return res;
254+
}
255+
256+
status_t fetch_as_float(float *dst, const value_t *v)
257+
{
258+
if (v->type == VT_FLOAT)
259+
{
260+
*dst = v->v_float;
261+
return STATUS_OK;
262+
}
263+
264+
// Perform cast
265+
value_t tmp;
266+
init_value(&tmp);
267+
lsp_finally { destroy_value(&tmp); };
268+
269+
status_t res = cast_int(&tmp, v);
270+
if (res == STATUS_OK)
271+
*dst = tmp.v_float;
272+
273+
return res;
274+
}
275+
276+
status_t fetch_as_float(double *dst, const value_t *v)
277+
{
278+
if (v->type == VT_FLOAT)
279+
{
280+
*dst = v->v_float;
281+
return STATUS_OK;
282+
}
283+
284+
// Perform cast
285+
value_t tmp;
286+
init_value(&tmp);
287+
lsp_finally { destroy_value(&tmp); };
288+
289+
status_t res = cast_int(&tmp, v);
290+
if (res == STATUS_OK)
291+
*dst = tmp.v_float;
292+
293+
return res;
294+
}
295+
296+
status_t fetch_as_bool(bool *dst, const value_t *v)
297+
{
298+
if (v->type == VT_BOOL)
299+
{
300+
*dst = v->v_bool;
301+
return STATUS_OK;
302+
}
303+
304+
// Perform cast
305+
value_t tmp;
306+
init_value(&tmp);
307+
lsp_finally { destroy_value(&tmp); };
308+
309+
status_t res = cast_int(&tmp, v);
310+
if (res == STATUS_OK)
311+
*dst = tmp.v_bool;
312+
313+
return res;
314+
}
315+
316+
status_t fetch_as_string(LSPString *dst, const value_t *v)
317+
{
318+
if (v->type == VT_STRING)
319+
return (dst->set(v->v_str)) ? STATUS_OK : STATUS_NO_MEM;
320+
321+
// Perform cast
322+
value_t tmp;
323+
init_value(&tmp);
324+
lsp_finally { destroy_value(&tmp); };
325+
326+
status_t res = cast_int(&tmp, v);
327+
if (res == STATUS_OK)
328+
dst->swap(v->v_str);
329+
330+
return res;
331+
}
332+
228333
status_t cast_value(value_t *v, value_type_t type)
229334
{
230335
switch (type)

0 commit comments

Comments
 (0)