Merge "Merge branch 'master' into simulator" into simulator
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / inc / uarraylist.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #ifndef U_ARRAYLIST_H_
22 #define U_ARRAYLIST_H_
23
24 #include <stdint.h>
25 #include <stdbool.h>
26
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31
32 /**
33  * array list structure.
34  *
35  * @note
36  * Members should be treated as private and not accessed directly. Instead
37  * all access should be through the defined u_arraylist_*() functions.
38  */
39 typedef struct u_arraylist_t
40 {
41     void **data;
42     uint32_t length;
43     uint32_t capacity;
44 } u_arraylist_t;
45
46 /**
47  * API to creates array list and initializes the elements.
48  * @return  u_arraylist_t if Success, NULL otherwise.
49  */
50 u_arraylist_t *u_arraylist_create();
51
52 /**
53  * Resets and deletes the array list.
54  * Arraylist elements are deleted. Calling function must take care of free
55  * dynamic memory allocated before freeing the arraylist.
56  * @param[in] list       u_arraylist pointer
57  */
58 void u_arraylist_free(u_arraylist_t **list);
59
60 /**
61  * Request that the list prepare room for the specified number of entries.
62  * If count is greater than the current internal storage size then an
63  * an attempt will be made to reallocate room for at least count items.
64  *
65  * In other cases there will be no effect.
66  *
67  * This call will not affect the length used and cannot be used to remove
68  * entries.
69  * @param list the list to operate on.
70  * @param count the size to attempt to reserve room for.
71  */
72 void u_arraylist_reserve(u_arraylist_t *list, size_t count);
73
74 /**
75  * Request that the storage in the list be reduced to fit its current length.
76  *
77  * The request is non-binding, and may not affect the entries in the list.
78  * @param list the list to operate on.
79  */
80 void u_arraylist_shrink_to_fit(u_arraylist_t *list);
81
82 /**
83  * Returns the data of the index from the array list.
84  * @param[in] list         pointer of array list.
85  * @param[in] index        index of array list.
86  * @return void pointer of data if success or NULL pointer otherwise.
87  */
88 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index);
89
90 /**
91  * Add data in the array list.
92  * @param[in] list        pointer of array list.
93  * @param[in] data        pointer of data.
94  * @return true if success, false otherwise.
95  */
96 bool u_arraylist_add(u_arraylist_t *list, void *data);
97
98 /**
99  * Remove the data of the index from the array list.
100  * @param[in] list       pointer of array list.
101  * @param[in] index      index of array list.
102  * @return void pointer of the data if success or NULL pointer otherwise.
103  */
104 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index);
105
106 /**
107  * Returns the length of the array list.
108  * @param[in] list       pointer of array list.
109  * @return length of the array list.
110  */
111 uint32_t u_arraylist_length(const u_arraylist_t *list);
112
113 /**
114  * Returns whether the data exists or not.
115  * @param[in] list       pointer of array list.
116  * @param[in] data       pointer of data.
117  * @return true if exists, false otherwise.
118  */
119 bool u_arraylist_contains(const u_arraylist_t *list,const void *data);
120
121 /**
122  * Destroys array list and elements (assuming elements are shallow).
123  * @param[in] list       pointer of array list.
124  */
125 void u_arraylist_destroy(u_arraylist_t *list);
126
127 #ifdef __cplusplus
128 }
129 #endif
130
131 #endif /* U_ARRAYLIST_H_ */