libamxc  1.10.3
C Generic Data Containers
variant_double.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <sys/resource.h>
56 
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <fcntl.h>
60 #include <math.h>
61 
62 #include <amxc_variant_priv.h>
63 
64 static int variant_double_to_string(amxc_var_t* const dest,
65  const amxc_var_t* const src) {
66  int retval = -1;
67  int check = 0;
68  int size_needed = snprintf(NULL, 0, "%f", src->data.d);
69  dest->data.s = (char*) calloc(size_needed + 1, sizeof(char));
70 
71  when_null(dest->data.s, exit);
72 
73  check = snprintf(dest->data.s, size_needed + 1, "%f", src->data.d);
74  if(check < 0) {
75  free(dest->data.s);
76  dest->data.s = NULL;
77  goto exit;
78  }
79 
80  retval = 0;
81 
82 exit:
83  return retval;
84 }
85 
86 static int variant_double_to_int8(amxc_var_t* const dest,
87  const amxc_var_t* const src) {
88  int retval = -1;
89 
90  /* verify overflow or underflow */
91  when_true(src->data.d > INT8_MAX || src->data.d < INT8_MIN, exit);
92 
93  dest->data.i8 = (int8_t) src->data.d;
94  retval = 0;
95 
96 exit:
97  return retval;
98 }
99 
100 static int variant_double_to_int16(amxc_var_t* const dest,
101  const amxc_var_t* const src) {
102  int retval = -1;
103 
104  /* verify overflow or underflow */
105  when_true(src->data.d > INT16_MAX || src->data.d < INT16_MIN, exit);
106 
107  dest->data.i16 = (int16_t) src->data.d;
108  retval = 0;
109 
110 exit:
111  return retval;
112 }
113 
114 static int variant_double_to_int32(amxc_var_t* const dest,
115  const amxc_var_t* const src) {
116  int retval = -1;
117 
118  /* verify overflow or underflow */
119  when_true(src->data.d > INT32_MAX || src->data.d < INT32_MIN, exit);
120 
121  dest->data.i32 = (int32_t) src->data.d;
122  retval = 0;
123 
124 exit:
125  return retval;
126 }
127 
128 static int variant_double_to_int64(amxc_var_t* const dest,
129  const amxc_var_t* const src) {
130  int retval = -1;
131 
132  /* verify overflow or underflow */
133  when_true(src->data.d > INT64_MAX || src->data.d < INT64_MIN, exit);
134 
135  dest->data.i64 = (int64_t) src->data.d;
136  retval = 0;
137 
138 exit:
139  return retval;
140 }
141 
142 static int variant_double_to_uint8(amxc_var_t* const dest,
143  const amxc_var_t* const src) {
144  int retval = -1;
145 
146  /* verify overflow or underflow */
147  when_true(fabs(src->data.d) > UINT8_MAX, exit);
148 
149  dest->data.ui8 = (uint8_t) fabs(src->data.d);
150  retval = 0;
151 
152 exit:
153  return retval;
154 }
155 
156 static int variant_double_to_uint16(amxc_var_t* const dest,
157  const amxc_var_t* const src) {
158  int retval = -1;
159 
160  /* verify overflow or underflow */
161  when_true(fabs(src->data.d) > UINT16_MAX, exit);
162 
163  dest->data.ui16 = (uint16_t) fabs(src->data.d);
164  retval = 0;
165 
166 exit:
167  return retval;
168 }
169 
170 static int variant_double_to_uint32(amxc_var_t* const dest,
171  const amxc_var_t* const src) {
172  int retval = -1;
173 
174  /* verify overflow or underflow */
175  when_true(fabs(src->data.d) > UINT32_MAX, exit);
176 
177  dest->data.ui32 = (uint32_t) fabs(src->data.d);
178  retval = 0;
179 
180 exit:
181  return retval;
182 }
183 
184 static int variant_double_to_uint64(amxc_var_t* const dest,
185  const amxc_var_t* const src) {
186  int retval = -1;
187 
188  /* verify overflow or underflow */
189  when_true(fabs(src->data.d) > UINT64_MAX, exit);
190 
191  dest->data.ui64 = (uint64_t) fabs(src->data.d);
192  retval = 0;
193 
194 exit:
195  return retval;
196 }
197 
198 static int variant_double_to_float(amxc_var_t* const dest,
199  const amxc_var_t* const src) {
200  int retval = -1;
201 
202  dest->data.f = (float) src->data.d;
203 
204  retval = 0;
205 
206  return retval;
207 }
208 
209 static int variant_double_to_bool(amxc_var_t* const dest,
210  const amxc_var_t* const src) {
211  int retval = -1;
212 
213  dest->data.b = src->data.d == 0 ? false : true;
214 
215  retval = 0;
216 
217  return retval;
218 }
219 
220 static int variant_double_convert_to(amxc_var_t* const dest,
221  const amxc_var_t* const src) {
222  int retval = -1;
223 
240  NULL,
241  NULL,
245  };
246 
247  if(dest->type_id >= AMXC_VAR_ID_CUSTOM_BASE) {
248  goto exit;
249  }
250 
251  if(convfn[dest->type_id] != NULL) {
252  if(dest->type_id == AMXC_VAR_ID_ANY) {
254  }
255  retval = convfn[dest->type_id](dest, src);
256  }
257 
258 exit:
259  return retval;
260 }
261 
262 static int variant_double_compare(const amxc_var_t* const lval,
263  const amxc_var_t* const rval,
264  int* const result) {
265  if(lval->data.d == rval->data.d) {
266  *result = 0;
267  } else if(lval->data.d > rval->data.d) {
268  *result = 1;
269  } else {
270  *result = -1;
271  }
272  return 0;
273 }
274 
276  .init = NULL,
277  .del = NULL,
278  .copy = amxc_var_default_copy,
279  .move = amxc_var_default_move,
280  .convert_from = NULL,
281  .convert_to = variant_double_convert_to,
282  .compare = variant_double_compare,
283  .get_key = NULL,
284  .set_key = NULL,
285  .get_index = NULL,
286  .set_index = NULL,
287  .type_id = 0,
288  .hit = { .ait = NULL, .key = NULL, .next = NULL },
289  .name = AMXC_VAR_NAME_DOUBLE
290 };
291 
294 }
295 
298 }
299 
300 int amxc_var_set_double(amxc_var_t* var, double val) {
301  int retval = -1;
302  when_null(var, exit);
303 
305 
306  var->data.d = val;
307  retval = 0;
308 
309 exit:
310  return retval;
311 }
312 
314  double retval = 0;
315  amxc_var_t variant;
316 
317  when_null(var, exit);
318 
319  amxc_var_init(&variant);
321  retval = variant.data.d;
322  amxc_var_clean(&variant);
323 
324 exit:
325  return retval;
326 }
327 
329  double retval = 0;
330  when_null(var, exit);
332 
333  retval = var->data.d;
334 
335 exit:
336  return retval;
337 }
338 
340  amxc_var_t* subvar = NULL;
341 
342  when_null(var, exit);
343  subvar = amxc_var_add_new(var);
344  when_null(subvar, exit);
345 
346  if(amxc_var_set_double(subvar, val) != 0) {
347  amxc_var_delete(&subvar);
348  }
349 
350 exit:
351  return subvar;
352 }
353 
354 amxc_var_t* amxc_var_add_new_key_double(amxc_var_t* const var, const char* key, double val) {
355  amxc_var_t* subvar = NULL;
356 
357  when_null(var, exit);
358  subvar = amxc_var_add_new_key(var, key);
359  when_null(subvar, exit);
360 
361  if(amxc_var_set_double(subvar, val) != 0) {
362  amxc_var_delete(&subvar);
363  }
364 
365 exit:
366  return subvar;
367 }
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_true(x, l)
Definition: amxc_macros.h:134
#define CONSTRUCTOR
Definition: amxc_macros.h:86
#define when_null(x, l)
Definition: amxc_macros.h:126
#define DESTRUCTOR
Definition: amxc_macros.h:90
int PRIVATE amxc_var_default_convert_to_null(amxc_var_t *const dest, const amxc_var_t *const src)
int PRIVATE amxc_var_default_move(amxc_var_t *const dest, amxc_var_t *const src)
Definition: amxc_variant.c:142
uint32_t PRIVATE amxc_var_add_type(amxc_var_type_t *const type, const uint32_t index)
int PRIVATE amxc_var_default_convert_to_htable(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:174
int PRIVATE amxc_var_default_copy(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:136
int PRIVATE amxc_var_remove_type(amxc_var_type_t *const type)
int PRIVATE amxc_var_default_convert_to_list(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:155
int amxc_var_set_double(amxc_var_t *var, double val)
Setter helper function.
double amxc_var_get_double(const amxc_var_t *var)
Conversion helper function.
amxc_var_t * amxc_var_add_new_double(amxc_var_t *const var, double val)
Conversion helper function.
amxc_var_t * amxc_var_add_new_key_double(amxc_var_t *const var, const char *key, double val)
Conversion helper function.
double amxc_var_get_const_double(const amxc_var_t *const var)
Conversion helper function.
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257
#define AMXC_VAR_ID_DOUBLE
Double variant id.
Definition: amxc_variant.h:194
#define AMXC_VAR_NAME_DOUBLE
Provides a name for variant id AMXC_VAR_ID_DOUBLE.
Definition: amxc_variant.h:336
#define AMXC_VAR_ID_ANY
Special variant id, typically used in cast or conversion functions.
Definition: amxc_variant.h:247
int(* amxc_var_convert_fn_t)(amxc_var_t *const dest, const amxc_var_t *const src)
Variant type callback function prototype for dynamically converting one type to another.
int amxc_var_set_type(amxc_var_t *const var, const uint32_t type)
Change the variant data type.
Definition: amxc_variant.c:261
int amxc_var_init(amxc_var_t *const var)
Initializes a variant.
Definition: amxc_variant.c:223
amxc_var_t * amxc_var_add_new(amxc_var_t *const var)
Adds a new variant to a composite variant.
Definition: amxc_variant.c:551
void amxc_var_clean(amxc_var_t *const var)
Clean-up and reset variant.
Definition: amxc_variant.c:237
void amxc_var_delete(amxc_var_t **var)
Frees the previously allocated variant.
Definition: amxc_variant.c:207
amxc_var_t * amxc_var_add_new_key(amxc_var_t *const var, const char *key)
Adds a new variant with a key to a composite variant.
Definition: amxc_variant.c:526
int amxc_var_convert(amxc_var_t *const dest, const amxc_var_t *src, const uint32_t type_id)
Converts one variant (source) to another variant(destination) using the specified variant type id.
Definition: amxc_variant.c:333
A variant type structure.
amxc_var_new_fn_t init
The variant struct definition.
Definition: amxc_variant.h:861
void * data
Definition: amxc_variant.h:883
uint32_t type_id
Definition: amxc_variant.h:864
static amxc_var_t * var
Definition: test_issue_58.c:77
static amxc_var_type_t amxc_variant_double
static int variant_double_to_uint16(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_to_uint8(amxc_var_t *const dest, const amxc_var_t *const src)
static CONSTRUCTOR void amxc_var_double_init(void)
static int variant_double_to_string(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_to_uint64(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_to_int16(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_to_uint32(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_to_float(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_convert_to(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_to_int32(amxc_var_t *const dest, const amxc_var_t *const src)
static DESTRUCTOR void amxc_var_double_cleanup(void)
static int variant_double_to_bool(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_to_int8(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_to_int64(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_double_compare(const amxc_var_t *const lval, const amxc_var_t *const rval, int *const result)