unit-tests: Fix "using integer constants in boolean context" warning
[strongswan.git] / src / libimcv / swima / swima_events.c
1 /*
2 * Copyright (C) 2017 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "swima_events.h"
17 #include "swima_record.h"
18
19 #include <collections/linked_list.h>
20 #include <utils/debug.h>
21
22 typedef struct private_swima_events_t private_swima_events_t;
23
24 /**
25 * Private data of a swima_events_t object.
26 *
27 */
28 struct private_swima_events_t {
29
30 /**
31 * Public swima_events_t interface.
32 */
33 swima_events_t public;
34
35 /**
36 * Epoch of Event IDs
37 */
38 uint32_t epoch;
39
40 /**
41 * Last Event ID
42 */
43 uint32_t last_eid;
44
45 /**
46 * Last Consulted Event ID
47 */
48 uint32_t last_consulted_eid;
49
50 /**
51 * List of SW records
52 */
53 linked_list_t *list;
54
55 /**
56 * Reference count
57 */
58 refcount_t ref;
59
60 };
61
62 METHOD(swima_events_t, add, void,
63 private_swima_events_t *this, swima_event_t *event)
64 {
65 this->list->insert_last(this->list, event);
66 }
67
68 METHOD(swima_events_t, get_count, int,
69 private_swima_events_t *this)
70 {
71 return this->list->get_count(this->list);
72 }
73
74 METHOD(swima_events_t, set_eid, void,
75 private_swima_events_t *this, uint32_t eid, uint32_t epoch)
76 {
77 this->last_eid = this->last_consulted_eid = eid;
78 this->epoch = epoch;
79 }
80
81 METHOD(swima_events_t, set_last_eid, void,
82 private_swima_events_t *this, uint32_t last_eid)
83 {
84 this->last_eid = last_eid;
85 }
86
87 METHOD(swima_events_t, get_eid, uint32_t,
88 private_swima_events_t *this, uint32_t *epoch, uint32_t *last_eid)
89 {
90 if (epoch)
91 {
92 *epoch = this->epoch;
93 }
94 if (last_eid)
95 {
96 *last_eid = this->last_eid;
97 }
98 return this->last_consulted_eid;
99 }
100
101 METHOD(swima_events_t, create_enumerator, enumerator_t*,
102 private_swima_events_t *this)
103 {
104 return this->list->create_enumerator(this->list);
105 }
106
107 METHOD(swima_events_t, get_ref, swima_events_t*,
108 private_swima_events_t *this)
109 {
110 ref_get(&this->ref);
111 return &this->public;
112 }
113
114 METHOD(swima_events_t, clear, void,
115 private_swima_events_t *this)
116 {
117 this->list->destroy_offset(this->list, offsetof(swima_event_t, destroy));
118 this->list = linked_list_create();
119 }
120
121 METHOD(swima_events_t, destroy, void,
122 private_swima_events_t *this)
123 {
124 if (ref_put(&this->ref))
125 {
126 this->list->destroy_offset(this->list, offsetof(swima_event_t, destroy));
127 free(this);
128 }
129 }
130
131 /**
132 * See header
133 */
134 swima_events_t *swima_events_create(void)
135 {
136 private_swima_events_t *this;
137
138 INIT(this,
139 .public = {
140 .add = _add,
141 .get_count = _get_count,
142 .set_eid = _set_eid,
143 .set_last_eid = _set_last_eid,
144 .get_eid = _get_eid,
145 .create_enumerator = _create_enumerator,
146 .get_ref = _get_ref,
147 .clear = _clear,
148 .destroy = _destroy,
149 },
150 .list = linked_list_create(),
151 .ref = 1,
152 );
153
154 return &this->public;
155 }