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-2015 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 NULL terminated string.
66  */
67 static inline int as_stringmap_set_str(as_map * m, const char * k, const char * v)
68 {
69  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) as_string_new_strdup(v));
70 }
71 
72 /**
73  * Set the specified key's value to an as_integer.
74  */
75 static inline int as_stringmap_set_integer(as_map * m, const char * k, as_integer * v)
76 {
77  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
78 }
79 
80 /**
81  * Set the specified key's value to an as_string.
82  */
83 static inline int as_stringmap_set_string(as_map * m, const char * k, as_string * 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_bytes.
90  */
91 static inline int as_stringmap_set_bytes(as_map * m, const char * k, as_bytes * 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_list.
98  */
99 static inline int as_stringmap_set_list(as_map * m, const char * k, as_list * 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_map.
106  */
107 static inline int as_stringmap_set_map(as_map * m, const char * k, as_map * v)
108 {
109  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
110 }
111 
112 /******************************************************************************
113  * GETTER FUNCTIONS
114  *****************************************************************************/
115 
116 /**
117  * Get the specified key's value as an as_val.
118  */
119 static inline as_val * as_stringmap_get(as_map * m, const char * k)
120 {
121  as_string key;
122  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
123  return v;
124 }
125 
126 /**
127  * Get the specified key's value as an int64_t.
128  */
129 static inline int64_t as_stringmap_get_int64(as_map * m, const char * k)
130 {
131  as_string key;
132  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
134  return i ? as_integer_toint(i) : 0;
135 }
136 
137 /**
138  * Get the specified key's value as a NULL terminated string.
139  */
140 static inline char * as_stringmap_get_str(as_map * m, const char * k)
141 {
142  as_string key;
143  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
144  as_string * s = as_string_fromval(v);
145  return s ? as_string_tostring(s) : NULL;
146 }
147 
148 /**
149  * Get the specified key's value as an as_integer.
150  */
151 static inline as_integer * as_stringmap_get_integer(as_map * m, const char * k)
152 {
153  as_string key;
154  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
155  return as_integer_fromval(v);
156 }
157 
158 /**
159  * Get the specified key's value as an as_string.
160  */
161 static inline as_string * as_stringmap_get_string(as_map * m, const char * k)
162 {
163  as_string key;
164  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
165  return as_string_fromval(v);
166 }
167 
168 /**
169  * Get the specified key's value as an as_bytes.
170  */
171 static inline as_bytes * as_stringmap_get_bytes(as_map * m, const char * k)
172 {
173  as_string key;
174  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
175  return as_bytes_fromval(v);
176 }
177 
178 /**
179  * Get the specified key's value as an as_list.
180  */
181 static inline as_list * as_stringmap_get_list(as_map * m, const char * k)
182 {
183  as_string key;
184  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
185  return as_list_fromval(v);
186 }
187 
188 /**
189  * Get the specified key's value as an as_map.
190  */
191 static inline as_map * as_stringmap_get_map(as_map * m, const char * k)
192 {
193  as_string key;
194  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
195  return as_map_fromval(v);
196 }
197 
198 /**
199  * @}
200  */
201 
202 #ifdef __cplusplus
203 } // end extern "C"
204 #endif
static as_integer * as_integer_fromval(const as_val *v)
Definition: as_integer.h:234
as_msg m
Definition: as_proto.h:796
static char * as_stringmap_get_str(as_map *m, const char *k)
Definition: as_stringmap.h:140
static as_integer * as_stringmap_get_integer(as_map *m, const char *k)
Definition: as_stringmap.h:151
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 int64_t as_integer_toint(const as_integer *integer)
Definition: as_integer.h:212
as_integer * as_integer_new(int64_t value)
Definition: as_val.h:55
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:67
static int64_t as_stringmap_get_int64(as_map *m, const char *k)
Definition: as_stringmap.h:129
#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:161
static int as_stringmap_set(as_map *m, const char *k, as_val *v)
Definition: as_stringmap.h:51
static as_bytes * as_bytes_fromval(const as_val *v)
Definition: as_bytes.h:912
static char * as_string_tostring(const as_string *string)
Definition: as_string.h:260
static as_string * as_string_fromval(const as_val *v)
Definition: as_string.h:296
static as_bytes * as_stringmap_get_bytes(as_map *m, const char *k)
Definition: as_stringmap.h:171
static int as_stringmap_set_list(as_map *m, const char *k, as_list *v)
Definition: as_stringmap.h:99
static as_list * as_list_fromval(as_val *v)
Definition: as_list.h:1272
static int as_stringmap_set_string(as_map *m, const char *k, as_string *v)
Definition: as_stringmap.h:83
static int as_stringmap_set_integer(as_map *m, const char *k, as_integer *v)
Definition: as_stringmap.h:75
static as_list * as_stringmap_get_list(as_map *m, const char *k)
Definition: as_stringmap.h:181
static int as_stringmap_set_map(as_map *m, const char *k, as_map *v)
Definition: as_stringmap.h:107
static as_map * as_map_fromval(const as_val *val)
Definition: as_map.h:406
static int as_stringmap_set_bytes(as_map *m, const char *k, as_bytes *v)
Definition: as_stringmap.h:91
static as_val * as_stringmap_get(as_map *m, const char *k)
Definition: as_stringmap.h:119
static as_map * as_stringmap_get_map(as_map *m, const char *k)
Definition: as_stringmap.h:191
as_string * as_string_init(as_string *string, char *value, bool free)