]> git.proxmox.com Git - mirror_ovs.git/blob - lib/smap.h
lib, ovsdb: Adapt headers for C++ usage
[mirror_ovs.git] / lib / smap.h
1 /* Copyright (c) 2012, 2014, 2015, 2016 Nicira, Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
14
15 #ifndef SMAP_H
16 #define SMAP_H 1
17
18 #include <netinet/in.h>
19 #include "hash.h"
20 #include "openvswitch/hmap.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 struct json;
27 struct uuid;
28
29 /* A map from string to string. */
30 struct smap {
31 struct hmap map; /* Contains "struct smap_node"s. */
32 };
33
34 struct smap_node {
35 struct hmap_node node; /* In struct smap's 'map' hmap. */
36 char *key;
37 char *value;
38 };
39
40 #define SMAP_INITIALIZER(SMAP) { HMAP_INITIALIZER(&(SMAP)->map) }
41
42 #define SMAP_FOR_EACH(SMAP_NODE, SMAP) \
43 HMAP_FOR_EACH_INIT (SMAP_NODE, node, &(SMAP)->map, \
44 BUILD_ASSERT_TYPE(SMAP_NODE, struct smap_node *), \
45 BUILD_ASSERT_TYPE(SMAP, struct smap *))
46
47 #define SMAP_FOR_EACH_SAFE(SMAP_NODE, NEXT, SMAP) \
48 HMAP_FOR_EACH_SAFE_INIT ( \
49 SMAP_NODE, NEXT, node, &(SMAP)->map, \
50 BUILD_ASSERT_TYPE(SMAP_NODE, struct smap_node *), \
51 BUILD_ASSERT_TYPE(NEXT, struct smap_node *), \
52 BUILD_ASSERT_TYPE(SMAP, struct smap *))
53
54 /* Initializer for an immutable struct smap 'SMAP' that contains one or two
55 * key-value pairs, e.g.
56 *
57 * const struct smap smap1 = SMAP_CONST1(&smap, "key", "value");
58 * const struct smap smap2 = SMAP_CONST2(&smap, "key1", "value1",
59 * "key2", "value2");
60 *
61 * An smap initialized this way must not be modified or destroyed.
62 *
63 * The 'KEY', 'K1', 'K2' arguments are evaluated multiple times.
64 */
65 #define SMAP_CONST1(SMAP, KEY, VALUE) (const struct smap) { \
66 HMAP_CONST(&(SMAP)->map, 1, SMAP_NODE(KEY, VALUE, NULL)) \
67 }
68 #define SMAP_CONST2(SMAP, K1, V1, K2, V2) (const struct smap) { \
69 HMAP_CONST(&(SMAP)->map, 2, \
70 SMAP_NODE(K1, V1, SMAP_NODE(K2, V2, NULL))) \
71 }
72 #define SMAP_NODE(KEY, VALUE, NEXT) \
73 &(struct smap_node) { \
74 .node = { \
75 .hash = hash_string(KEY, 0), \
76 .next = (NEXT), \
77 }, \
78 .key = CONST_CAST(char *, KEY), \
79 .value = CONST_CAST(char *, VALUE), \
80 }.node
81
82
83 void smap_init(struct smap *);
84 void smap_destroy(struct smap *);
85
86 struct smap_node *smap_add(struct smap *, const char *, const char *);
87 struct smap_node *smap_add_nocopy(struct smap *, char *, char *);
88 bool smap_add_once(struct smap *, const char *, const char *);
89 void smap_add_format(struct smap *, const char *key, const char *, ...)
90 OVS_PRINTF_FORMAT(3, 4);
91 void smap_add_ipv6(struct smap *, const char *, struct in6_addr *);
92 void smap_replace(struct smap *, const char *, const char *);
93
94 void smap_remove(struct smap *, const char *);
95 void smap_remove_node(struct smap *, struct smap_node *);
96 void smap_steal(struct smap *, struct smap_node *, char **keyp, char **valuep);
97 void smap_clear(struct smap *);
98
99 const char *smap_get(const struct smap *, const char *key);
100 const char *smap_get_def(const struct smap *, const char *key,
101 const char *def);
102 struct smap_node *smap_get_node(const struct smap *, const char *);
103 bool smap_get_bool(const struct smap *smap, const char *key, bool def);
104 int smap_get_int(const struct smap *smap, const char *key, int def);
105 unsigned long long int smap_get_ullong(const struct smap *, const char *key,
106 unsigned long long def);
107 bool smap_get_uuid(const struct smap *, const char *key, struct uuid *);
108
109 bool smap_is_empty(const struct smap *);
110 size_t smap_count(const struct smap *);
111
112 void smap_clone(struct smap *dst, const struct smap *src);
113 const struct smap_node **smap_sort(const struct smap *);
114
115 void smap_from_json(struct smap *, const struct json *);
116 struct json *smap_to_json(const struct smap *);
117
118 bool smap_equal(const struct smap *, const struct smap *);
119
120 #ifdef __cplusplus
121 }
122 #endif
123
124 #endif /* smap.h */