1 #ifndef PROTON_OBJECT_H
2 #define PROTON_OBJECT_H 1
25 #include <proton/cid.h>
28 #include <proton/type_compat.h>
30 #include <proton/import_export.h>
40 typedef const void* pn_handle_t;
41 typedef intptr_t pn_shandle_t;
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;
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 *);
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 *);
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[];
74 #define PN_CLASSCLASS(PREFIX) PREFIX ## __class
76 #define PN_CLASSDEF(PREFIX) \
77 static void PREFIX ## _initialize_cast(void *object) { \
78 PREFIX ## _initialize((PREFIX ## _t *) object); \
81 static void PREFIX ## _finalize_cast(void *object) { \
82 PREFIX ## _finalize((PREFIX ## _t *) object); \
85 static uintptr_t PREFIX ## _hashcode_cast(void *object) { \
86 uintptr_t (*fp)(PREFIX ## _t *) = PREFIX ## _hashcode; \
88 return fp((PREFIX ## _t *) object); \
90 return (uintptr_t) object; \
94 static intptr_t PREFIX ## _compare_cast(void *a, void *b) { \
95 intptr_t (*fp)(PREFIX ## _t *, PREFIX ## _t *) = PREFIX ## _compare; \
97 return fp((PREFIX ## _t *) a, (PREFIX ## _t *) b); \
99 return (intptr_t) a - (intptr_t) b; \
103 static int PREFIX ## _inspect_cast(void *object, pn_string_t *str) { \
104 int (*fp)(PREFIX ## _t *, pn_string_t *) = PREFIX ## _inspect; \
106 return fp((PREFIX ## _t *) object, str); \
108 return pn_string_addf(str, "%s<%p>", #PREFIX, object); \
112 const pn_class_t PN_CLASSCLASS(PREFIX)[] = {{ \
116 PREFIX ## _initialize_cast, \
119 pn_object_refcount, \
120 PREFIX ## _finalize_cast, \
123 PREFIX ## _hashcode_cast, \
124 PREFIX ## _compare_cast, \
125 PREFIX ## _inspect_cast \
128 PREFIX ## _t *PREFIX ## _new(void) { \
129 return (PREFIX ## _t *) pn_class_new(PN_CLASSCLASS(PREFIX), \
130 sizeof(PREFIX ## _t)); \
133 #define PN_CLASS(PREFIX) { \
137 PREFIX ## _initialize, \
140 pn_object_refcount, \
141 PREFIX ## _finalize, \
144 PREFIX ## _hashcode, \
145 PREFIX ## _compare, \
149 #define PN_METACLASS(PREFIX) { \
153 PREFIX ## _initialize, \
156 PREFIX ## _refcount, \
157 PREFIX ## _finalize, \
160 PREFIX ## _hashcode, \
161 PREFIX ## _compare, \
166 #define PN_STRUCT_CLASSDEF(PREFIX) \
167 static const pn_class_t *PREFIX ## _reify(void *p); \
168 const pn_class_t PN_CLASSCLASS(PREFIX)[] = {{ \
183 const pn_class_t *PREFIX ## _reify(void *p) { \
184 return PN_CLASSCLASS(PREFIX); \
187 PN_EXTERN pn_cid_t pn_class_id(
const pn_class_t *clazz);
188 PN_EXTERN
const char *pn_class_name(
const pn_class_t *clazz);
189 PN_EXTERN
void *pn_class_new(
const pn_class_t *clazz,
size_t size);
197 PN_EXTERN
void *pn_class_incref(
const pn_class_t *clazz,
void *
object);
198 PN_EXTERN
int pn_class_refcount(
const pn_class_t *clazz,
void *
object);
199 PN_EXTERN
int pn_class_decref(
const pn_class_t *clazz,
void *
object);
201 PN_EXTERN
void pn_class_free(
const pn_class_t *clazz,
void *
object);
203 PN_EXTERN
const pn_class_t *pn_class_reify(
const pn_class_t *clazz,
void *
object);
204 PN_EXTERN uintptr_t pn_class_hashcode(
const pn_class_t *clazz,
void *
object);
205 PN_EXTERN intptr_t pn_class_compare(
const pn_class_t *clazz,
void *a,
void *b);
206 PN_EXTERN
bool pn_class_equals(
const pn_class_t *clazz,
void *a,
void *b);
207 PN_EXTERN
int pn_class_inspect(
const pn_class_t *clazz,
void *
object, pn_string_t *dst);
209 PN_EXTERN
void *pn_void_new(
const pn_class_t *clazz,
size_t size);
210 PN_EXTERN
void pn_void_incref(
void *
object);
211 PN_EXTERN
void pn_void_decref(
void *
object);
212 PN_EXTERN
int pn_void_refcount(
void *
object);
213 PN_EXTERN uintptr_t pn_void_hashcode(
void *
object);
214 PN_EXTERN intptr_t pn_void_compare(
void *a,
void *b);
215 PN_EXTERN
int pn_void_inspect(
void *
object, pn_string_t *dst);
217 PN_EXTERN
void *pn_object_new(
const pn_class_t *clazz,
size_t size);
218 PN_EXTERN
const pn_class_t *pn_object_reify(
void *
object);
219 PN_EXTERN
void pn_object_incref(
void *
object);
220 PN_EXTERN
int pn_object_refcount(
void *
object);
221 PN_EXTERN
void pn_object_decref(
void *
object);
222 PN_EXTERN
void pn_object_free(
void *
object);
224 PN_EXTERN
void *pn_incref(
void *
object);
225 PN_EXTERN
int pn_decref(
void *
object);
226 PN_EXTERN
int pn_refcount(
void *
object);
227 PN_EXTERN
void pn_free(
void *
object);
228 PN_EXTERN
const pn_class_t *pn_class(
void*
object);
229 PN_EXTERN uintptr_t pn_hashcode(
void *
object);
230 PN_EXTERN intptr_t pn_compare(
void *a,
void *b);
231 PN_EXTERN
bool pn_equals(
void *a,
void *b);
232 PN_EXTERN
int pn_inspect(
void *
object, pn_string_t *dst);
234 #define PN_REFCOUNT (0x1)
236 PN_EXTERN pn_list_t *pn_list(
const pn_class_t *clazz,
size_t capacity);
237 PN_EXTERN
size_t pn_list_size(pn_list_t *list);
238 PN_EXTERN
void *pn_list_get(pn_list_t *list,
int index);
239 PN_EXTERN
void pn_list_set(pn_list_t *list,
int index,
void *value);
240 PN_EXTERN
int pn_list_add(pn_list_t *list,
void *value);
241 PN_EXTERN
void *pn_list_pop(pn_list_t *list);
242 PN_EXTERN ssize_t pn_list_index(pn_list_t *list,
void *value);
243 PN_EXTERN
bool pn_list_remove(pn_list_t *list,
void *value);
244 PN_EXTERN
void pn_list_del(pn_list_t *list,
int index,
int n);
245 PN_EXTERN
void pn_list_clear(pn_list_t *list);
246 PN_EXTERN
void pn_list_iterator(pn_list_t *list, pn_iterator_t *iter);
247 PN_EXTERN
void pn_list_minpush(pn_list_t *list,
void *value);
248 PN_EXTERN
void *pn_list_minpop(pn_list_t *list);
250 #define PN_REFCOUNT_KEY (0x2)
251 #define PN_REFCOUNT_VALUE (0x4)
253 PN_EXTERN pn_map_t *pn_map(
const pn_class_t *key,
const pn_class_t *value,
254 size_t capacity,
float load_factor);
255 PN_EXTERN
size_t pn_map_size(pn_map_t *map);
256 PN_EXTERN
int pn_map_put(pn_map_t *map,
void *key,
void *value);
257 PN_EXTERN
void *pn_map_get(pn_map_t *map,
void *key);
258 PN_EXTERN
void pn_map_del(pn_map_t *map,
void *key);
259 PN_EXTERN pn_handle_t pn_map_head(pn_map_t *map);
260 PN_EXTERN pn_handle_t pn_map_next(pn_map_t *map, pn_handle_t entry);
261 PN_EXTERN
void *pn_map_key(pn_map_t *map, pn_handle_t entry);
262 PN_EXTERN
void *pn_map_value(pn_map_t *map, pn_handle_t entry);
264 PN_EXTERN pn_hash_t *pn_hash(
const pn_class_t *clazz,
size_t capacity,
float load_factor);
265 PN_EXTERN
size_t pn_hash_size(pn_hash_t *hash);
266 PN_EXTERN
int pn_hash_put(pn_hash_t *hash, uintptr_t key,
void *value);
267 PN_EXTERN
void *pn_hash_get(pn_hash_t *hash, uintptr_t key);
268 PN_EXTERN
void pn_hash_del(pn_hash_t *hash, uintptr_t key);
269 PN_EXTERN pn_handle_t pn_hash_head(pn_hash_t *hash);
270 PN_EXTERN pn_handle_t pn_hash_next(pn_hash_t *hash, pn_handle_t entry);
271 PN_EXTERN uintptr_t pn_hash_key(pn_hash_t *hash, pn_handle_t entry);
272 PN_EXTERN
void *pn_hash_value(pn_hash_t *hash, pn_handle_t entry);
274 PN_EXTERN pn_string_t *pn_string(
const char *bytes);
275 PN_EXTERN pn_string_t *pn_stringn(
const char *bytes,
size_t n);
276 PN_EXTERN
const char *pn_string_get(pn_string_t *
string);
277 PN_EXTERN
size_t pn_string_size(pn_string_t *
string);
278 PN_EXTERN
int pn_string_set(pn_string_t *
string,
const char *bytes);
279 PN_EXTERN
int pn_string_setn(pn_string_t *
string,
const char *bytes,
size_t n);
280 PN_EXTERN ssize_t pn_string_put(pn_string_t *
string,
char *dst);
281 PN_EXTERN
void pn_string_clear(pn_string_t *
string);
282 PN_EXTERN
int pn_string_format(pn_string_t *
string,
const char *format, ...)
284 __attribute__ ((format (printf, 2, 3)))
287 PN_EXTERN
int pn_string_vformat(pn_string_t *
string,
const char *format, va_list ap);
288 PN_EXTERN
int pn_string_addf(pn_string_t *
string,
const char *format, ...)
290 __attribute__ ((format (printf, 2, 3)))
293 PN_EXTERN
int pn_string_vaddf(pn_string_t *
string,
const char *format, va_list ap);
294 PN_EXTERN
int pn_string_grow(pn_string_t *
string,
size_t capacity);
295 PN_EXTERN
char *pn_string_buffer(pn_string_t *
string);
296 PN_EXTERN
size_t pn_string_capacity(pn_string_t *
string);
297 PN_EXTERN
int pn_string_resize(pn_string_t *
string,
size_t size);
298 PN_EXTERN
int pn_string_copy(pn_string_t *
string, pn_string_t *src);
300 PN_EXTERN pn_iterator_t *pn_iterator(
void);
301 PN_EXTERN
void *pn_iterator_start(pn_iterator_t *iterator,
302 pn_iterator_next_t next,
size_t size);
303 PN_EXTERN
void *pn_iterator_next(pn_iterator_t *iterator);
305 #define PN_LEGCTX ((pn_handle_t) 0)
324 #define PN_HANDLE(name) \
325 static const char _PN_HANDLE_ ## name = 0; \
326 static const pn_handle_t name = ((pn_handle_t) &_PN_HANDLE_ ## name);
328 PN_EXTERN pn_record_t *pn_record(
void);
329 PN_EXTERN
void pn_record_def(pn_record_t *record, pn_handle_t key,
const pn_class_t *clazz);
330 PN_EXTERN
bool pn_record_has(pn_record_t *record, pn_handle_t key);
331 PN_EXTERN
void *pn_record_get(pn_record_t *record, pn_handle_t key);
332 PN_EXTERN
void pn_record_set(pn_record_t *record, pn_handle_t key,
void *value);
333 PN_EXTERN
void pn_record_clear(pn_record_t *record);