All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_stringmap.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2016 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 
18 /**
19  * as_stringmap provides a convenience interface for populating a map with
20  * string keys.
21  *
22  * @addtogroup stringmap_t StringMap
23  * @{
24  */
25 
26 #pragma once
27 
28 #include <aerospike/as_util.h>
29 #include <aerospike/as_val.h>
30 #include <aerospike/as_integer.h>
31 #include <aerospike/as_string.h>
32 #include <aerospike/as_bytes.h>
33 #include <aerospike/as_list.h>
34 #include <aerospike/as_map.h>
35 
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <string.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /******************************************************************************
45  * SETTER FUNCTIONS
46  *****************************************************************************/
47 
48 /**
49  * Set the specified key's value to an as_val.
50  */
51 static inline int as_stringmap_set(as_map * m, const char * k, as_val * v)
52 {
53  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), v);
54 }
55 
56 /**
57  * Set the specified key's value to an int64_t.
58  */
59 static inline int as_stringmap_set_int64(as_map * m, const char * k, int64_t v)
60 {
61  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) as_integer_new(v));
62 }
63 
64 /**
65  * Set the specified key's value to a double.
66  */
67 static inline int as_stringmap_set_double(as_map * m, const char * k, double v)
68 {
69  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) as_double_new(v));
70 }
71 
72 /**
73  * Set the specified key's value to a NULL terminated string.
74  */
75 static inline int as_stringmap_set_str(as_map * m, const char * k, const char * v)
76 {
77  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) as_string_new_strdup(v));
78 }
79 
80 /**
81  * Set the specified key's value to an as_integer.
82  */
83 static inline int as_stringmap_set_integer(as_map * m, const char * k, as_integer * v)
84 {
85  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
86 }
87 
88 /**
89  * Set the specified key's value to an as_integer.
90  */
91 static inline int as_stringmap_set_as_double(as_map * m, const char * k, as_double * v)
92 {
93  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
94 }
95 
96 /**
97  * Set the specified key's value to an as_string.
98  */
99 static inline int as_stringmap_set_string(as_map * m, const char * k, as_string * v)
100 {
101  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
102 }
103 
104 /**
105  * Set the specified key's value to an as_bytes.
106  */
107 static inline int as_stringmap_set_bytes(as_map * m, const char * k, as_bytes * v)
108 {
109  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
110 }
111 
112 /**
113  * Set the specified key's value to an as_list.
114  */
115 static inline int as_stringmap_set_list(as_map * m, const char * k, as_list * v)
116 {
117  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
118 }
119 
120 /**
121  * Set the specified key's value to an as_map.
122  */
123 static inline int as_stringmap_set_map(as_map * m, const char * k, as_map * v)
124 {
125  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
126 }
127 
128 /******************************************************************************
129  * GETTER FUNCTIONS
130  *****************************************************************************/
131 
132 /**
133  * Get the specified key's value as an as_val.
134  */
135 static inline as_val * as_stringmap_get(as_map * m, const char * k)
136 {
137  as_string key;
138  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
139  return v;
140 }
141 
142 /**
143  * Get the specified key's value as an int64_t.
144  */
145 static inline int64_t as_stringmap_get_int64(as_map * m, const char * k)
146 {
147  as_string key;
148  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
150  return i ? as_integer_toint(i) : 0;
151 }
152 
153 /**
154  * Get the specified key's value as a double.
155  */
156 static inline double as_stringmap_get_double(as_map * m, const char * k)
157 {
158  as_string key;
159  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
160  as_double * ptr = as_double_fromval(v);
161  return ptr ? ptr->value : 0.0;
162 }
163 
164 /**
165  * Get the specified key's value as a NULL terminated string.
166  */
167 static inline char * as_stringmap_get_str(as_map * m, const char * k)
168 {
169  as_string key;
170  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
171  as_string * s = as_string_fromval(v);
172  return s ? as_string_tostring(s) : NULL;
173 }
174 
175 /**
176  * Get the specified key's value as an as_integer.
177  */
178 static inline as_integer * as_stringmap_get_integer(as_map * m, const char * k)
179 {
180  as_string key;
181  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
182  return as_integer_fromval(v);
183 }
184 
185 /**
186  * Get the specified key's value as an as_double.
187  */
188 static inline as_double * as_stringmap_get_as_double(as_map * m, const char * k)
189 {
190  as_string key;
191  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
192  return as_double_fromval(v);
193 }
194 
195 /**
196  * Get the specified key's value as an as_string.
197  */
198 static inline as_string * as_stringmap_get_string(as_map * m, const char * k)
199 {
200  as_string key;
201  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
202  return as_string_fromval(v);
203 }
204 
205 /**
206  * Get the specified key's value as an as_bytes.
207  */
208 static inline as_bytes * as_stringmap_get_bytes(as_map * m, const char * k)
209 {
210  as_string key;
211  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
212  return as_bytes_fromval(v);
213 }
214 
215 /**
216  * Get the specified key's value as an as_list.
217  */
218 static inline as_list * as_stringmap_get_list(as_map * m, const char * k)
219 {
220  as_string key;
221  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
222  return as_list_fromval(v);
223 }
224 
225 /**
226  * Get the specified key's value as an as_map.
227  */
228 static inline as_map * as_stringmap_get_map(as_map * m, const char * k)
229 {
230  as_string key;
231  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
232  return as_map_fromval(v);
233 }
234 
235 /**
236  * @}
237  */
238 
239 #ifdef __cplusplus
240 } // end extern "C"
241 #endif
static as_integer * as_integer_fromval(const as_val *v)
Definition: as_integer.h:234
as_msg m
Definition: as_proto.h:893
static char * as_stringmap_get_str(as_map *m, const char *k)
Definition: as_stringmap.h:167
static as_double * as_stringmap_get_as_double(as_map *m, const char *k)
Definition: as_stringmap.h:188
static as_integer * as_stringmap_get_integer(as_map *m, const char *k)
Definition: as_stringmap.h:178
static int as_stringmap_set_int64(as_map *m, const char *k, int64_t v)
Definition: as_stringmap.h:59
Definition: as_map.h:61
static double as_stringmap_get_double(as_map *m, const char *k)
Definition: as_stringmap.h:156
static as_double * as_double_fromval(const as_val *value)
Definition: as_double.h:229
static int64_t as_integer_toint(const as_integer *integer)
Definition: as_integer.h:212
double value
Definition: as_double.h:109
as_integer * as_integer_new(int64_t value)
Definition: as_val.h:57
as_string * as_string_new_strdup(const char *value)
static int as_stringmap_set_str(as_map *m, const char *k, const char *v)
Definition: as_stringmap.h:75
static int as_stringmap_set_double(as_map *m, const char *k, double v)
Definition: as_stringmap.h:67
static int64_t as_stringmap_get_int64(as_map *m, const char *k)
Definition: as_stringmap.h:145
#define as_util_hook(hook, default, object, args...)
Definition: as_util.h:36
static as_string * as_stringmap_get_string(as_map *m, const char *k)
Definition: as_stringmap.h:198
static int as_stringmap_set(as_map *m, const char *k, as_val *v)
Definition: as_stringmap.h:51
static int as_stringmap_set_as_double(as_map *m, const char *k, as_double *v)
Definition: as_stringmap.h:91
static as_bytes * as_bytes_fromval(const as_val *v)
Definition: as_bytes.h:970
static char * as_string_tostring(const as_string *string)
Definition: as_string.h:258
static as_string * as_string_fromval(const as_val *v)
Definition: as_string.h:294
static as_bytes * as_stringmap_get_bytes(as_map *m, const char *k)
Definition: as_stringmap.h:208
static int as_stringmap_set_list(as_map *m, const char *k, as_list *v)
Definition: as_stringmap.h:115
static as_list * as_list_fromval(as_val *v)
Definition: as_list.h:1469
static int as_stringmap_set_string(as_map *m, const char *k, as_string *v)
Definition: as_stringmap.h:99
static int as_stringmap_set_integer(as_map *m, const char *k, as_integer *v)
Definition: as_stringmap.h:83
static as_list * as_stringmap_get_list(as_map *m, const char *k)
Definition: as_stringmap.h:218
static int as_stringmap_set_map(as_map *m, const char *k, as_map *v)
Definition: as_stringmap.h:123
static as_map * as_map_fromval(const as_val *val)
Definition: as_map.h:404
as_double * as_double_new(double value)
static int as_stringmap_set_bytes(as_map *m, const char *k, as_bytes *v)
Definition: as_stringmap.h:107
static as_val * as_stringmap_get(as_map *m, const char *k)
Definition: as_stringmap.h:135
static as_map * as_stringmap_get_map(as_map *m, const char *k)
Definition: as_stringmap.h:228
as_string * as_string_init(as_string *string, char *value, bool free)