| 1 | /* |
| 2 | * Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]> |
| 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_ARRAYS_H |
| 9 | #define LIBCBOR_ARRAYS_H |
| 10 | |
| 11 | #include "cbor/cbor_export.h" |
| 12 | #include "cbor/common.h" |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | /** Get the number of members |
| 19 | * |
| 20 | * @param item An array |
| 21 | * @return The number of members |
| 22 | */ |
| 23 | _CBOR_NODISCARD |
| 24 | CBOR_EXPORT size_t cbor_array_size(const cbor_item_t* item); |
| 25 | |
| 26 | /** Get the size of the allocated storage |
| 27 | * |
| 28 | * @param item An array |
| 29 | * @return The size of the allocated storage (number of items) |
| 30 | */ |
| 31 | _CBOR_NODISCARD |
| 32 | CBOR_EXPORT size_t cbor_array_allocated(const cbor_item_t* item); |
| 33 | |
| 34 | /** Get item by index |
| 35 | * |
| 36 | * @param item An array |
| 37 | * @param index The index (zero-based) |
| 38 | * @return Reference to the item, or `NULL` in case of boundary violation. |
| 39 | * |
| 40 | * Increases the reference count of the underlying item. The returned reference |
| 41 | * must be released using #cbor_decref. |
| 42 | */ |
| 43 | _CBOR_NODISCARD |
| 44 | CBOR_EXPORT cbor_item_t* cbor_array_get(const cbor_item_t* item, size_t index); |
| 45 | |
| 46 | /** Set item by index |
| 47 | * |
| 48 | * If the index is out of bounds, the array is not modified and false is |
| 49 | * returned. Creating arrays with holes is not possible. |
| 50 | * |
| 51 | * @param item An array |
| 52 | * @param value The item to assign. Its reference count will be increased by |
| 53 | * one. |
| 54 | * @param index The index (zero-based) |
| 55 | * @return `true` on success, `false` on allocation failure. |
| 56 | */ |
| 57 | _CBOR_NODISCARD |
| 58 | CBOR_EXPORT bool cbor_array_set(cbor_item_t* item, size_t index, |
| 59 | cbor_item_t* value); |
| 60 | |
| 61 | /** Replace item at an index |
| 62 | * |
| 63 | * The reference to the item being replaced will be released using #cbor_decref. |
| 64 | * |
| 65 | * @param item An array |
| 66 | * @param value The item to assign. Its reference count will be increased by |
| 67 | * one. |
| 68 | * @param index The index (zero-based) |
| 69 | * @return true on success, false on allocation failure. |
| 70 | */ |
| 71 | _CBOR_NODISCARD |
| 72 | CBOR_EXPORT bool cbor_array_replace(cbor_item_t* item, size_t index, |
| 73 | cbor_item_t* value); |
| 74 | |
| 75 | /** Is the array definite? |
| 76 | * |
| 77 | * @param item An array |
| 78 | * @return Is the array definite? |
| 79 | */ |
| 80 | _CBOR_NODISCARD |
| 81 | CBOR_EXPORT bool cbor_array_is_definite(const cbor_item_t* item); |
| 82 | |
| 83 | /** Is the array indefinite? |
| 84 | * |
| 85 | * @param item An array |
| 86 | * @return Is the array indefinite? |
| 87 | */ |
| 88 | _CBOR_NODISCARD |
| 89 | CBOR_EXPORT bool cbor_array_is_indefinite(const cbor_item_t* item); |
| 90 | |
| 91 | /** Get the array contents |
| 92 | * |
| 93 | * The items may be reordered and modified as long as references remain |
| 94 | * consistent. |
| 95 | * |
| 96 | * @param item An array item |
| 97 | * @return An array of #cbor_item_t pointers of size #cbor_array_size. |
| 98 | */ |
| 99 | _CBOR_NODISCARD |
| 100 | CBOR_EXPORT cbor_item_t** cbor_array_handle(const cbor_item_t* item); |
| 101 | |
| 102 | /** Create new definite array |
| 103 | * |
| 104 | * @param size Number of slots to preallocate |
| 105 | * @return Reference to the new array item. The item's reference count is |
| 106 | * initialized to one. |
| 107 | * @return `NULL` if memory allocation fails |
| 108 | */ |
| 109 | _CBOR_NODISCARD |
| 110 | CBOR_EXPORT cbor_item_t* cbor_new_definite_array(size_t size); |
| 111 | |
| 112 | /** Create new indefinite array |
| 113 | * |
| 114 | * @return Reference to the new array item. The item's reference count is |
| 115 | * initialized to one. |
| 116 | * @return `NULL` if memory allocation fails |
| 117 | */ |
| 118 | _CBOR_NODISCARD |
| 119 | CBOR_EXPORT cbor_item_t* cbor_new_indefinite_array(void); |
| 120 | |
| 121 | /** Append to the end |
| 122 | * |
| 123 | * For indefinite items, storage may be reallocated. For definite items, only |
| 124 | * the preallocated capacity is available. |
| 125 | * |
| 126 | * @param array An array |
| 127 | * @param pushee The item to push. Its reference count will be increased by |
| 128 | * one. |
| 129 | * @return `true` on success, `false` on failure |
| 130 | */ |
| 131 | _CBOR_NODISCARD |
| 132 | CBOR_EXPORT bool cbor_array_push(cbor_item_t* array, cbor_item_t* pushee); |
| 133 | |
| 134 | #ifdef __cplusplus |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | #endif // LIBCBOR_ARRAYS_H |