All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
modules/common/target/Linux-x86_64/include/aerospike/as_stringmap.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2008-2013 by Aerospike.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  *****************************************************************************/
22 
23 /**
24  * as_stringmap provides a convenience interface for populating a map with
25  * string keys.
26  *
27  * @addtogroup stringmap_t StringMap
28  * @{
29  */
30 
31 #pragma once
32 
33 #include <aerospike/as_util.h>
34 #include <aerospike/as_val.h>
35 #include <aerospike/as_integer.h>
36 #include <aerospike/as_string.h>
37 #include <aerospike/as_bytes.h>
38 #include <aerospike/as_list.h>
39 #include <aerospike/as_map.h>
40 
41 #include <stdbool.h>
42 #include <stdint.h>
43 #include <string.h>
44 
45 /******************************************************************************
46  * SETTER FUNCTIONS
47  *****************************************************************************/
48 
49 /**
50  * Set the specified key's value to an as_val.
51  */
52 static inline int as_stringmap_set(as_map * m, const char * k, as_val * v)
53 {
54  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), v);
55 }
56 
57 /**
58  * Set the specified key's value to an int64_t.
59  */
60 static inline int as_stringmap_set_int64(as_map * m, const char * k, int64_t v)
61 {
62  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) as_integer_new(v));
63 }
64 
65 /**
66  * Set the specified key's value to a NULL terminated string.
67  */
68 static inline int as_stringmap_set_str(as_map * m, const char * k, const char * v)
69 {
70  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) as_string_new_strdup(v));
71 }
72 
73 /**
74  * Set the specified key's value to an as_integer.
75  */
76 static inline int as_stringmap_set_integer(as_map * m, const char * k, as_integer * v)
77 {
78  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
79 }
80 
81 /**
82  * Set the specified key's value to an as_string.
83  */
84 static inline int as_stringmap_set_string(as_map * m, const char * k, as_string * v)
85 {
86  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
87 }
88 
89 /**
90  * Set the specified key's value to an as_bytes.
91  */
92 static inline int as_stringmap_set_bytes(as_map * m, const char * k, as_bytes * v)
93 {
94  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
95 }
96 
97 /**
98  * Set the specified key's value to an as_list.
99  */
100 static inline int as_stringmap_set_list(as_map * m, const char * k, as_list * v)
101 {
102  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
103 }
104 
105 /**
106  * Set the specified key's value to an as_map.
107  */
108 static inline int as_stringmap_set_map(as_map * m, const char * k, as_map * v)
109 {
110  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
111 }
112 
113 /******************************************************************************
114  * GETTER FUNCTIONS
115  *****************************************************************************/
116 
117 /**
118  * Get the specified key's value as an as_val.
119  */
120 static inline as_val * as_stringmap_get(as_map * m, const char * k)
121 {
122  as_string key;
123  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
124  return v;
125 }
126 
127 /**
128  * Get the specified key's value as an int64_t.
129  */
130 static inline int64_t as_stringmap_get_int64(as_map * m, const char * k)
131 {
132  as_string key;
133  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
135  return i ? as_integer_toint(i) : 0;
136 }
137 
138 /**
139  * Get the specified key's value as a NULL terminated string.
140  */
141 static inline char * as_stringmap_get_str(as_map * m, const char * k)
142 {
143  as_string key;
144  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
145  as_string * s = as_string_fromval(v);
146  return s ? as_string_tostring(s) : NULL;
147 }
148 
149 /**
150  * Get the specified key's value as an as_integer.
151  */
152 static inline as_integer * as_stringmap_get_integer(as_map * m, const char * k)
153 {
154  as_string key;
155  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
156  return as_integer_fromval(v);
157 }
158 
159 /**
160  * Get the specified key's value as an as_string.
161  */
162 static inline as_string * as_stringmap_get_string(as_map * m, const char * k)
163 {
164  as_string key;
165  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
166  return as_string_fromval(v);
167 }
168 
169 /**
170  * Get the specified key's value as an as_bytes.
171  */
172 static inline as_bytes * as_stringmap_get_bytes(as_map * m, const char * k)
173 {
174  as_string key;
175  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
176  return as_bytes_fromval(v);
177 }
178 
179 /**
180  * Get the specified key's value as an as_list.
181  */
182 static inline as_list * as_stringmap_get_list(as_map * m, const char * k)
183 {
184  as_string key;
185  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
186  return as_list_fromval(v);
187 }
188 
189 /**
190  * Get the specified key's value as an as_map.
191  */
192 static inline as_map * as_stringmap_get_map(as_map * m, const char * k)
193 {
194  as_string key;
195  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
196  return as_map_fromval(v);
197 }
198 
199 /**
200  * @}
201  */