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