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:: 156 * It is possible to produce an invalid CBOR value by assigning an invalid 157 * value using this mechanism. Please consult the standard before use. 158 * \endrst 159 * 160 * @param item A ctrl item 161 * @param value The simple value to assign. Please consult the standard for 162 * allowed values 163 */ 164 CBOR_EXPORT void cbor_set_ctrl(cbor_item_t* item, uint8_t value); 165 166 /** Assign a boolean value to a boolean ctrl item 167 * 168 * @param item A ctrl item 169 * @param value The simple value to assign. 170 */ 171 CBOR_EXPORT void cbor_set_bool(cbor_item_t* item, bool value); 172 173 /** Assigns a float value 174 * 175 * @param item A half precision float 176 * @param value The value to assign 177 */ 178 CBOR_EXPORT void cbor_set_float2(cbor_item_t* item, float value); 179 180 /** Assigns a float value 181 * 182 * @param item A single precision float 183 * @param value The value to assign 184 */ 185 CBOR_EXPORT void cbor_set_float4(cbor_item_t* item, float value); 186 187 /** Assigns a float value 188 * 189 * @param item A double precision float 190 * @param value The value to assign 191 */ 192 CBOR_EXPORT void cbor_set_float8(cbor_item_t* item, double value); 193 194 /** Reads the control value 195 * 196 * @param item A ctrl item 197 * @return the simple value 198 */ 199 _CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_ctrl_value(const cbor_item_t* item); 200 201 /** Constructs a new float 202 * 203 * @param value the value to use 204 * @return Reference to the new float item. The item's reference count is 205 * initialized to one. 206 * @return `NULL` if memory allocation fails 207 */ 208 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t* cbor_build_float2(float value); 209 210 /** Constructs a new float 211 * 212 * @param value the value to use 213 * @return Reference to the new float item. The item's reference count is 214 * initialized to one. 215 * @return `NULL` if memory allocation fails 216 */ 217 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t* cbor_build_float4(float value); 218 219 /** Constructs a new float 220 * 221 * @param value the value to use 222 * @return Reference to the new float item. The item's reference count is 223 * initialized to one. 224 * @return `NULL` if memory allocation fails 225 */ 226 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t* cbor_build_float8(double value); 227 228 /** Constructs a ctrl item 229 * 230 * @param value the value to use 231 * @return Reference to the new ctrl item. The item's reference count is 232 * initialized to one. 233 * @return `NULL` if memory allocation fails 234 */ 235 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t* cbor_build_ctrl(uint8_t value); 236 237 #ifdef __cplusplus 238 } 239 #endif 240 241 #endif // LIBCBOR_FLOATS_CTRLS_H 242