xref: /freebsd/contrib/libcbor/src/cbor/maps.h (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
110ff414cSEd Maste /*
210ff414cSEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
310ff414cSEd Maste  *
410ff414cSEd Maste  * libcbor is free software; you can redistribute it and/or modify
510ff414cSEd Maste  * it under the terms of the MIT license. See LICENSE for details.
610ff414cSEd Maste  */
710ff414cSEd Maste 
810ff414cSEd Maste #ifndef LIBCBOR_MAPS_H
910ff414cSEd Maste #define LIBCBOR_MAPS_H
1010ff414cSEd Maste 
1110ff414cSEd Maste #include "cbor/cbor_export.h"
1210ff414cSEd Maste #include "cbor/common.h"
1310ff414cSEd Maste 
1410ff414cSEd Maste #ifdef __cplusplus
1510ff414cSEd Maste extern "C" {
1610ff414cSEd Maste #endif
1710ff414cSEd Maste 
1810ff414cSEd Maste /*
1910ff414cSEd Maste  * ============================================================================
2010ff414cSEd Maste  * Map manipulation
2110ff414cSEd Maste  * ============================================================================
2210ff414cSEd Maste  */
2310ff414cSEd Maste 
2410ff414cSEd Maste /** Get the number of pairs
2510ff414cSEd Maste  *
26*5d3e7166SEd Maste  * @param item A map
2710ff414cSEd Maste  * @return The number of pairs
2810ff414cSEd Maste  */
29*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_map_size(const cbor_item_t *item);
3010ff414cSEd Maste 
3110ff414cSEd Maste /** Get the size of the allocated storage
3210ff414cSEd Maste  *
33*5d3e7166SEd Maste  * @param item A map
3410ff414cSEd Maste  * @return Allocated storage size (as the number of #cbor_pair items)
3510ff414cSEd Maste  */
36*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_map_allocated(const cbor_item_t *item);
3710ff414cSEd Maste 
3810ff414cSEd Maste /** Create a new definite map
3910ff414cSEd Maste  *
4010ff414cSEd Maste  * @param size The number of slots to preallocate
41*5d3e7166SEd Maste  * @return Reference to the new map item. The item's reference count is
42*5d3e7166SEd Maste  * initialized to one.
43*5d3e7166SEd Maste  * @return `NULL` if memory allocation fails
4410ff414cSEd Maste  */
45*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_definite_map(size_t size);
4610ff414cSEd Maste 
4710ff414cSEd Maste /** Create a new indefinite map
4810ff414cSEd Maste  *
49*5d3e7166SEd Maste  * @return Reference to the new map item. The item's reference count is
50*5d3e7166SEd Maste  * initialized to one.
51*5d3e7166SEd Maste  * @return `NULL` if memory allocation fails
5210ff414cSEd Maste  */
53*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_indefinite_map(void);
5410ff414cSEd Maste 
5510ff414cSEd Maste /** Add a pair to the map
5610ff414cSEd Maste  *
5710ff414cSEd Maste  * For definite maps, items can only be added to the preallocated space. For
5810ff414cSEd Maste  * indefinite maps, the storage will be expanded as needed
5910ff414cSEd Maste  *
60*5d3e7166SEd Maste  * @param item A map
61*5d3e7166SEd Maste  * @param pair The key-value pair to add. Reference count of the #cbor_pair.key
62*5d3e7166SEd Maste  * and #cbor_pair.value will be increased by one.
63*5d3e7166SEd Maste  * @return `true` on success, `false` if memory allocation failed (indefinite
64*5d3e7166SEd Maste  * maps) or the preallocated storage is full (definite maps)
6510ff414cSEd Maste  */
66*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT bool cbor_map_add(cbor_item_t *item,
67*5d3e7166SEd Maste                                               struct cbor_pair pair);
6810ff414cSEd Maste 
6910ff414cSEd Maste /** Add a key to the map
7010ff414cSEd Maste  *
7110ff414cSEd Maste  * Sets the value to `NULL`. Internal API.
7210ff414cSEd Maste  *
73*5d3e7166SEd Maste  * @param item A map
74*5d3e7166SEd Maste  * @param key The key, Its reference count will be be increased by one.
7510ff414cSEd Maste  * @return `true` on success, `false` if either reallocation failed or the
76*5d3e7166SEd Maste  * preallocated storage is full
7710ff414cSEd Maste  */
78*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT bool _cbor_map_add_key(cbor_item_t *item,
79*5d3e7166SEd Maste                                                    cbor_item_t *key);
8010ff414cSEd Maste 
8110ff414cSEd Maste /** Add a value to the map
8210ff414cSEd Maste  *
8310ff414cSEd Maste  * Assumes that #_cbor_map_add_key has been called. Internal API.
8410ff414cSEd Maste  *
85*5d3e7166SEd Maste  * @param item A map
86*5d3e7166SEd Maste  * @param value The value. Its reference count will be be increased by one.
8710ff414cSEd Maste  * @return `true` on success, `false` if either reallocation failed or the
88*5d3e7166SEd Maste  * preallocated storage is full
8910ff414cSEd Maste  */
90*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT bool _cbor_map_add_value(cbor_item_t *item,
91*5d3e7166SEd Maste                                                      cbor_item_t *value);
9210ff414cSEd Maste 
9310ff414cSEd Maste /** Is this map definite?
9410ff414cSEd Maste  *
95*5d3e7166SEd Maste  * @param item A map
9610ff414cSEd Maste  * @return Is this map definite?
9710ff414cSEd Maste  */
98*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT bool cbor_map_is_definite(const cbor_item_t *item);
9910ff414cSEd Maste 
10010ff414cSEd Maste /** Is this map indefinite?
10110ff414cSEd Maste  *
102*5d3e7166SEd Maste  * @param item A map
10310ff414cSEd Maste  * @return Is this map indefinite?
10410ff414cSEd Maste  */
105*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT bool cbor_map_is_indefinite(
106*5d3e7166SEd Maste     const cbor_item_t *item);
10710ff414cSEd Maste 
10810ff414cSEd Maste /** Get the pairs storage
10910ff414cSEd Maste  *
110*5d3e7166SEd Maste  * @param item A map
11110ff414cSEd Maste  * @return Array of #cbor_map_size pairs. Manipulation is possible as long as
11210ff414cSEd Maste  * references remain valid.
11310ff414cSEd Maste  */
114*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT struct cbor_pair *cbor_map_handle(
115*5d3e7166SEd Maste     const cbor_item_t *item);
11610ff414cSEd Maste 
11710ff414cSEd Maste #ifdef __cplusplus
11810ff414cSEd Maste }
11910ff414cSEd Maste #endif
12010ff414cSEd Maste 
12110ff414cSEd Maste #endif  // LIBCBOR_MAPS_H
122