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