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-2014 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 /******************************************************************************
41  * SETTER FUNCTIONS
42  *****************************************************************************/
43 
44 /**
45  * Set the specified key's value to an as_val.
46  */
47 static inline int as_stringmap_set(as_map * m, const char * k, as_val * v)
48 {
49  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), v);
50 }
51 
52 /**
53  * Set the specified key's value to an int64_t.
54  */
55 static inline int as_stringmap_set_int64(as_map * m, const char * k, int64_t v)
56 {
57  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) as_integer_new(v));
58 }
59 
60 /**
61  * Set the specified key's value to a NULL terminated string.
62  */
63 static inline int as_stringmap_set_str(as_map * m, const char * k, const char * v)
64 {
65  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) as_string_new_strdup(v));
66 }
67 
68 /**
69  * Set the specified key's value to an as_integer.
70  */
71 static inline int as_stringmap_set_integer(as_map * m, const char * k, as_integer * v)
72 {
73  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
74 }
75 
76 /**
77  * Set the specified key's value to an as_string.
78  */
79 static inline int as_stringmap_set_string(as_map * m, const char * k, as_string * v)
80 {
81  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
82 }
83 
84 /**
85  * Set the specified key's value to an as_bytes.
86  */
87 static inline int as_stringmap_set_bytes(as_map * m, const char * k, as_bytes * v)
88 {
89  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
90 }
91 
92 /**
93  * Set the specified key's value to an as_list.
94  */
95 static inline int as_stringmap_set_list(as_map * m, const char * k, as_list * v)
96 {
97  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
98 }
99 
100 /**
101  * Set the specified key's value to an as_map.
102  */
103 static inline int as_stringmap_set_map(as_map * m, const char * k, as_map * v)
104 {
105  return as_util_hook(set, 1, m, (as_val *) as_string_new_strdup(k), (as_val *) v);
106 }
107 
108 /******************************************************************************
109  * GETTER FUNCTIONS
110  *****************************************************************************/
111 
112 /**
113  * Get the specified key's value as an as_val.
114  */
115 static inline as_val * as_stringmap_get(as_map * m, const char * k)
116 {
117  as_string key;
118  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
119  return v;
120 }
121 
122 /**
123  * Get the specified key's value as an int64_t.
124  */
125 static inline int64_t as_stringmap_get_int64(as_map * m, const char * k)
126 {
127  as_string key;
128  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
130  return i ? as_integer_toint(i) : 0;
131 }
132 
133 /**
134  * Get the specified key's value as a NULL terminated string.
135  */
136 static inline char * as_stringmap_get_str(as_map * m, const char * k)
137 {
138  as_string key;
139  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
140  as_string * s = as_string_fromval(v);
141  return s ? as_string_tostring(s) : NULL;
142 }
143 
144 /**
145  * Get the specified key's value as an as_integer.
146  */
147 static inline as_integer * as_stringmap_get_integer(as_map * m, const char * k)
148 {
149  as_string key;
150  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
151  return as_integer_fromval(v);
152 }
153 
154 /**
155  * Get the specified key's value as an as_string.
156  */
157 static inline as_string * as_stringmap_get_string(as_map * m, const char * k)
158 {
159  as_string key;
160  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
161  return as_string_fromval(v);
162 }
163 
164 /**
165  * Get the specified key's value as an as_bytes.
166  */
167 static inline as_bytes * as_stringmap_get_bytes(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  return as_bytes_fromval(v);
172 }
173 
174 /**
175  * Get the specified key's value as an as_list.
176  */
177 static inline as_list * as_stringmap_get_list(as_map * m, const char * k)
178 {
179  as_string key;
180  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
181  return as_list_fromval(v);
182 }
183 
184 /**
185  * Get the specified key's value as an as_map.
186  */
187 static inline as_map * as_stringmap_get_map(as_map * m, const char * k)
188 {
189  as_string key;
190  as_val * v = as_util_hook(get, NULL, m, (as_val *) as_string_init(&key, (char *) k, false));
191  return as_map_fromval(v);
192 }
193 
194 /**
195  * @}
196  */
static as_integer * as_integer_fromval(const as_val *v)
Definition: as_integer.h:230
static char * as_stringmap_get_str(as_map *m, const char *k)
Definition: as_stringmap.h:136
static as_integer * as_stringmap_get_integer(as_map *m, const char *k)
Definition: as_stringmap.h:147
static int as_stringmap_set_int64(as_map *m, const char *k, int64_t v)
Definition: as_stringmap.h:55
Definition: as_map.h:57
static int64_t as_integer_toint(const as_integer *integer)
Definition: as_integer.h:208
as_integer * as_integer_new(int64_t value)
Definition: as_val.h:51
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:63
static int64_t as_stringmap_get_int64(as_map *m, const char *k)
Definition: as_stringmap.h:125
#define as_util_hook(hook, default, object, args...)
Definition: as_util.h:32
static as_string * as_stringmap_get_string(as_map *m, const char *k)
Definition: as_stringmap.h:157
static int as_stringmap_set(as_map *m, const char *k, as_val *v)
Definition: as_stringmap.h:47
static as_bytes * as_bytes_fromval(const as_val *v)
Definition: as_bytes.h:911
static char * as_string_tostring(const as_string *string)
Definition: as_string.h:225
static as_string * as_string_fromval(const as_val *v)
Definition: as_string.h:261
static as_bytes * as_stringmap_get_bytes(as_map *m, const char *k)
Definition: as_stringmap.h:167
static int as_stringmap_set_list(as_map *m, const char *k, as_list *v)
Definition: as_stringmap.h:95
static as_list * as_list_fromval(as_val *v)
Definition: as_list.h:1269
static int as_stringmap_set_string(as_map *m, const char *k, as_string *v)
Definition: as_stringmap.h:79
static int as_stringmap_set_integer(as_map *m, const char *k, as_integer *v)
Definition: as_stringmap.h:71
static as_list * as_stringmap_get_list(as_map *m, const char *k)
Definition: as_stringmap.h:177
static int as_stringmap_set_map(as_map *m, const char *k, as_map *v)
Definition: as_stringmap.h:103
static as_map * as_map_fromval(const as_val *val)
Definition: as_map.h:402
static int as_stringmap_set_bytes(as_map *m, const char *k, as_bytes *v)
Definition: as_stringmap.h:87
static as_val * as_stringmap_get(as_map *m, const char *k)
Definition: as_stringmap.h:115
static as_map * as_stringmap_get_map(as_map *m, const char *k)
Definition: as_stringmap.h:187
as_string * as_string_init(as_string *string, char *value, bool free)