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