/src/strongswan/src/libstrongswan/metadata/metadata_factory.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2021 Tobias Brunner |
3 | | * Copyright (C) 2021 Thomas Egerer |
4 | | * |
5 | | * Copyright (C) secunet Security Networks AG |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify it |
8 | | * under the terms of the GNU General Public License as published by the |
9 | | * Free Software Foundation; either version 2 of the License, or (at your |
10 | | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, but |
13 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
14 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
15 | | * for more details. |
16 | | */ |
17 | | |
18 | | #include "metadata_factory.h" |
19 | | #include "metadata_int.h" |
20 | | |
21 | | #include <library.h> |
22 | | #include <collections/hashtable.h> |
23 | | |
24 | | typedef struct private_metadata_factory_t private_metadata_factory_t; |
25 | | |
26 | | /** |
27 | | * Private data |
28 | | */ |
29 | | struct private_metadata_factory_t { |
30 | | |
31 | | /** |
32 | | * Public interface |
33 | | */ |
34 | | metadata_factory_t public; |
35 | | |
36 | | /** |
37 | | * Registered metadata types (entry_t) |
38 | | */ |
39 | | hashtable_t *types; |
40 | | }; |
41 | | |
42 | | /** |
43 | | * Registered constructor data |
44 | | */ |
45 | | typedef struct { |
46 | | /** Type name */ |
47 | | char *type; |
48 | | /** Constructor */ |
49 | | metadata_create_t create; |
50 | | } entry_t; |
51 | | |
52 | | /** |
53 | | * Destroy an entry |
54 | | */ |
55 | | static void destroy_entry(entry_t *this) |
56 | 15.6k | { |
57 | 15.6k | if (this) |
58 | 7.84k | { |
59 | 7.84k | free(this->type); |
60 | 7.84k | free(this); |
61 | 7.84k | } |
62 | 15.6k | } |
63 | | |
64 | | METHOD(metadata_factory_t, create, metadata_t*, |
65 | | private_metadata_factory_t *this, const char *type, ...) |
66 | 0 | { |
67 | 0 | metadata_t *metadata = NULL; |
68 | 0 | entry_t *entry; |
69 | 0 | va_list args; |
70 | |
|
71 | 0 | entry = this->types->get(this->types, type); |
72 | 0 | if (entry) |
73 | 0 | { |
74 | 0 | va_start(args, type); |
75 | 0 | metadata = entry->create(type, args); |
76 | 0 | va_end(args); |
77 | 0 | } |
78 | 0 | return metadata; |
79 | 0 | } |
80 | | |
81 | | METHOD(metadata_factory_t, register_type, void, |
82 | | private_metadata_factory_t *this, const char *type, |
83 | | metadata_create_t create) |
84 | 7.84k | { |
85 | 7.84k | entry_t *entry; |
86 | | |
87 | 7.84k | INIT(entry, |
88 | 7.84k | .type = strdup(type), |
89 | 7.84k | .create = create, |
90 | 7.84k | ); |
91 | | |
92 | 7.84k | destroy_entry(this->types->put(this->types, entry->type, entry)); |
93 | 7.84k | } |
94 | | |
95 | | METHOD(metadata_factory_t, destroy, void, |
96 | | private_metadata_factory_t *this) |
97 | 3.92k | { |
98 | 3.92k | this->types->destroy_function(this->types, (void*)destroy_entry); |
99 | 3.92k | free(this); |
100 | 3.92k | } |
101 | | |
102 | | metadata_factory_t *metadata_factory_create() |
103 | 3.92k | { |
104 | 3.92k | private_metadata_factory_t *this; |
105 | | |
106 | 3.92k | INIT(this, |
107 | 3.92k | .public = { |
108 | 3.92k | .create = _create, |
109 | 3.92k | .register_type = _register_type, |
110 | 3.92k | .destroy = _destroy, |
111 | 3.92k | }, |
112 | 3.92k | .types = hashtable_create(hashtable_hash_str, hashtable_equals_str, 0), |
113 | 3.92k | ); |
114 | | |
115 | | /* register pre-defined types */ |
116 | 3.92k | register_type(this, METADATA_TYPE_INT, metadata_create_int); |
117 | 3.92k | register_type(this, METADATA_TYPE_UINT64, metadata_create_int); |
118 | | |
119 | 3.92k | return &this->public; |
120 | 3.92k | } |