All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_boolean.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  * Boolean value.
34  *
35  * To use the boolean value, you should use one of the two constants:
36  *
37  * as_boolean as_true;
38  * as_boolean as_false;
39  *
40  * Both `as_boolean_init()` and `as_boolean_new()` should be used sparingly.
41  *
42  * @extends as_val
43  * @ingroup aerospike_t
44  */
45 typedef struct as_boolean_s {
46 
47  /**
48  * @private
49  * as_boolean is a subtype of as_val.
50  * You can cast as_boolean to as_val.
51  */
52  as_val _;
53 
54  /**
55  * The boolean value.
56  */
57  bool value;
58 
59 } as_boolean;
60 
61 /******************************************************************************
62  * CONSTANTS
63  *****************************************************************************/
64 
65 /**
66  * True value.
67  *
68  * Use this when you need to use an `as_boolean` containing `true`,
69  * rather than allocating a new `as_boolean`.
70  */
71 AS_EXTERN extern const as_boolean as_true;
72 
73 /**
74  * False value.
75  *
76  * Use this when you need to use an `as_boolean` containing `true`,
77  * rather than allocating a new `as_boolean`.
78  */
79 AS_EXTERN extern const as_boolean as_false;
80 
81 /******************************************************************************
82  * INSTANCE FUNCTIONS
83  ******************************************************************************/
84 
85 /**
86  * Initialize a stack allocated `as_boolean` with the given boolean value.
87  *
88  * @param boolean The `as_boolean` to initialize.
89  * @param value The bool value.
90  *
91  * @return On success, the initialized value. Otherwise NULL.
92  *
93  * @relatesalso as_boolean
94  */
95 AS_EXTERN as_boolean * as_boolean_init(as_boolean * boolean, bool value);
96 
97 /**
98  * Creates a new heap allocated `as_boolean` and initializes with
99  * the given boolean value.
100  *
101  * @param value The bool value.
102  *
103  * @return On success, the newly allocated value. Otherwise NULL.
104  *
105  * @relatesalso as_boolean
106  */
107 AS_EXTERN as_boolean * as_boolean_new(bool value);
108 
109 /**
110  * Destroy the `as_boolean` and release associated resources.
111  *
112  * @param boolean The `as_boolean` to destroy.
113  *
114  * @relatesalso as_boolean
115  */
116 static inline void as_boolean_destroy(as_boolean * boolean) {
117  as_val_destroy((as_val *) boolean);
118 }
119 
120 /******************************************************************************
121  * VALUE FUNCTIONS
122  ******************************************************************************/
123 
124 /**
125  * Get the bool value. If boolean is NULL, then return the fallback value.
126  *
127  * @relatesalso as_boolean
128  */
129 static inline bool as_boolean_getorelse(const as_boolean * boolean, bool fallback) {
130  return boolean ? boolean->value : fallback;
131 }
132 
133 /**
134  * Get the bool value.
135  *
136  * @relatesalso as_boolean
137  */
138 static inline bool as_boolean_get(const as_boolean * boolean) {
139  return as_boolean_getorelse(boolean, false);
140 }
141 
142 /**
143  * Get the bool value.
144  * @deprecated Use as_boolean_get() instead.
145  *
146  * @relatesalso as_boolean
147  */
148 static inline bool as_boolean_tobool(const as_boolean * boolean) {
149  return as_boolean_getorelse(boolean, false);
150 }
151 
152 /******************************************************************************
153  * CONVERSION FUNCTIONS
154  *****************************************************************************/
155 
156 /**
157  * Convert to an as_val.
158  *
159  * @relatesalso as_boolean
160  */
161 static inline as_val * as_boolean_toval(const as_boolean * boolean) {
162  return (as_val *) boolean;
163 }
164 
165 /**
166  * Convert from an as_val.
167  *
168  * @relatesalso as_boolean
169  */
170 static inline as_boolean * as_boolean_fromval(const as_val * v) {
172 }
173 
174 /******************************************************************************
175  * as_val FUNCTIONS
176  *****************************************************************************/
177 
178 /**
179  * @private
180  * Internal helper function for destroying an as_val.
181  */
183 
184 /**
185  * @private
186  * Internal helper function for getting the hashcode of an as_val.
187  */
188 AS_EXTERN uint32_t as_boolean_val_hashcode(const as_val * v);
189 
190 /**
191  * @private
192  * Internal helper function for getting the string representation of an as_val.
193  */
194 AS_EXTERN char * as_boolean_val_tostring(const as_val * v);
195 
196 #ifdef __cplusplus
197 } // end extern "C"
198 #endif
AS_EXTERN as_boolean * as_boolean_new(bool value)
AS_EXTERN const as_boolean as_false
#define as_util_fromval(object, type_id, type)
Definition: as_util.h:43
Definition: as_val.h:61
#define AS_EXTERN
Definition: as_std.h:25
static bool as_boolean_tobool(const as_boolean *boolean)
Definition: as_boolean.h:148
static void as_boolean_destroy(as_boolean *boolean)
Definition: as_boolean.h:116
bool value
Definition: as_boolean.h:57
static as_val * as_boolean_toval(const as_boolean *boolean)
Definition: as_boolean.h:161
AS_EXTERN uint32_t as_boolean_val_hashcode(const as_val *v)
AS_EXTERN const as_boolean as_true
static as_boolean * as_boolean_fromval(const as_val *v)
Definition: as_boolean.h:170
AS_EXTERN char * as_boolean_val_tostring(const as_val *v)
static bool as_boolean_getorelse(const as_boolean *boolean, bool fallback)
Definition: as_boolean.h:129
#define as_val_destroy(__v)
Definition: as_val.h:114
AS_EXTERN as_boolean * as_boolean_init(as_boolean *boolean, bool value)
AS_EXTERN void as_boolean_val_destroy(as_val *v)
static bool as_boolean_get(const as_boolean *boolean)
Definition: as_boolean.h:138