Qpid Proton C API  0.24.0
object.h
1 #ifndef PROTON_OBJECT_H
2 #define PROTON_OBJECT_H 1
3 
4 /*
5  *
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  *
23  */
24 
25 #include <proton/cid.h>
26 #include <proton/types.h>
27 #include <stdarg.h>
28 #include <proton/type_compat.h>
29 #include <stddef.h>
30 #include <proton/import_export.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
40 typedef const void* pn_handle_t;
41 typedef intptr_t pn_shandle_t;
42 
43 typedef struct pn_class_t pn_class_t;
44 typedef struct pn_string_t pn_string_t;
45 typedef struct pn_list_t pn_list_t;
46 typedef struct pn_map_t pn_map_t;
47 typedef struct pn_hash_t pn_hash_t;
48 typedef void *(*pn_iterator_next_t)(void *state);
49 typedef struct pn_iterator_t pn_iterator_t;
50 typedef struct pn_record_t pn_record_t;
51 
52 struct pn_class_t {
53  const char *name;
54  const pn_cid_t cid;
55  void *(*newinst)(const pn_class_t *, size_t);
56  void (*initialize)(void *);
57  void (*incref)(void *);
58  void (*decref)(void *);
59  int (*refcount)(void *);
60  void (*finalize)(void *);
61  void (*free)(void *);
62  const pn_class_t *(*reify)(void *);
63  uintptr_t (*hashcode)(void *);
64  intptr_t (*compare)(void *, void *);
65  int (*inspect)(void *, pn_string_t *);
66 };
67 
68 /* Hack alert: Declare these as arrays so we can treat the name of the single
69  object as the address */
70 PN_EXTERN extern const pn_class_t PN_OBJECT[];
71 PN_EXTERN extern const pn_class_t PN_VOID[];
72 PN_EXTERN extern const pn_class_t PN_WEAKREF[];
73 
74 #define PN_CLASSDEF(PREFIX) \
75 static void PREFIX ## _initialize_cast(void *object) { \
76  PREFIX ## _initialize((PREFIX ## _t *) object); \
77 } \
78  \
79 static void PREFIX ## _finalize_cast(void *object) { \
80  PREFIX ## _finalize((PREFIX ## _t *) object); \
81 } \
82  \
83 static uintptr_t PREFIX ## _hashcode_cast(void *object) { \
84  uintptr_t (*fp)(PREFIX ## _t *) = PREFIX ## _hashcode; \
85  if (fp) { \
86  return fp((PREFIX ## _t *) object); \
87  } else { \
88  return (uintptr_t) object; \
89  } \
90 } \
91  \
92 static intptr_t PREFIX ## _compare_cast(void *a, void *b) { \
93  intptr_t (*fp)(PREFIX ## _t *, PREFIX ## _t *) = PREFIX ## _compare; \
94  if (fp) { \
95  return fp((PREFIX ## _t *) a, (PREFIX ## _t *) b); \
96  } else { \
97  return (intptr_t) a - (intptr_t) b; \
98  } \
99 } \
100  \
101 static int PREFIX ## _inspect_cast(void *object, pn_string_t *str) { \
102  int (*fp)(PREFIX ## _t *, pn_string_t *) = PREFIX ## _inspect; \
103  if (fp) { \
104  return fp((PREFIX ## _t *) object, str); \
105  } else { \
106  return pn_string_addf(str, "%s<%p>", #PREFIX, object); \
107  } \
108 } \
109  \
110 const pn_class_t *PREFIX ## __class(void) { \
111  static const pn_class_t clazz = { \
112  #PREFIX, \
113  CID_ ## PREFIX, \
114  pn_object_new, \
115  PREFIX ## _initialize_cast, \
116  pn_object_incref, \
117  pn_object_decref, \
118  pn_object_refcount, \
119  PREFIX ## _finalize_cast, \
120  pn_object_free, \
121  pn_object_reify, \
122  PREFIX ## _hashcode_cast, \
123  PREFIX ## _compare_cast, \
124  PREFIX ## _inspect_cast \
125  }; \
126  return &clazz; \
127 } \
128  \
129 PREFIX ## _t *PREFIX ## _new(void) { \
130  return (PREFIX ## _t *) pn_class_new(PREFIX ## __class(), \
131  sizeof(PREFIX ## _t)); \
132 }
133 
134 #define PN_CLASS(PREFIX) { \
135  #PREFIX, \
136  CID_ ## PREFIX, \
137  pn_object_new, \
138  PREFIX ## _initialize, \
139  pn_object_incref, \
140  pn_object_decref, \
141  pn_object_refcount, \
142  PREFIX ## _finalize, \
143  pn_object_free, \
144  pn_object_reify, \
145  PREFIX ## _hashcode, \
146  PREFIX ## _compare, \
147  PREFIX ## _inspect \
148 }
149 
150 #define PN_METACLASS(PREFIX) { \
151  #PREFIX, \
152  CID_ ## PREFIX, \
153  PREFIX ## _new, \
154  PREFIX ## _initialize, \
155  PREFIX ## _incref, \
156  PREFIX ## _decref, \
157  PREFIX ## _refcount, \
158  PREFIX ## _finalize, \
159  PREFIX ## _free, \
160  PREFIX ## _reify, \
161  PREFIX ## _hashcode, \
162  PREFIX ## _compare, \
163  PREFIX ## _inspect \
164 }
165 
166 /* Class to identify a plain C struct in a pn_event_t. No refcounting or memory management. */
167 #define PN_STRUCT_CLASSDEF(PREFIX, CID) \
168  const pn_class_t *PREFIX ## __class(void); \
169  static const pn_class_t *PREFIX ## _reify(void *p) { return PREFIX ## __class(); } \
170  const pn_class_t *PREFIX ## __class(void) { \
171  static const pn_class_t clazz = { \
172  #PREFIX, \
173  CID, \
174  NULL, /*_new*/ \
175  NULL, /*_initialize*/ \
176  pn_void_incref, \
177  pn_void_decref, \
178  pn_void_refcount, \
179  NULL, /* _finalize */ \
180  NULL, /* _free */ \
181  PREFIX ## _reify, \
182  pn_void_hashcode, \
183  pn_void_compare, \
184  pn_void_inspect \
185  }; \
186  return &clazz; \
187  }
188 
189 PN_EXTERN pn_cid_t pn_class_id(const pn_class_t *clazz);
190 PN_EXTERN const char *pn_class_name(const pn_class_t *clazz);
191 PN_EXTERN void *pn_class_new(const pn_class_t *clazz, size_t size);
192 
193 /* pn_incref, pn_decref and pn_refcount are for internal use by the proton
194  library, the should not be called by application code. Application code
195  should use the appropriate pn_*_free function (pn_link_free, pn_session_free
196  etc.) when it is finished with a proton value. Proton values should only be
197  used when handling a pn_event_t that refers to them.
198 */
199 PN_EXTERN void *pn_class_incref(const pn_class_t *clazz, void *object);
200 PN_EXTERN int pn_class_refcount(const pn_class_t *clazz, void *object);
201 PN_EXTERN int pn_class_decref(const pn_class_t *clazz, void *object);
202 
203 PN_EXTERN void pn_class_free(const pn_class_t *clazz, void *object);
204 
205 PN_EXTERN const pn_class_t *pn_class_reify(const pn_class_t *clazz, void *object);
206 PN_EXTERN uintptr_t pn_class_hashcode(const pn_class_t *clazz, void *object);
207 PN_EXTERN intptr_t pn_class_compare(const pn_class_t *clazz, void *a, void *b);
208 PN_EXTERN bool pn_class_equals(const pn_class_t *clazz, void *a, void *b);
209 PN_EXTERN int pn_class_inspect(const pn_class_t *clazz, void *object, pn_string_t *dst);
210 
211 PN_EXTERN void *pn_void_new(const pn_class_t *clazz, size_t size);
212 PN_EXTERN void pn_void_incref(void *object);
213 PN_EXTERN void pn_void_decref(void *object);
214 PN_EXTERN int pn_void_refcount(void *object);
215 PN_EXTERN uintptr_t pn_void_hashcode(void *object);
216 PN_EXTERN intptr_t pn_void_compare(void *a, void *b);
217 PN_EXTERN int pn_void_inspect(void *object, pn_string_t *dst);
218 
219 PN_EXTERN void *pn_object_new(const pn_class_t *clazz, size_t size);
220 PN_EXTERN const pn_class_t *pn_object_reify(void *object);
221 PN_EXTERN void pn_object_incref(void *object);
222 PN_EXTERN int pn_object_refcount(void *object);
223 PN_EXTERN void pn_object_decref(void *object);
224 PN_EXTERN void pn_object_free(void *object);
225 
226 PN_EXTERN void *pn_incref(void *object);
227 PN_EXTERN int pn_decref(void *object);
228 PN_EXTERN int pn_refcount(void *object);
229 PN_EXTERN void pn_free(void *object);
230 PN_EXTERN const pn_class_t *pn_class(void* object);
231 PN_EXTERN uintptr_t pn_hashcode(void *object);
232 PN_EXTERN intptr_t pn_compare(void *a, void *b);
233 PN_EXTERN bool pn_equals(void *a, void *b);
234 PN_EXTERN int pn_inspect(void *object, pn_string_t *dst);
235 
236 #define PN_REFCOUNT (0x1)
237 
238 PN_EXTERN pn_list_t *pn_list(const pn_class_t *clazz, size_t capacity);
239 PN_EXTERN size_t pn_list_size(pn_list_t *list);
240 PN_EXTERN void *pn_list_get(pn_list_t *list, int index);
241 PN_EXTERN void pn_list_set(pn_list_t *list, int index, void *value);
242 PN_EXTERN int pn_list_add(pn_list_t *list, void *value);
243 PN_EXTERN void *pn_list_pop(pn_list_t *list);
244 PN_EXTERN ssize_t pn_list_index(pn_list_t *list, void *value);
245 PN_EXTERN bool pn_list_remove(pn_list_t *list, void *value);
246 PN_EXTERN void pn_list_del(pn_list_t *list, int index, int n);
247 PN_EXTERN void pn_list_clear(pn_list_t *list);
248 PN_EXTERN void pn_list_iterator(pn_list_t *list, pn_iterator_t *iter);
249 PN_EXTERN void pn_list_minpush(pn_list_t *list, void *value);
250 PN_EXTERN void *pn_list_minpop(pn_list_t *list);
251 
252 #define PN_REFCOUNT_KEY (0x2)
253 #define PN_REFCOUNT_VALUE (0x4)
254 
255 PN_EXTERN pn_map_t *pn_map(const pn_class_t *key, const pn_class_t *value,
256  size_t capacity, float load_factor);
257 PN_EXTERN size_t pn_map_size(pn_map_t *map);
258 PN_EXTERN int pn_map_put(pn_map_t *map, void *key, void *value);
259 PN_EXTERN void *pn_map_get(pn_map_t *map, void *key);
260 PN_EXTERN void pn_map_del(pn_map_t *map, void *key);
261 PN_EXTERN pn_handle_t pn_map_head(pn_map_t *map);
262 PN_EXTERN pn_handle_t pn_map_next(pn_map_t *map, pn_handle_t entry);
263 PN_EXTERN void *pn_map_key(pn_map_t *map, pn_handle_t entry);
264 PN_EXTERN void *pn_map_value(pn_map_t *map, pn_handle_t entry);
265 
266 PN_EXTERN pn_hash_t *pn_hash(const pn_class_t *clazz, size_t capacity, float load_factor);
267 PN_EXTERN size_t pn_hash_size(pn_hash_t *hash);
268 PN_EXTERN int pn_hash_put(pn_hash_t *hash, uintptr_t key, void *value);
269 PN_EXTERN void *pn_hash_get(pn_hash_t *hash, uintptr_t key);
270 PN_EXTERN void pn_hash_del(pn_hash_t *hash, uintptr_t key);
271 PN_EXTERN pn_handle_t pn_hash_head(pn_hash_t *hash);
272 PN_EXTERN pn_handle_t pn_hash_next(pn_hash_t *hash, pn_handle_t entry);
273 PN_EXTERN uintptr_t pn_hash_key(pn_hash_t *hash, pn_handle_t entry);
274 PN_EXTERN void *pn_hash_value(pn_hash_t *hash, pn_handle_t entry);
275 
276 PN_EXTERN pn_string_t *pn_string(const char *bytes);
277 PN_EXTERN pn_string_t *pn_stringn(const char *bytes, size_t n);
278 PN_EXTERN const char *pn_string_get(pn_string_t *string);
279 PN_EXTERN size_t pn_string_size(pn_string_t *string);
280 PN_EXTERN int pn_string_set(pn_string_t *string, const char *bytes);
281 PN_EXTERN int pn_string_setn(pn_string_t *string, const char *bytes, size_t n);
282 PN_EXTERN ssize_t pn_string_put(pn_string_t *string, char *dst);
283 PN_EXTERN void pn_string_clear(pn_string_t *string);
284 PN_EXTERN int pn_string_format(pn_string_t *string, const char *format, ...)
285 #ifdef __GNUC__
286  __attribute__ ((format (printf, 2, 3)))
287 #endif
288  ;
289 PN_EXTERN int pn_string_vformat(pn_string_t *string, const char *format, va_list ap);
290 PN_EXTERN int pn_string_addf(pn_string_t *string, const char *format, ...)
291 #ifdef __GNUC__
292  __attribute__ ((format (printf, 2, 3)))
293 #endif
294  ;
295 PN_EXTERN int pn_string_vaddf(pn_string_t *string, const char *format, va_list ap);
296 PN_EXTERN int pn_string_grow(pn_string_t *string, size_t capacity);
297 PN_EXTERN char *pn_string_buffer(pn_string_t *string);
298 PN_EXTERN size_t pn_string_capacity(pn_string_t *string);
299 PN_EXTERN int pn_string_resize(pn_string_t *string, size_t size);
300 PN_EXTERN int pn_string_copy(pn_string_t *string, pn_string_t *src);
301 
302 PN_EXTERN pn_iterator_t *pn_iterator(void);
303 PN_EXTERN void *pn_iterator_start(pn_iterator_t *iterator,
304  pn_iterator_next_t next, size_t size);
305 PN_EXTERN void *pn_iterator_next(pn_iterator_t *iterator);
306 
307 #define PN_LEGCTX ((pn_handle_t) 0)
308 
315 
318 
326 #define PN_HANDLE(name) \
327  static const char _PN_HANDLE_ ## name = 0; \
328  static const pn_handle_t name = ((pn_handle_t) &_PN_HANDLE_ ## name);
329 
330 PN_EXTERN pn_record_t *pn_record(void);
331 PN_EXTERN void pn_record_def(pn_record_t *record, pn_handle_t key, const pn_class_t *clazz);
332 PN_EXTERN bool pn_record_has(pn_record_t *record, pn_handle_t key);
333 PN_EXTERN void *pn_record_get(pn_record_t *record, pn_handle_t key);
334 PN_EXTERN void pn_record_set(pn_record_t *record, pn_handle_t key, void *value);
335 PN_EXTERN void pn_record_clear(pn_record_t *record);
336 
341 #ifdef __cplusplus
342 }
343 #endif
344 
345 #endif /* object.h */
AMQP and API data types.