]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/ordered-set.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / ordered-set.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2015 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include "hashmap.h"
24
25 typedef struct OrderedSet OrderedSet;
26
27 static inline OrderedSet* ordered_set_new(const struct hash_ops *ops) {
28 return (OrderedSet*) ordered_hashmap_new(ops);
29 }
30
31 static inline int ordered_set_ensure_allocated(OrderedSet **s, const struct hash_ops *ops) {
32 if (*s)
33 return 0;
34
35 *s = ordered_set_new(ops);
36 if (!*s)
37 return -ENOMEM;
38
39 return 0;
40 }
41
42 static inline OrderedSet* ordered_set_free(OrderedSet *s) {
43 ordered_hashmap_free((OrderedHashmap*) s);
44 return NULL;
45 }
46
47 static inline OrderedSet* ordered_set_free_free(OrderedSet *s) {
48 ordered_hashmap_free_free((OrderedHashmap*) s);
49 return NULL;
50 }
51
52 static inline int ordered_set_put(OrderedSet *s, void *p) {
53 return ordered_hashmap_put((OrderedHashmap*) s, p, p);
54 }
55
56 static inline bool ordered_set_isempty(OrderedSet *s) {
57 return ordered_hashmap_isempty((OrderedHashmap*) s);
58 }
59
60 static inline bool ordered_set_iterate(OrderedSet *s, Iterator *i, void **value) {
61 return ordered_hashmap_iterate((OrderedHashmap*) s, i, value, NULL);
62 }
63
64 int ordered_set_consume(OrderedSet *s, void *p);
65 int ordered_set_put_strdup(OrderedSet *s, const char *p);
66 int ordered_set_put_strdupv(OrderedSet *s, char **l);
67
68 #define ORDERED_SET_FOREACH(e, s, i) \
69 for ((i) = ITERATOR_FIRST; ordered_set_iterate((s), &(i), (void**)&(e)); )
70
71 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedSet*, ordered_set_free);
72 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedSet*, ordered_set_free_free);
73
74 #define _cleanup_ordered_set_free_ _cleanup_(ordered_set_freep)
75 #define _cleanup_ordered_set_free_free_ _cleanup_(ordered_set_free_freep)