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