/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#ifndef LIBCBOR_ARRAYS_H8#define LIBCBOR_ARRAYS_H910#include "cbor/cbor_export.h"11#include "cbor/common.h"1213#ifdef __cplusplus14extern "C" {15#endif1617/** Get the number of members18*19* @param item An array20* @return The number of members21*/22_CBOR_NODISCARD23CBOR_EXPORT size_t cbor_array_size(const cbor_item_t* item);2425/** Get the size of the allocated storage26*27* @param item An array28* @return The size of the allocated storage (number of items)29*/30_CBOR_NODISCARD31CBOR_EXPORT size_t cbor_array_allocated(const cbor_item_t* item);3233/** Get item by index34*35* @param item An array36* @param index The index (zero-based)37* @return Reference to the item, or `NULL` in case of boundary violation.38*39* Increases the reference count of the underlying item. The returned reference40* must be released using #cbor_decref.41*/42_CBOR_NODISCARD43CBOR_EXPORT cbor_item_t* cbor_array_get(const cbor_item_t* item, size_t index);4445/** Set item by index46*47* If the index is out of bounds, the array is not modified and false is48* returned. Creating arrays with holes is not possible.49*50* @param item An array51* @param value The item to assign52* @param index The index (zero-based)53* @return `true` on success, `false` on allocation failure.54*/55_CBOR_NODISCARD56CBOR_EXPORT bool cbor_array_set(cbor_item_t* item, size_t index,57cbor_item_t* value);5859/** Replace item at an index60*61* The reference to the item being replaced will be released using #cbor_decref.62*63* @param item An array64* @param value The item to assign. Its reference count will be increased by65* one.66* @param index The index (zero-based)67* @return true on success, false on allocation failure.68*/69_CBOR_NODISCARD70CBOR_EXPORT bool cbor_array_replace(cbor_item_t* item, size_t index,71cbor_item_t* value);7273/** Is the array definite?74*75* @param item An array76* @return Is the array definite?77*/78_CBOR_NODISCARD79CBOR_EXPORT bool cbor_array_is_definite(const cbor_item_t* item);8081/** Is the array indefinite?82*83* @param item An array84* @return Is the array indefinite?85*/86_CBOR_NODISCARD87CBOR_EXPORT bool cbor_array_is_indefinite(const cbor_item_t* item);8889/** Get the array contents90*91* The items may be reordered and modified as long as references remain92* consistent.93*94* @param item An array item95* @return An array of #cbor_item_t pointers of size #cbor_array_size.96*/97_CBOR_NODISCARD98CBOR_EXPORT cbor_item_t** cbor_array_handle(const cbor_item_t* item);99100/** Create new definite array101*102* @param size Number of slots to preallocate103* @return Reference to the new array item. The item's reference count is104* initialized to one.105* @return `NULL` if memory allocation fails106*/107_CBOR_NODISCARD108CBOR_EXPORT cbor_item_t* cbor_new_definite_array(size_t size);109110/** Create new indefinite array111*112* @return Reference to the new array item. The item's reference count is113* initialized to one.114* @return `NULL` if memory allocation fails115*/116_CBOR_NODISCARD117CBOR_EXPORT cbor_item_t* cbor_new_indefinite_array(void);118119/** Append to the end120*121* For indefinite items, storage may be reallocated. For definite items, only122* the preallocated capacity is available.123*124* @param array An array125* @param pushee The item to push. Its reference count will be increased by126* one.127* @return `true` on success, `false` on failure128*/129_CBOR_NODISCARD130CBOR_EXPORT bool cbor_array_push(cbor_item_t* array, cbor_item_t* pushee);131132#ifdef __cplusplus133}134#endif135136#endif // LIBCBOR_ARRAYS_H137138139