Imported Upstream version 0.9.2
[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 "cacommon.h"
26
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31
32 /**
33  * @struct u_arraylist_t
34  * @brief array list structure
35  */
36 typedef struct u_arraylist_t
37 {
38     void **data;
39     uint32_t length;
40     uint32_t size;
41 } u_arraylist_t;
42
43 /**
44  * @brief API to creates array list and initializes the elements.
45  * @return  u_arraylist_t if Success, NULL otherwise
46  */
47 u_arraylist_t *u_arraylist_create();
48
49 /**
50  * @brief Resets and deletes the array list
51  * Arraylist elements are deleted. Calling function must take care of free
52  * dynamic memory allocated before freeing the arraylist
53  * @param list- u_arraylist pointer
54  * @return CAResult_t
55  * CA_STATUS_OK if Success, CA_STATUS_INVALID_PARAM if pointer to list is NULL
56  */
57 CAResult_t u_arraylist_free(u_arraylist_t **list);
58
59 /**
60  * @brief Returns the data of the index from the array list
61  * @param list
62  *     [IN] pointer of array list
63  * @param index
64  *     [IN] index of array list
65  * @return void pointer of data if success or NULL pointer otherwise
66  */
67 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index);
68
69 /**
70  * @brief Add data in the array list
71  * @param list
72  *     [IN] pointer of array list
73  * @param data
74  *     [IN] pointer of data
75  * @return CAResult_t
76  * CA_STATUS_OK if Success, CA_MEMORY_ALLOC_FAILED if memory allocation fails
77  */
78 CAResult_t u_arraylist_add(u_arraylist_t *list, void *data);
79
80 /**
81  * @brief Remove the data of the index from the array list
82  * @param list
83  *     [IN] pointer of array list
84  * @param index
85  *     [IN] index of array list
86  * @return void pointer of the data if success or NULL pointer otherwise
87  */
88 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index);
89
90 /**
91  * @brief Returns the length of the array list
92  * @param list
93  *     [IN] pointer of array list
94  * @return length of the array list
95  */
96 uint32_t u_arraylist_length(const u_arraylist_t *list);
97
98 /**
99  * @brief Returns whether the data exists or not
100  * @param list
101  *     [IN] pointer of array list
102  * @param data
103  *     [IN] pointer of data
104  * @return true if exists, false otherwise
105  */
106 bool u_arraylist_contains(const u_arraylist_t *list,const void *data);
107
108 /**
109  * @brief Destroys array list and elements (assuming elements are shallow)
110  * @param list
111  *    [IN] pointer of array list
112  */
113 void u_arraylist_destroy(u_arraylist_t *list);
114
115 #ifdef __cplusplus
116 }
117 #endif
118
119 #endif /* U_ARRAYLIST_H_ */