All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_integer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 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 #pragma once
19 
20 #include <aerospike/as_std.h>
21 #include <aerospike/as_util.h>
22 #include <aerospike/as_val.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /******************************************************************************
29  * TYPES
30  ******************************************************************************/
31 
32 /**
33  * Container for integer values.
34  *
35  * ## Initialization
36  *
37  * An as_integer should be initialized via one of the provided function.
38  * - as_integer_init()
39  * - as_integer_new()
40  *
41  * To initialize a stack allocated as_integer, use as_integer_init():
42  *
43  * ~~~~~~~~~~{.c}
44  * as_integer i;
45  * as_integer_init(&i, 100);
46  * ~~~~~~~~~~
47  *
48  * To create and initialize a heap allocated as_integer, use as_integer_new():
49  *
50  * ~~~~~~~~~~{.c}
51  * as_integer* i = as_integer_new(100);
52  * ~~~~~~~~~~
53  *
54  * ## Destruction
55  *
56  * When a heap allocated as_integer instance is no longer required, then you should
57  * release the resources associated with it via as_integer_destroy():
58  *
59  * ~~~~~~~~~~{.c}
60  * as_integer_destroy(i);
61  * ~~~~~~~~~~
62  *
63  * ## Usage
64  *
65  * There are two functions for getting the boxed value contained by
66  * as_integer:
67  *
68  * as_integer_get() returns the contained value. If an error occurred, then
69  * 0 (zero) is returned. Possible errors is the as_integer instance is NULL.
70  *
71  * ~~~~~~~~~~{.c}
72  * int64_t ival = as_integer_get(i);
73  * ~~~~~~~~~~
74  *
75  * as_integer_getorelse() allows you to return a default value if an error
76  * occurs:
77  *
78  * ~~~~~~~~~~{.c}
79  * int64_t ival = as_integer_getorelse(i, -1);
80  * ~~~~~~~~~~
81  *
82  * ## Conversions
83  *
84  * as_integer is derived from as_val, so it is generally safe to down cast:
85  *
86  * ~~~~~~~~~~{.c}
87  * as_val val = (as_val) i;
88  * ~~~~~~~~~~
89  *
90  * However, upcasting is more error prone. When doing so, you should use
91  * as_integer_fromval(). If conversion fails, then the return value is NULL.
92  *
93  * ~~~~~~~~~~{.c}
94  * as_integer * i = as_integer_fromval(val);
95  * ~~~~~~~~~~
96  *
97  * @extends as_val
98  * @ingroup aerospike_t
99  */
100 typedef struct as_integer_s {
101 
102  /**
103  * @private
104  * as_boolean is a subtype of as_val.
105  * You can cast as_boolean to as_val.
106  */
107  as_val _;
108 
109  /**
110  * The integer value
111  */
112  int64_t value;
113 
114 } as_integer;
115 
116 /******************************************************************************
117  * FUNCTIONS
118  ******************************************************************************/
119 
120 /**
121  * Initialize a stack allocated `as_integer` with the given integer value.
122  *
123  * ~~~~~~~~~~{.c}
124  * as_integer i;
125  * as_integer_init(&i, 123);
126  * ~~~~~~~~~~
127  *
128  * as_integer_destroy() is not required for a stack allocated as_integer.
129  *
130  * @param integer The `as_integer` to initialize.
131  * @param value The integer value.
132  *
133  * @return On success, the initialized value. Otherwise NULL.
134  *
135  * @relatesalso as_integer
136  */
137 AS_EXTERN as_integer * as_integer_init(as_integer * integer, int64_t value);
138 
139 /**
140  * Creates a new heap allocated as_integer.
141  *
142  * ~~~~~~~~~~{.c}
143  * as_integer * i = as_integer_new(123);
144  * ~~~~~~~~~~
145  *
146  * When the `as_integer` is no longer needed, you should release it an it's
147  * resources:
148  *
149  * ~~~~~~~~~~{.c}
150  * as_integer_destroy(&i);
151  * ~~~~~~~~~~
152  *
153  * @param value The integer value.
154  *
155  * @return On success, the initialized value. Otherwise NULL.
156  *
157  * @relatesalso as_integer
158  */
159 AS_EXTERN as_integer * as_integer_new(int64_t value);
160 
161 /**
162  * Destroy the `as_integer` and release resources.
163  *
164  * ~~~~~~~~~~{.c}
165  * as_integer_destroy(i);
166  * ~~~~~~~~~~
167  *
168  * @param integer The integer to destroy.
169  *
170  * @relatesalso as_integer
171  */
172 static inline void as_integer_destroy(as_integer * integer) {
173  as_val_destroy((as_val *) integer);
174 }
175 
176 /******************************************************************************
177  * VALUE FUNCTIONS
178  ******************************************************************************/
179 
180 /**
181  * Get the int64_t value. If integer is NULL, then return the fallback value.
182  *
183  * @relatesalso as_integer
184  */
185 static inline int64_t as_integer_getorelse(const as_integer * integer, int64_t fallback) {
186  return integer ? integer->value : fallback;
187 }
188 
189 /**
190  * Get the int64_t value.
191  *
192  * @relatesalso as_integer
193  */
194 static inline int64_t as_integer_get(const as_integer * integer) {
195  return as_integer_getorelse(integer, 0);
196 }
197 
198 /**
199  * Get the int64_t value.
200  * @deprecated Use as_integer_get() instead.
201  *
202  * @relatesalso as_integer
203  */
204 static inline int64_t as_integer_toint(const as_integer * integer) {
205  return as_integer_getorelse(integer, 0);
206 }
207 
208 /******************************************************************************
209  * CONVERSION FUNCTIONS
210  ******************************************************************************/
211 
212 /**
213  * Convert to an as_val.
214  *
215  * @relatesalso as_integer
216  */
217 static inline as_val * as_integer_toval(const as_integer * i) {
218  return (as_val *) i;
219 }
220 
221 /**
222  * Convert from an as_val.
223  *
224  * @relatesalso as_integer
225  */
226 static inline as_integer * as_integer_fromval(const as_val * v) {
228 }
229 
230 /******************************************************************************
231  * as_val FUNCTIONS
232  ******************************************************************************/
233 
234 /**
235  * @private
236  * Internal helper function for destroying an as_val.
237  */
239 
240 /**
241  * @private
242  * Internal helper function for getting the hashcode of an as_val.
243  */
244 AS_EXTERN uint32_t as_integer_val_hashcode(const as_val * v);
245 
246 /**
247  * @private
248  * Internal helper function for getting the string representation of an as_val.
249  */
250 AS_EXTERN char * as_integer_val_tostring(const as_val * v);
251 
252 #ifdef __cplusplus
253 } // end extern "C"
254 #endif
static as_integer * as_integer_fromval(const as_val *v)
Definition: as_integer.h:226
static int64_t as_integer_getorelse(const as_integer *integer, int64_t fallback)
Definition: as_integer.h:185
static void as_integer_destroy(as_integer *integer)
Definition: as_integer.h:172
#define as_util_fromval(object, type_id, type)
Definition: as_util.h:43
static int64_t as_integer_toint(const as_integer *integer)
Definition: as_integer.h:204
AS_EXTERN char * as_integer_val_tostring(const as_val *v)
Definition: as_val.h:61
AS_EXTERN void as_integer_val_destroy(as_val *v)
#define AS_EXTERN
Definition: as_std.h:25
AS_EXTERN uint32_t as_integer_val_hashcode(const as_val *v)
int64_t value
Definition: as_integer.h:112
AS_EXTERN as_integer * as_integer_init(as_integer *integer, int64_t value)
#define as_val_destroy(__v)
Definition: as_val.h:114
AS_EXTERN as_integer * as_integer_new(int64_t value)
static int64_t as_integer_get(const as_integer *integer)
Definition: as_integer.h:194
static as_val * as_integer_toval(const as_integer *i)
Definition: as_integer.h:217