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
15extern "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_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[borrow] A float or ctrl item
35 * @return The width.
36 */
37_CBOR_NODISCARD CBOR_EXPORT cbor_float_width
38cbor_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[borrow] 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[borrow] 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[borrow] 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[borrow] 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[borrow] 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 **new** 1B ctrl or `NULL` upon memory allocation failure
92 */
93_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_ctrl(void);
94
95/** Constructs a new float item
96 *
97 * The width cannot be changed once the item is created
98 *
99 * @return **new** 2B float or `NULL` upon memory allocation failure
100 */
101_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float2(void);
102
103/** Constructs a new float item
104 *
105 * The width cannot be changed once the item is created
106 *
107 * @return **new** 4B float or `NULL` upon memory allocation failure
108 */
109_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float4(void);
110
111/** Constructs a new float item
112 *
113 * The width cannot be changed once the item is created
114 *
115 * @return **new** 8B float or `NULL` upon memory allocation failure
116 */
117_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float8(void);
118
119/** Constructs new null ctrl item
120 *
121 * @return **new** null ctrl item or `NULL` upon memory allocation failure
122 */
123_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_null(void);
124
125/** Constructs new undef ctrl item
126 *
127 * @return **new** undef ctrl item or `NULL` upon memory allocation failure
128 */
129_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_undef(void);
130
131/** Constructs new boolean ctrl item
132 *
133 * @param value The value to use
134 * @return **new** boolean ctrl item or `NULL` upon memory allocation failure
135 */
136_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_bool(bool value);
137
138/** Assign a control value
139 *
140 * \rst
141 * .. warning:: It is possible to produce an invalid CBOR value by assigning a
142 *  invalid value using this mechanism. Please consult the standard before use.
143 * \endrst
144 *
145 * @param item[borrow] A ctrl item
146 * @param value The simple value to assign. Please consult the standard for
147 * 	allowed values
148 */
149CBOR_EXPORT void cbor_set_ctrl(cbor_item_t *item, uint8_t value);
150
151/** Assign a boolean value to a boolean ctrl item
152 *
153 * @param item[borrow] A ctrl item
154 * @param value The simple value to assign.
155 */
156CBOR_EXPORT void cbor_set_bool(cbor_item_t *item, bool value);
157
158/** Assigns a float value
159 *
160 * @param item[borrow] A half precision float
161 * @param value The value to assign
162 */
163CBOR_EXPORT void cbor_set_float2(cbor_item_t *item, float value);
164
165/** Assigns a float value
166 *
167 * @param item[borrow] A single precision float
168 * @param value The value to assign
169 */
170CBOR_EXPORT void cbor_set_float4(cbor_item_t *item, float value);
171
172/** Assigns a float value
173 *
174 * @param item[borrow] A double precision float
175 * @param value The value to assign
176 */
177CBOR_EXPORT void cbor_set_float8(cbor_item_t *item, double value);
178
179/** Reads the control value
180 *
181 * @param item[borrow] A ctrl item
182 * @return the simple value
183 */
184_CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_ctrl_value(const cbor_item_t *item);
185
186/** Constructs a new float
187 *
188 * @param value the value to use
189 * @return **new** float
190 */
191_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float2(float value);
192
193/** Constructs a new float
194 *
195 * @param value the value to use
196 * @return **new** float or `NULL` upon memory allocation failure
197 */
198_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float4(float value);
199
200/** Constructs a new float
201 *
202 * @param value the value to use
203 * @return **new** float or `NULL` upon memory allocation failure
204 */
205_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float8(double value);
206
207/** Constructs a ctrl item
208 *
209 * @param value the value to use
210 * @return **new** ctrl item or `NULL` upon memory allocation failure
211 */
212_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_ctrl(uint8_t value);
213
214#ifdef __cplusplus
215}
216#endif
217
218#endif  // LIBCBOR_FLOATS_CTRLS_H
219