aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/utils/linked_list.h
blob: ee07ce487293a93fb95ffade182b7fd32995d6e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * @file linked_list.h
 * 
 * @brief Interface of linked_list_t.
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_

#include <types.h>
#include <utils/iterator.h>


typedef struct linked_list_t linked_list_t;

/**
 * @brief Class implementing a double linked list (named only as linked list).
 *
 * @warning Access to an object of this type is not thread-save.
 * 
 * @see job_queue_t, event_queue_t, send_queue_t
 * 
 * @ingroup utils
 */
struct linked_list_t {

	/**
	 * @brief Gets the count of items in the list.
	 * 
	 * @param linked_list 		calling object
	 * @return 					number of items in list
	 */
	int (*get_count) (linked_list_t *linked_list);
	
	/**
	 * @brief Creates a iterator for the given list.
	 * 
	 * @warning Created iterator_t object has to get destroyed by the caller.
	 * 
	 * @param linked_list 		calling object
	 * @param[in] forward 		iterator direction (TRUE: front to end)
	 * @return					new iterator_t object
	 */
	iterator_t * (*create_iterator) (linked_list_t *linked_list, bool forward);

	/**
	 * @brief Inserts a new item at the beginning of the list.
	 * 
	 * @param linked_list 	calling object
	 * @param[in] 			item value to insert in list
	 */
	void (*insert_first) (linked_list_t *linked_list, void *item);

	/**
	 * @brief Removes the first item in the list and returns its value.
	 * 
	 * @param linked_list 	calling object
	 * @param[in] item 		returned value of first item
	 * @return
	 * 						- SUCCESS
	 * 						- NOT_FOUND, if list is empty
	 */
	status_t (*remove_first) (linked_list_t *linked_list, void **item);

	/**
	 * @brief Returns the value of the first list item without removing it.
	 * 
	 * @param linked_list 	calling object
	 * @param[out] 			item returned value of first item
	 * @return
	 * 						- SUCCESS
	 * 						- NOT_FOUND, if list is empty
	 */
	status_t (*get_first) (linked_list_t *linked_list, void **item);

	/**
	 * @brief Inserts a new item at the end of the list.
	 * 
	 * @param linked_list 	calling object
	 * @param[in] 			item value to insert into list
	 */
	void (*insert_last) (linked_list_t *linked_list, void *item);
	
	/**
	 * @brief Removes the last item in the list and returns its value.
	 * 
	 * @param linked_list 	calling object
	 * @param[out] 			item returned value of last item
	 * @return
	 * 						- SUCCESS
	 * 						- NOT_FOUND if list is empty
	 */
	status_t (*remove_last) (linked_list_t *linked_list, void **item);

	/**
	 * @brief Returns the value of the last list item without removing it.
	 * 
	 * @param linked_list 	calling object
	 * @param[out] 			item returned value of last item
	 * @return
	 * 						- SUCCESS
	 * 						- NOT_FOUND if list is empty
	 */
	status_t (*get_last) (linked_list_t *linked_list, void **item);
	
	/**
	 * @brief Destroys a linked_list object.
	 * 
	 * @warning All items are removed before deleting the list. The
	 *          associated values are NOT destroyed. 
	 * 			Destroying an list which is not empty may cause
	 * 			memory leaks!
	 * 
	 * @param linked_list 	calling object
	 */
	void (*destroy) (linked_list_t *linked_list);
};

/**
 * @brief Creates an empty linked list object.
 * 
 * @return the created linked list.
 * 
 * @ingroup utils
 */
linked_list_t *linked_list_create();


#endif /*LINKED_LIST_H_*/