syslog-ng source
list-adt.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2019 Balabit
3  * Copyright (c) 2019 Laszlo Budai <laszlo.budai@balabit.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 
25 #ifndef LIST_ADT_H_INCLUDED
26 #define LIST_ADT_H_INCLUDED
27 
28 #include <syslog-ng.h>
29 
30 typedef struct _List List;
31 
32 typedef void (*list_foreach_fn)(gconstpointer list_data, gpointer user_data);
33 
34 struct _List
35 {
36  void (*append)(List *self, gconstpointer item);
37  void (*foreach)(List *self, list_foreach_fn foreach_fn, gpointer user_data);
38  gboolean (*is_empty)(List *self);
39  void (*remove_all)(List *self);
40  void (*free_fn)(List *self);
41 };
42 
43 static inline void
44 list_append(List *self, gconstpointer item)
45 {
46  g_assert(self->append);
47 
48  self->append(self, item);
49 }
50 
51 static inline void
52 list_foreach(List *self, list_foreach_fn foreach_fn, gpointer user_data)
53 {
54  g_assert(self->foreach);
55 
56  self->foreach(self, foreach_fn, user_data);
57 }
58 
59 static inline gboolean
60 list_is_empty(List *self)
61 {
62  g_assert(self->is_empty);
63 
64  return self->is_empty(self);
65 }
66 
67 static inline void
68 list_remove_all(List *self)
69 {
70  g_assert(self->remove_all);
71 
72  self->remove_all(self);
73 }
74 
75 static inline void
76 list_free(List *self)
77 {
78  if (self->free_fn)
79  self->free_fn(self);
80 
81  g_free(self);
82 }
83 
84 #endif
85 
void(* list_foreach_fn)(gconstpointer list_data, gpointer user_data)
Definition: list-adt.h:32
#define self
Definition: rcptid.c:38
Definition: list-adt.h:35
gboolean(* is_empty)(List *self)
Definition: list-adt.h:38
void(* remove_all)(List *self)
Definition: list-adt.h:39
void(* append)(List *self, gconstpointer item)
Definition: list-adt.h:36
void(* free_fn)(List *self)
Definition: list-adt.h:40