common: list: impliment error disp and peek functions.
[avr_work.git] / common / list.h
blob247e914ed8789551369fcb98c3f1af8f618de5e3
1 /*
2 * list.h
3 */
4 #ifndef _LIST_H_
5 #define _LIST_H_
6 #include <stdint.h>
7 #include <stdbool.h>
9 typedef char list_base_t; // allowed to use char* to access any type of mem.
10 typedef uint8_t list_index_t;
11 typedef int8_t list_error_t;
13 typedef struct {
14 list_base_t *buffer; // buffer
15 list_index_t first; // position of first element
16 list_index_t end; // position of last element +1
17 list_index_t ct; // number of elements in the list
18 const list_index_t sz; // size of the buffer
19 } list_t;
21 #define LIST_INITIALIZER(buff) {\
22 .buffer = (buff), .first = 0, .end = 0, .ct = 0,\
23 .sz = sizeof(buff) / sizeof(list_base_t) \
26 // add to front (push)
27 #define list_push list_push_front
28 int8_t list_push_front(list_t *list, list_base_t x);
29 int8_t list_push_front_o(list_t *list, list_base_t x);
31 // add to back (append)
32 #define list_append list_push_back
33 list_error_t list_push_back(list_t *list, list_base_t x);
34 list_error_t list_push_back_o(list_t *list, list_base_t x);
36 // remove
37 #define list_pop list_pop_front
38 list_base_t list_pop_front(list_t *list); // (pop)
39 #define list_take list_pop_back
40 list_base_t list_pop_back(list_t *list); // (take)
42 // peek
43 list_base_t list_peek_front(list_t *list);
44 list_base_t list_peek_back(list_t *list);
45 list_base_t list_peek(list_t *list, list_index_t index);
47 void list_flush(list_t *list);
48 bool list_empty(list_t *list);
49 bool list_full(list_t *list);
51 #endif