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