xref: /freebsd/contrib/libcbor/src/cbor/floats_ctrls.h (revision 093cf790569775b80662926efea6d9d3464bde94)
1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #ifndef LIBCBOR_FLOATS_CTRLS_H
9 #define LIBCBOR_FLOATS_CTRLS_H
10 
11 #include "cbor/cbor_export.h"
12 #include "cbor/common.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /*
19  * ============================================================================
20  * Float manipulation
21  * ============================================================================
22  */
23 
24 /** Is this a ctrl value?
25  *
26  * @param item[borrow] A float or ctrl item
27  * @return Is this a ctrl value?
28  */
29 CBOR_EXPORT bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item);
30 
31 /** Get the float width
32  *
33  * @param item[borrow] A float or ctrl item
34  * @return The width.
35  */
36 CBOR_EXPORT cbor_float_width cbor_float_get_width(const cbor_item_t *item);
37 
38 /** Get a half precision float
39  *
40  * The item must have the corresponding width
41  *
42  * @param[borrow] A half precision float
43  * @return half precision value
44  */
45 CBOR_EXPORT float cbor_float_get_float2(const cbor_item_t *item);
46 
47 /** Get a single precision float
48  *
49  * The item must have the corresponding width
50  *
51  * @param[borrow] A signle precision float
52  * @return single precision value
53  */
54 CBOR_EXPORT float cbor_float_get_float4(const cbor_item_t *item);
55 
56 /** Get a double precision float
57  *
58  * The item must have the corresponding width
59  *
60  * @param[borrow] A double precision float
61  * @return double precision value
62  */
63 CBOR_EXPORT double cbor_float_get_float8(const cbor_item_t *item);
64 
65 /** Get the float value represented as double
66  *
67  * Can be used regardless of the width.
68  *
69  * @param[borrow] Any float
70  * @return double precision value
71  */
72 CBOR_EXPORT double cbor_float_get_float(const cbor_item_t *item);
73 
74 /** Get value from a boolean ctrl item
75  *
76  * @param item[borrow] A ctrl item
77  * @return boolean value
78  */
79 CBOR_EXPORT bool cbor_get_bool(const cbor_item_t *item);
80 
81 /** Constructs a new ctrl item
82  *
83  * The width cannot be changed once the item is created
84  *
85  * @return **new** 1B ctrl or `NULL` upon memory allocation failure
86  */
87 CBOR_EXPORT cbor_item_t *cbor_new_ctrl();
88 
89 /** Constructs a new float item
90  *
91  * The width cannot be changed once the item is created
92  *
93  * @return **new** 2B float or `NULL` upon memory allocation failure
94  */
95 CBOR_EXPORT cbor_item_t *cbor_new_float2();
96 
97 /** Constructs a new float item
98  *
99  * The width cannot be changed once the item is created
100  *
101  * @return **new** 4B float or `NULL` upon memory allocation failure
102  */
103 CBOR_EXPORT cbor_item_t *cbor_new_float4();
104 
105 /** Constructs a new float item
106  *
107  * The width cannot be changed once the item is created
108  *
109  * @return **new** 8B float or `NULL` upon memory allocation failure
110  */
111 CBOR_EXPORT cbor_item_t *cbor_new_float8();
112 
113 /** Constructs new null ctrl item
114  *
115  * @return **new** null ctrl item or `NULL` upon memory allocation failure
116  */
117 CBOR_EXPORT cbor_item_t *cbor_new_null();
118 
119 /** Constructs new undef ctrl item
120  *
121  * @return **new** undef ctrl item or `NULL` upon memory allocation failure
122  */
123 CBOR_EXPORT cbor_item_t *cbor_new_undef();
124 
125 /** Constructs new boolean ctrl item
126  *
127  * @param value The value to use
128  * @return **new** boolen ctrl item or `NULL` upon memory allocation failure
129  */
130 CBOR_EXPORT cbor_item_t *cbor_build_bool(bool value);
131 
132 /** Assign a control value
133  *
134  * \rst
135  * .. warning:: It is possible to produce an invalid CBOR value by assigning a
136  *  invalid value using this mechanism. Please consult the standard before use.
137  * \endrst
138  *
139  * @param item[borrow] A ctrl item
140  * @param value The simple value to assign. Please consult the standard for
141  * 	allowed values
142  */
143 CBOR_EXPORT void cbor_set_ctrl(cbor_item_t *item, uint8_t value);
144 
145 /** Assign a boolean value to a boolean ctrl item
146  *
147  * @param item[borrow] A ctrl item
148  * @param value The simple value to assign.
149  */
150 CBOR_EXPORT void cbor_set_bool(cbor_item_t *item, bool value);
151 
152 /** Assigns a float value
153  *
154  * @param item[borrow] A half precision float
155  * @param value The value to assign
156  */
157 CBOR_EXPORT void cbor_set_float2(cbor_item_t *item, float value);
158 
159 /** Assigns a float value
160  *
161  * @param item[borrow] A single precision float
162  * @param value The value to assign
163  */
164 CBOR_EXPORT void cbor_set_float4(cbor_item_t *item, float value);
165 
166 /** Assigns a float value
167  *
168  * @param item[borrow] A double precision float
169  * @param value The value to assign
170  */
171 CBOR_EXPORT void cbor_set_float8(cbor_item_t *item, double value);
172 
173 /** Reads the control value
174  *
175  * @param item[borrow] A ctrl item
176  * @return the simple value
177  */
178 CBOR_EXPORT uint8_t cbor_ctrl_value(const cbor_item_t *item);
179 
180 /** Constructs a new float
181  *
182  * @param value the value to use
183  * @return **new** float
184  */
185 CBOR_EXPORT cbor_item_t *cbor_build_float2(float value);
186 
187 /** Constructs a new float
188  *
189  * @param value the value to use
190  * @return **new** float or `NULL` upon memory allocation failure
191  */
192 CBOR_EXPORT cbor_item_t *cbor_build_float4(float value);
193 
194 /** Constructs a new float
195  *
196  * @param value the value to use
197  * @return **new** float or `NULL` upon memory allocation failure
198  */
199 CBOR_EXPORT cbor_item_t *cbor_build_float8(double value);
200 
201 /** Constructs a ctrl item
202  *
203  * @param value the value to use
204  * @return **new** ctrl item or `NULL` upon memory allocation failure
205  */
206 CBOR_EXPORT cbor_item_t *cbor_build_ctrl(uint8_t value);
207 
208 #ifdef __cplusplus
209 }
210 #endif
211 
212 #endif  // LIBCBOR_FLOATS_CTRLS_H
213