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