/*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_MAPS_H8#define LIBCBOR_MAPS_H910#include "cbor/cbor_export.h"11#include "cbor/common.h"1213#ifdef __cplusplus14extern "C" {15#endif1617/*18* ============================================================================19* Map manipulation20* ============================================================================21*/2223/** Get the number of pairs24*25* @param item A map26* @return The number of pairs27*/28_CBOR_NODISCARD CBOR_EXPORT size_t cbor_map_size(const cbor_item_t *item);2930/** Get the size of the allocated storage31*32* @param item A map33* @return Allocated storage size (as the number of #cbor_pair items)34*/35_CBOR_NODISCARD CBOR_EXPORT size_t cbor_map_allocated(const cbor_item_t *item);3637/** Create a new definite map38*39* @param size The number of slots to preallocate40* @return Reference to the new map item. The item's reference count is41* initialized to one.42* @return `NULL` if memory allocation fails43*/44_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_definite_map(size_t size);4546/** Create a new indefinite map47*48* @return Reference to the new map item. The item's reference count is49* initialized to one.50* @return `NULL` if memory allocation fails51*/52_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_indefinite_map(void);5354/** Add a pair to the map55*56* For definite maps, items can only be added to the preallocated space. For57* indefinite maps, the storage will be expanded as needed58*59* @param item A map60* @param pair The key-value pair to add. Reference count of the #cbor_pair.key61* and #cbor_pair.value will be increased by one.62* @return `true` on success, `false` if memory allocation failed (indefinite63* maps) or the preallocated storage is full (definite maps)64*/65_CBOR_NODISCARD CBOR_EXPORT bool cbor_map_add(cbor_item_t *item,66struct cbor_pair pair);6768/** Add a key to the map69*70* Sets the value to `NULL`. Internal API.71*72* @param item A map73* @param key The key, Its reference count will be be increased by one.74* @return `true` on success, `false` if either reallocation failed or the75* preallocated storage is full76*/77_CBOR_NODISCARD CBOR_EXPORT bool _cbor_map_add_key(cbor_item_t *item,78cbor_item_t *key);7980/** Add a value to the map81*82* Assumes that #_cbor_map_add_key has been called. Internal API.83*84* @param item A map85* @param value The value. Its reference count will be be increased by one.86* @return `true` on success, `false` if either reallocation failed or the87* preallocated storage is full88*/89_CBOR_NODISCARD CBOR_EXPORT bool _cbor_map_add_value(cbor_item_t *item,90cbor_item_t *value);9192/** Is this map definite?93*94* @param item A map95* @return Is this map definite?96*/97_CBOR_NODISCARD CBOR_EXPORT bool cbor_map_is_definite(const cbor_item_t *item);9899/** Is this map indefinite?100*101* @param item A map102* @return Is this map indefinite?103*/104_CBOR_NODISCARD CBOR_EXPORT bool cbor_map_is_indefinite(105const cbor_item_t *item);106107/** Get the pairs storage108*109* @param item A map110* @return Array of #cbor_map_size pairs. Manipulation is possible as long as111* references remain valid.112*/113_CBOR_NODISCARD CBOR_EXPORT struct cbor_pair *cbor_map_handle(114const cbor_item_t *item);115116#ifdef __cplusplus117}118#endif119120#endif // LIBCBOR_MAPS_H121122123