microui  14.1.0
microui
ui_rect_collection.h
1 /*
2  * C
3  *
4  * Copyright 2023-2024 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
8 #ifndef UI_RECT_COLLECTION_H
9 #define UI_RECT_COLLECTION_H
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /*
16  * @brief Exposes the ui_rect_collection_t type that handles unordered ui_rect_t collections
17  */
18 
19 // --------------------------------------------------------------------------------
20 // Includes
21 // --------------------------------------------------------------------------------
22 
23 #include <assert.h>
24 
25 #include "ui_rect.h"
26 #include "ui_configuration.h"
27 
28 // --------------------------------------------------------------------------------
29 // Typedefs
30 // --------------------------------------------------------------------------------
31 
32 /*
33  * @brief A rectangle collection contains an array of rectangles and a pointer to
34  * the latest rectangle.
35  */
36 typedef struct ui_rect_collection_t {
37  ui_rect_t data[UI_RECT_COLLECTION_MAX_LENGTH];
38  size_t length;
40 
41 // --------------------------------------------------------------------------------
42 // Public functions
43 // --------------------------------------------------------------------------------
44 
45 /*
46  * @brief Clears the collection: the latest rectangle points on the first rectangle.
47  *
48  * @param[in] collection the collection to clear
49  */
50 static inline void UI_RECT_COLLECTION_clear(ui_rect_collection_t *collection) {
51  collection->length = 0;
52 }
53 
54 /*
55  * @brief Initializes the collection.
56  *
57  * @param[in] collection the collection to initialize
58  */
59 static inline void UI_RECT_COLLECTION_init(ui_rect_collection_t *collection) {
60  UI_RECT_COLLECTION_clear(collection);
61 }
62 
63 /*
64  * @brief Gets the number of rectangles that have been added to the collection.
65  *
66  * @param[in] collection the collection to check
67  *
68  * @return the available number of rectangles in the collection
69  */
70 static inline size_t UI_RECT_COLLECTION_get_length(const ui_rect_collection_t *collection) {
71  return collection->length;
72 }
73 
74 /*
75  * @brief Tells if the collection is full.
76  *
77  * @param[in] collection the collection to check
78  *
79  * @return true when the collection is full
80  */
81 static inline bool UI_RECT_COLLECTION_is_full(const ui_rect_collection_t *collection) {
82  return UI_RECT_COLLECTION_get_length(collection) >= UI_RECT_COLLECTION_MAX_LENGTH;
83 }
84 
85 /*
86  * @brief Tells if the collection is empty.
87  *
88  * @param[in] collection the collection to check
89  *
90  * @return true when the collection is empty
91  */
92 static inline bool UI_RECT_COLLECTION_is_empty(const ui_rect_collection_t *collection) {
93  return collection->length == 0u;
94 }
95 
96 /*
97  * @brief Gets the last rectangle added to the collection or NULL if the collection is empty.
98  *
99  * @param[in] collection the collection where retrieve latest element
100  *
101  * @return a rectangle or NULL
102  */
103 static inline ui_rect_t * UI_RECT_COLLECTION_get_last(ui_rect_collection_t *collection) {
104  return UI_RECT_COLLECTION_is_empty(collection) ? NULL : (collection->data + collection->length - 1u);
105 }
106 
107 /*
108  * @brief Gets a pointer to the address after the last rectangle in the collection.
109  *
110  * @param[in] collection the collection
111  *
112  * @return a pointer to the address after the last element in the collection
113  */
114 static inline ui_rect_t * UI_RECT_COLLECTION_get_end(ui_rect_collection_t *collection) {
115  return collection->data + collection->length;
116 }
117 
118 /*
119  * @brief Adds a rectangle in the collection. The implementation assumes that caller ensure that
120  * the collection is not full. The rectangle is copied to the collection.
121  *
122  * @param[in] collection the collection where copying the rectangle
123  * @param[in] element the rectangle to add
124  */
125 static inline void UI_RECT_COLLECTION_add_rect(ui_rect_collection_t *collection, const ui_rect_t element) {
126  assert(!UI_RECT_COLLECTION_is_full(collection));
127  collection->data[collection->length] = element;
128  collection->length++;
129 }
130 
131 /*
132  * @brief Removes a rectangle from the collection. The behavior is undefined if the rectangle is not part of the
133  * collection.
134  *
135  * @param[in] collection the collection which contains the rectangle
136  * @param[in] element the rectangle to remove
137  */
138 static inline void UI_RECT_COLLECTION_remove_rect(ui_rect_collection_t *collection, ui_rect_t *element) {
139  assert(!UI_RECT_COLLECTION_is_empty(collection));
140  assert(element >= collection->data);
141  assert(element < (collection->data + collection->length));
142  *element = *UI_RECT_COLLECTION_get_last(collection);
143  collection->length--;
144 }
145 
146 /*
147  * @brief Removes all empty rectangles from the collection.
148  *
149  * @param[in] collection the collection
150  */
151 static inline void UI_RECT_COLLECTION_remove_empty_rects(ui_rect_collection_t *collection) {
152  // cppcheck-suppress [misra-c2012-14.2] do not increment i systematically
153  for (size_t i = 0; i < collection->length;) {
154  if (UI_RECT_is_empty(&collection->data[i])) {
155  UI_RECT_COLLECTION_remove_rect(collection, &collection->data[i]);
156  } else {
157  i++;
158  }
159  }
160 }
161 
162 // --------------------------------------------------------------------------------
163 // EOF
164 // --------------------------------------------------------------------------------
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif // UI_RECT_COLLECTION_H
MicroEJ MicroUI library low level API: enable some features according to the hardware capabilities.