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