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 A float or ctrl item
27  * @return Is this a ctrl value?
28  */
29 _CBOR_NODISCARD CBOR_EXPORT bool cbor_float_ctrl_is_ctrl(
30     const cbor_item_t *item);
31 
32 /** Get the float width
33  *
34  * @param item A float or ctrl item
35  * @return The width.
36  */
37 _CBOR_NODISCARD CBOR_EXPORT cbor_float_width
38 cbor_float_get_width(const cbor_item_t *item);
39 
40 /** Get a half precision float
41  *
42  * The item must have the corresponding width
43  *
44  * @param item A half precision float
45  * @return half precision value
46  */
47 _CBOR_NODISCARD CBOR_EXPORT float cbor_float_get_float2(
48     const cbor_item_t *item);
49 
50 /** Get a single precision float
51  *
52  * The item must have the corresponding width
53  *
54  * @param item A single precision float
55  * @return single precision value
56  */
57 _CBOR_NODISCARD CBOR_EXPORT float cbor_float_get_float4(
58     const cbor_item_t *item);
59 
60 /** Get a double precision float
61  *
62  * The item must have the corresponding width
63  *
64  * @param item A double precision float
65  * @return double precision value
66  */
67 _CBOR_NODISCARD CBOR_EXPORT double cbor_float_get_float8(
68     const cbor_item_t *item);
69 
70 /** Get the float value represented as double
71  *
72  * Can be used regardless of the width.
73  *
74  * @param item Any float
75  * @return double precision value
76  */
77 _CBOR_NODISCARD CBOR_EXPORT double cbor_float_get_float(
78     const cbor_item_t *item);
79 
80 /** Get value from a boolean ctrl item
81  *
82  * @param item A ctrl item
83  * @return boolean value
84  */
85 _CBOR_NODISCARD CBOR_EXPORT bool cbor_get_bool(const cbor_item_t *item);
86 
87 /** Constructs a new ctrl item
88  *
89  * The width cannot be changed once the item is created
90  *
91  * @return Reference to the new ctrl item. The item's reference count is
92  * initialized to one.
93  * @return `NULL` if memory allocation fails
94  */
95 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_ctrl(void);
96 
97 /** Constructs a new float item
98  *
99  * The width cannot be changed once the item is created
100  *
101  * @return Reference to the new float item. The item's reference count is
102  * initialized to one.
103  * @return `NULL` if memory allocation fails
104  */
105 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float2(void);
106 
107 /** Constructs a new float item
108  *
109  * The width cannot be changed once the item is created
110  *
111  * @return Reference to the new float item. The item's reference count is
112  * initialized to one.
113  * @return `NULL` if memory allocation fails
114  */
115 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float4(void);
116 
117 /** Constructs a new float item
118  *
119  * The width cannot be changed once the item is created
120  *
121  * @return Reference to the new float item. The item's reference count is
122  * initialized to one.
123  * @return `NULL` if memory allocation fails
124  */
125 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float8(void);
126 
127 /** Constructs new null ctrl item
128  *
129  * @return Reference to the new null item. The item's reference count is
130  * initialized to one.
131  * @return `NULL` if memory allocation fails
132  */
133 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_null(void);
134 
135 /** Constructs new undef ctrl item
136  *
137  * @return Reference to the new undef item. The item's reference count is
138  * initialized to one.
139  * @return `NULL` if memory allocation fails
140  */
141 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_undef(void);
142 
143 /** Constructs new boolean ctrl item
144  *
145  * @param value The value to use
146  * @return Reference to the new boolean item. The item's reference count is
147  * initialized to one.
148  * @return `NULL` if memory allocation fails
149  */
150 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_bool(bool value);
151 
152 /** Assign a control value
153  *
154  * \rst
155  * .. warning:: It is possible to produce an invalid CBOR value by assigning a
156  *  invalid value using this mechanism. Please consult the standard before use.
157  * \endrst
158  *
159  * @param item A ctrl item
160  * @param value The simple value to assign. Please consult the standard for
161  * 	allowed values
162  */
163 CBOR_EXPORT void cbor_set_ctrl(cbor_item_t *item, uint8_t value);
164 
165 /** Assign a boolean value to a boolean ctrl item
166  *
167  * @param item A ctrl item
168  * @param value The simple value to assign.
169  */
170 CBOR_EXPORT void cbor_set_bool(cbor_item_t *item, bool value);
171 
172 /** Assigns a float value
173  *
174  * @param item A half precision float
175  * @param value The value to assign
176  */
177 CBOR_EXPORT void cbor_set_float2(cbor_item_t *item, float value);
178 
179 /** Assigns a float value
180  *
181  * @param item A single precision float
182  * @param value The value to assign
183  */
184 CBOR_EXPORT void cbor_set_float4(cbor_item_t *item, float value);
185 
186 /** Assigns a float value
187  *
188  * @param item A double precision float
189  * @param value The value to assign
190  */
191 CBOR_EXPORT void cbor_set_float8(cbor_item_t *item, double value);
192 
193 /** Reads the control value
194  *
195  * @param item A ctrl item
196  * @return the simple value
197  */
198 _CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_ctrl_value(const cbor_item_t *item);
199 
200 /** Constructs a new float
201  *
202  * @param value the value to use
203  * @return Reference to the new float item. The item's reference count is
204  * initialized to one.
205  * @return `NULL` if memory allocation fails
206  */
207 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float2(float value);
208 
209 /** Constructs a new float
210  *
211  * @param value the value to use
212  * @return Reference to the new float item. The item's reference count is
213  * initialized to one.
214  * @return `NULL` if memory allocation fails
215  */
216 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float4(float value);
217 
218 /** Constructs a new float
219  *
220  * @param value the value to use
221  * @return Reference to the new float item. The item's reference count is
222  * initialized to one.
223  * @return `NULL` if memory allocation fails
224  */
225 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float8(double value);
226 
227 /** Constructs a ctrl item
228  *
229  * @param value the value to use
230  * @return Reference to the new ctrl item. The item's reference count is
231  * initialized to one.
232  * @return `NULL` if memory allocation fails
233  */
234 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_ctrl(uint8_t value);
235 
236 #ifdef __cplusplus
237 }
238 #endif
239 
240 #endif  // LIBCBOR_FLOATS_CTRLS_H
241