Path: blob/main/contrib/libcbor/src/cbor/floats_ctrls.h
39536 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#ifndef LIBCBOR_FLOATS_CTRLS_H8#define LIBCBOR_FLOATS_CTRLS_H910#include "cbor/cbor_export.h"11#include "cbor/common.h"1213#ifdef __cplusplus14extern "C" {15#endif1617/*18* ============================================================================19* Float manipulation20* ============================================================================21*/2223/** Is this a ctrl value?24*25* @param item A float or ctrl item26* @return Is this a ctrl value?27*/28_CBOR_NODISCARD CBOR_EXPORT bool cbor_float_ctrl_is_ctrl(29const cbor_item_t *item);3031/** Get the float width32*33* @param item A float or ctrl item34* @return The width.35*/36_CBOR_NODISCARD CBOR_EXPORT cbor_float_width37cbor_float_get_width(const cbor_item_t *item);3839/** Get a half precision float40*41* The item must have the corresponding width42*43* @param item A half precision float44* @return half precision value45*/46_CBOR_NODISCARD CBOR_EXPORT float cbor_float_get_float2(47const cbor_item_t *item);4849/** Get a single precision float50*51* The item must have the corresponding width52*53* @param item A single precision float54* @return single precision value55*/56_CBOR_NODISCARD CBOR_EXPORT float cbor_float_get_float4(57const cbor_item_t *item);5859/** Get a double precision float60*61* The item must have the corresponding width62*63* @param item A double precision float64* @return double precision value65*/66_CBOR_NODISCARD CBOR_EXPORT double cbor_float_get_float8(67const cbor_item_t *item);6869/** Get the float value represented as double70*71* Can be used regardless of the width.72*73* @param item Any float74* @return double precision value75*/76_CBOR_NODISCARD CBOR_EXPORT double cbor_float_get_float(77const cbor_item_t *item);7879/** Get value from a boolean ctrl item80*81* @param item A ctrl item82* @return boolean value83*/84_CBOR_NODISCARD CBOR_EXPORT bool cbor_get_bool(const cbor_item_t *item);8586/** Constructs a new ctrl item87*88* The width cannot be changed once the item is created89*90* @return Reference to the new ctrl item. The item's reference count is91* initialized to one.92* @return `NULL` if memory allocation fails93*/94_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_ctrl(void);9596/** Constructs a new float item97*98* The width cannot be changed once the item is created99*100* @return Reference to the new float item. The item's reference count is101* initialized to one.102* @return `NULL` if memory allocation fails103*/104_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float2(void);105106/** Constructs a new float item107*108* The width cannot be changed once the item is created109*110* @return Reference to the new float item. The item's reference count is111* initialized to one.112* @return `NULL` if memory allocation fails113*/114_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float4(void);115116/** Constructs a new float item117*118* The width cannot be changed once the item is created119*120* @return Reference to the new float item. The item's reference count is121* initialized to one.122* @return `NULL` if memory allocation fails123*/124_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_float8(void);125126/** Constructs new null ctrl item127*128* @return Reference to the new null item. The item's reference count is129* initialized to one.130* @return `NULL` if memory allocation fails131*/132_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_null(void);133134/** Constructs new undef ctrl item135*136* @return Reference to the new undef item. The item's reference count is137* initialized to one.138* @return `NULL` if memory allocation fails139*/140_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_undef(void);141142/** Constructs new boolean ctrl item143*144* @param value The value to use145* @return Reference to the new boolean item. The item's reference count is146* initialized to one.147* @return `NULL` if memory allocation fails148*/149_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_bool(bool value);150151/** Assign a control value152*153* \rst154* .. warning:: It is possible to produce an invalid CBOR value by assigning a155* invalid value using this mechanism. Please consult the standard before use.156* \endrst157*158* @param item A ctrl item159* @param value The simple value to assign. Please consult the standard for160* allowed values161*/162CBOR_EXPORT void cbor_set_ctrl(cbor_item_t *item, uint8_t value);163164/** Assign a boolean value to a boolean ctrl item165*166* @param item A ctrl item167* @param value The simple value to assign.168*/169CBOR_EXPORT void cbor_set_bool(cbor_item_t *item, bool value);170171/** Assigns a float value172*173* @param item A half precision float174* @param value The value to assign175*/176CBOR_EXPORT void cbor_set_float2(cbor_item_t *item, float value);177178/** Assigns a float value179*180* @param item A single precision float181* @param value The value to assign182*/183CBOR_EXPORT void cbor_set_float4(cbor_item_t *item, float value);184185/** Assigns a float value186*187* @param item A double precision float188* @param value The value to assign189*/190CBOR_EXPORT void cbor_set_float8(cbor_item_t *item, double value);191192/** Reads the control value193*194* @param item A ctrl item195* @return the simple value196*/197_CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_ctrl_value(const cbor_item_t *item);198199/** Constructs a new float200*201* @param value the value to use202* @return Reference to the new float item. The item's reference count is203* initialized to one.204* @return `NULL` if memory allocation fails205*/206_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float2(float value);207208/** Constructs a new float209*210* @param value the value to use211* @return Reference to the new float item. The item's reference count is212* initialized to one.213* @return `NULL` if memory allocation fails214*/215_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float4(float value);216217/** Constructs a new float218*219* @param value the value to use220* @return Reference to the new float item. The item's reference count is221* initialized to one.222* @return `NULL` if memory allocation fails223*/224_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_float8(double value);225226/** Constructs a ctrl item227*228* @param value the value to use229* @return Reference to the new ctrl item. The item's reference count is230* initialized to one.231* @return `NULL` if memory allocation fails232*/233_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_ctrl(uint8_t value);234235#ifdef __cplusplus236}237#endif238239#endif // LIBCBOR_FLOATS_CTRLS_H240241242