xref: /freebsd/contrib/libcbor/src/cbor/maps.h (revision 9768746b)
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_MAPS_H
9 #define LIBCBOR_MAPS_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  * Map manipulation
21  * ============================================================================
22  */
23 
24 /** Get the number of pairs
25  *
26  * @param item[borrow] A map
27  * @return The number of pairs
28  */
29 CBOR_EXPORT size_t cbor_map_size(const cbor_item_t *item);
30 
31 /** Get the size of the allocated storage
32  *
33  * @param item[borrow] A map
34  * @return Allocated storage size (as the number of #cbor_pair items)
35  */
36 CBOR_EXPORT size_t cbor_map_allocated(const cbor_item_t *item);
37 
38 /** Create a new definite map
39  *
40  * @param size The number of slots to preallocate
41  * @return **new** definite map. `NULL` on malloc failure.
42  */
43 CBOR_EXPORT cbor_item_t *cbor_new_definite_map(size_t size);
44 
45 /** Create a new indefinite map
46  *
47  * @param size The number of slots to preallocate
48  * @return **new** definite map. `NULL` on malloc failure.
49  */
50 CBOR_EXPORT cbor_item_t *cbor_new_indefinite_map();
51 
52 /** Add a pair to the map
53  *
54  * For definite maps, items can only be added to the preallocated space. For
55  * indefinite maps, the storage will be expanded as needed
56  *
57  * @param item[borrow] A map
58  * @param pair[incref] The key-value pair to add (incref is member-wise)
59  * @return `true` on success, `false` if either reallocation failed or the
60  * preallcoated storage is full
61  */
62 CBOR_EXPORT bool cbor_map_add(cbor_item_t *item, struct cbor_pair pair);
63 
64 /** Add a key to the map
65  *
66  * Sets the value to `NULL`. Internal API.
67  *
68  * @param item[borrow] A map
69  * @param key[incref] The key
70  * @return `true` on success, `false` if either reallocation failed or the
71  * preallcoated storage is full
72  */
73 CBOR_EXPORT bool _cbor_map_add_key(cbor_item_t *item, cbor_item_t *key);
74 
75 /** Add a value to the map
76  *
77  * Assumes that #_cbor_map_add_key has been called. Internal API.
78  *
79  * @param item[borrow] A map
80  * @param key[incref] The value
81  * @return `true` on success, `false` if either reallocation failed or the
82  * preallcoated storage is full
83  */
84 CBOR_EXPORT bool _cbor_map_add_value(cbor_item_t *item, cbor_item_t *value);
85 
86 /** Is this map definite?
87  *
88  * @param item[borrow] A map
89  * @return Is this map definite?
90  */
91 CBOR_EXPORT bool cbor_map_is_definite(const cbor_item_t *item);
92 
93 /** Is this map indefinite?
94  *
95  * @param item[borrow] A map
96  * @return Is this map indefinite?
97  */
98 CBOR_EXPORT bool cbor_map_is_indefinite(const cbor_item_t *item);
99 
100 /** Get the pairs storage
101  *
102  * @param item[borrow] A map
103  * @return Array of #cbor_map_size pairs. Manipulation is possible as long as
104  * references remain valid.
105  */
106 CBOR_EXPORT struct cbor_pair *cbor_map_handle(const cbor_item_t *item);
107 
108 #ifdef __cplusplus
109 }
110 #endif
111 
112 #endif  // LIBCBOR_MAPS_H
113