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