summaryrefslogtreecommitdiffstats
path: root/lib/libcbor/src/cbor/maps.h
blob: 4e27eefe325f567d64b490d3be034684beb968af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#ifndef LIBCBOR_MAPS_H
#define LIBCBOR_MAPS_H

#include "cbor/common.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
 * ============================================================================
 * Map manipulation
 * ============================================================================
 */

/** Get the number of pairs
 *
 * @param item[borrow] A map
 * @return The number of pairs
 */
size_t cbor_map_size(const cbor_item_t *item);

/** Get the size of the allocated storage
 *
 * @param item[borrow] A map
 * @return Allocated storage size (as the number of #cbor_pair items)
 */
size_t cbor_map_allocated(const cbor_item_t *item);

/** Create a new definite map
 *
 * @param size The number of slots to preallocate
 * @return **new** definite map. `NULL` on malloc failure.
 */
cbor_item_t *cbor_new_definite_map(size_t size);

/** Create a new indefinite map
 *
 * @param size The number of slots to preallocate
 * @return **new** definite map. `NULL` on malloc failure.
 */
cbor_item_t *cbor_new_indefinite_map();

/** Add a pair to the map
 *
 * For definite maps, items can only be added to the preallocated space. For
 * indefinite maps, the storage will be expanded as needed
 *
 * @param item[borrow] A map
 * @param pair[incref] The key-value pair to add (incref is member-wise)
 * @return `true` on success, `false` if either reallocation failed or the
 * preallcoated storage is full
 */
bool cbor_map_add(cbor_item_t *item, struct cbor_pair pair);

/** Add a key to the map
 *
 * Sets the value to `NULL`. Internal API.
 *
 * @param item[borrow] A map
 * @param key[incref] The key
 * @return `true` on success, `false` if either reallocation failed or the
 * preallcoated storage is full
 */
bool _cbor_map_add_key(cbor_item_t *item, cbor_item_t *key);

/** Add a value to the map
 *
 * Assumes that #_cbor_map_add_key has been called. Internal API.
 *
 * @param item[borrow] A map
 * @param key[incref] The value
 * @return `true` on success, `false` if either reallocation failed or the
 * preallcoated storage is full
 */
bool _cbor_map_add_value(cbor_item_t *item, cbor_item_t *value);

/** Is this map definite?
 *
 * @param item[borrow] A map
 * @return Is this map definite?
 */
bool cbor_map_is_definite(const cbor_item_t *item);

/** Is this map indefinite?
 *
 * @param item[borrow] A map
 * @return Is this map indefinite?
 */
bool cbor_map_is_indefinite(const cbor_item_t *item);

/** Get the pairs storage
 *
 * @param item[borrow] A map
 * @return Array of #cbor_map_size pairs. Manipulation is possible as long as
 * references remain valid.
 */
struct cbor_pair *cbor_map_handle(const cbor_item_t *item);

#ifdef __cplusplus
}
#endif

#endif  // LIBCBOR_MAPS_H