All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_val.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 
18 #pragma once
19 
20 #include <citrusleaf/cf_atomic.h>
21 
22 #include <stdbool.h>
23 #include <stdint.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /******************************************************************************
30  * TYPES
31  *****************************************************************************/
32 
33 /**
34  * as_val types
35  */
36 typedef enum as_val_t {
37  AS_UNDEF = 0,
38  AS_UNKNOWN = 0, //<! @deprecated
39  AS_NIL = 1,
40  AS_BOOLEAN = 2,
41  AS_INTEGER = 3,
42  AS_STRING = 4,
43  AS_LIST = 5,
44  AS_MAP = 6,
45  AS_REC = 7,
46  AS_PAIR = 8,
47  AS_BYTES = 9,
48  AS_DOUBLE = 10,
49  AS_GEOJSON = 11,
51 } __attribute__((packed)) as_val_t;
52 
53 /**
54  * Represents a value
55  * @ingroup aerospike_t
56  */
57 typedef struct as_val_s {
58 
59  /**
60  * Value type
61  */
62  enum as_val_t type;
63 
64  /**
65  * Value can be freed.
66  * Should be false for stack allocated values.
67  */
68  bool free;
69 
70  /**
71  * Reference count
72  * Values are ref counted.
73  * To increment the count, use `as_val_reserve()`
74  */
75  cf_atomic32 count;
76 
77 } as_val;
78 
79 /******************************************************************************
80  * MACROS
81  *****************************************************************************/
82 
83 /**
84  * Returns the `as_val.type` of a value.
85  *
86  * @param __v The `as_val` to get the type of
87  *
88  * @return An as_val_t value. If the type is unknown, then it will
89  * be AS_UNKNOWN.
90  */
91 #define as_val_type(__v) (__v ? ((as_val *)__v)->type : AS_UNDEF)
92 
93 /**
94  * Increment the `as_val.count` of a value.
95  *
96  * @param __v The `as_val` to be incremented.
97  *
98  * @return The value, with it's refcount incremented.
99  */
100 #define as_val_reserve(__v) ( as_val_val_reserve((as_val *)__v) )
101 
102 /**
103  * Decrement the `as_val.count` of a value. If `as_val.count` reaches 0 (zero) and
104  * `as_val.free` is true, then free the `as_val` instance.
105  *
106  * @param __v The `as_val` to be decremented.
107  *
108  * @return The value, if its `as_val.count` > 0. Otherwise NULL.
109  */
110 #define as_val_destroy(__v) ( as_val_val_destroy((as_val *)__v) )
111 
112 /**
113  * Get the hashcode value for the value.
114  *
115  * @param __v The `as_val` to get the hashcode value for.
116  *
117  * @return The hashcode value.
118  */
119 #define as_val_hashcode(__v) ( as_val_val_hashcode((as_val *)__v) )
120 
121 /**
122  * Get the string representation of the value.
123  *
124  * @param __v The `as_val` to get the string value for.
125  *
126  * @return The string representation on success. Otherwise NULL.
127  */
128 #define as_val_tostring(__v) ( as_val_val_tostring((as_val *)__v) )
129 
130 /******************************************************************************
131  * FUNCTIONS
132  *****************************************************************************/
133 
134 /**
135  * @private
136  * Helper function for incrementing the count of a value.
137  */
139 
140 /**
141  * @private
142  * Helper function for decrementing the count of a value,
143  * and if count==0 and free==true, then free the value.
144  */
146 
147 /**
148  * @private
149  * Helper function for calculating the hash value.
150  */
151 uint32_t as_val_val_hashcode(const as_val *);
152 
153 /**
154  * @private
155  * Helper function for generating the string representation.
156  */
157 char * as_val_val_tostring(const as_val *);
158 
159 /******************************************************************************
160  * INSTANCE FUNCTIONS
161  *****************************************************************************/
162 
163 /**
164  * @private
165  * Initialize an as_val.
166  * Should only be used by subtypes.
167  * @deprecated Use as_val_cons() instead.
168  */
169 static inline void as_val_init(as_val * v, as_val_t type, bool free)
170 {
171  v->type = type;
172  v->free = free;
173  v->count = 1;
174 }
175 
176 
177 /**
178  * @private
179  * Initialize an as_val.
180  * Should only be used by subtypes.
181  */
182 static inline as_val * as_val_cons(as_val * val, as_val_t type, bool free)
183 {
184  if ( !val ) return val;
185 
186  val->type = type;
187  val->free = free;
188  val->count = 1;
189  return val;
190 }
191 
192 #ifdef __cplusplus
193 } // end extern "C"
194 #endif