From 497eb5a4a4279faf716ceb8d469f2217cbbabaf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Tue, 20 Mar 2012 20:12:40 -0700 Subject: [PATCH] add description --- base/array.c | 1 + base/bool.c | 1 + base/data.c | 3 ++- base/db.c | 1 + base/dict.c | 1 + base/error.c | 3 ++- base/heimbase.c | 28 ++++++++++++++++++++++++---- base/heimbase.h | 10 +++++++++- base/heimbasepriv.h | 5 ++++- base/null.c | 1 + base/number.c | 3 ++- base/string.c | 42 +++++++++++++++++++++++++++++++++++++++++- 12 files changed, 89 insertions(+), 10 deletions(-) diff --git a/base/array.c b/base/array.c index a513077c1..9590bea34 100644 --- a/base/array.c +++ b/base/array.c @@ -63,6 +63,7 @@ struct heim_type_data array_object = { array_dealloc, NULL, NULL, + NULL, NULL }; diff --git a/base/bool.c b/base/bool.c index 72edcc71e..f7b66ee35 100644 --- a/base/bool.c +++ b/base/bool.c @@ -42,6 +42,7 @@ struct heim_type_data _heim_bool_object = { NULL, NULL, NULL, + NULL, NULL }; diff --git a/base/data.c b/base/data.c index a9f836d98..4aa6efc66 100644 --- a/base/data.c +++ b/base/data.c @@ -80,7 +80,8 @@ struct heim_type_data _heim_data_object = { data_dealloc, NULL, data_cmp, - data_hash + data_hash, + NULL }; /** diff --git a/base/db.c b/base/db.c index 13eab5115..ae7244c5d 100644 --- a/base/db.c +++ b/base/db.c @@ -94,6 +94,7 @@ struct heim_type_data db_object = { db_dealloc, NULL, NULL, + NULL, NULL }; diff --git a/base/dict.c b/base/dict.c index 3893dfb9d..8d73846b2 100644 --- a/base/dict.c +++ b/base/dict.c @@ -71,6 +71,7 @@ struct heim_type_data dict_object = { dict_dealloc, NULL, NULL, + NULL, NULL }; diff --git a/base/error.c b/base/error.c index 1b2413d68..54313698a 100644 --- a/base/error.c +++ b/base/error.c @@ -72,7 +72,8 @@ struct heim_type_data _heim_error_object = { error_dealloc, NULL, error_cmp, - error_hash + error_hash, + NULL }; heim_error_t diff --git a/base/heimbase.c b/base/heimbase.c index 51b76b9a1..529f14703 100644 --- a/base/heimbase.c +++ b/base/heimbase.c @@ -138,6 +138,20 @@ heim_release(void *ptr) heim_abort("over release"); } +/** + * If used require wrapped in autorelease pool + */ + +heim_string_t +heim_description(heim_object_t ptr) +{ + struct heim_base *p = PTR2BASE(ptr); + if (p->isa->desc == NULL) + return heim_auto_release(heim_string_ref_create(p->isa->name, NULL)); + return heim_auto_release(p->isa->desc(ptr)); +} + + void _heim_make_permanent(heim_object_t ptr) { @@ -254,6 +268,7 @@ struct heim_type_data memory_object = { memory_dealloc, NULL, NULL, + NULL, NULL }; @@ -290,7 +305,8 @@ _heim_create_type(const char *name, heim_type_dealloc dealloc, heim_type_copy copy, heim_type_cmp cmp, - heim_type_hash hash) + heim_type_hash hash, + heim_type_description desc) { heim_type_t type; @@ -305,6 +321,7 @@ _heim_create_type(const char *name, type->copy = copy; type->cmp = cmp; type->hash = hash; + type->desc = desc; return type; } @@ -509,7 +526,8 @@ static struct heim_type_data _heim_autorel_object = { autorel_dealloc, NULL, autorel_cmp, - autorel_hash + autorel_hash, + NULL }; /** @@ -548,7 +566,7 @@ heim_auto_release_create(void) * @param ptr object */ -void +heim_object_t heim_auto_release(heim_object_t ptr) { struct heim_base *p = PTR2BASE(ptr); @@ -556,7 +574,7 @@ heim_auto_release(heim_object_t ptr) heim_auto_release_t ar; if (ptr == NULL || heim_base_is_tagged(ptr)) - return; + return ptr; /* drop from old pool */ if ((ar = p->autorelpool) != NULL) { @@ -573,6 +591,8 @@ heim_auto_release(heim_object_t ptr) HEIM_TAILQ_INSERT_HEAD(&ar->pool, p, autorel); p->autorelpool = ar; HEIMDAL_MUTEX_unlock(&ar->pool_mutex); + + return ptr; } /** diff --git a/base/heimbase.h b/base/heimbase.h index c5172eced..7e99bd087 100644 --- a/base/heimbase.h +++ b/base/heimbase.h @@ -177,6 +177,7 @@ heim_string_t heim_string_ref_create(const char *, heim_string_free_f_t); heim_string_t heim_string_create_with_bytes(const void *, size_t); heim_string_t heim_string_ref_create_with_bytes(const void *, size_t, heim_string_free_f_t); +heim_string_t heim_string_create_with_format(const char *, ...); heim_tid_t heim_string_get_type_id(void); const char * heim_string_get_utf8(heim_string_t); @@ -336,7 +337,7 @@ typedef struct heim_auto_release * heim_auto_release_t; heim_auto_release_t heim_auto_release_create(void); void heim_auto_release_drain(heim_auto_release_t); -void heim_auto_release(heim_object_t); +heim_object_t heim_auto_release(heim_object_t); /* * JSON @@ -362,6 +363,13 @@ heim_string_t heim_serialize(heim_object_t, heim_json_flags_t flags, heim_error_t *); +/* + * Debug + */ + +heim_string_t +heim_description(heim_object_t ptr); + /* * Binary search. * diff --git a/base/heimbasepriv.h b/base/heimbasepriv.h index e20734292..58bf4a73a 100644 --- a/base/heimbasepriv.h +++ b/base/heimbasepriv.h @@ -37,6 +37,7 @@ typedef void (*heim_type_init)(void *); typedef heim_object_t (*heim_type_copy)(void *); typedef int (*heim_type_cmp)(void *, void *); typedef unsigned long (*heim_type_hash)(void *); +typedef heim_string_t (*heim_type_description)(void *); typedef struct heim_type_data *heim_type_t; @@ -69,6 +70,7 @@ struct heim_type_data { heim_type_copy copy; heim_type_cmp cmp; heim_type_hash hash; + heim_type_description desc; }; heim_type_t _heim_get_isa(heim_object_t); @@ -79,7 +81,8 @@ _heim_create_type(const char *name, heim_type_dealloc dealloc, heim_type_copy copy, heim_type_cmp cmp, - heim_type_hash hash); + heim_type_hash hash, + heim_type_description desc); heim_object_t _heim_alloc_object(heim_type_t type, size_t size); diff --git a/base/null.c b/base/null.c index 66731aad2..43bde965c 100644 --- a/base/null.c +++ b/base/null.c @@ -42,6 +42,7 @@ struct heim_type_data _heim_null_object = { NULL, NULL, NULL, + NULL, NULL }; diff --git a/base/number.c b/base/number.c index 72631a531..c259f6997 100644 --- a/base/number.c +++ b/base/number.c @@ -73,7 +73,8 @@ struct heim_type_data _heim_number_object = { number_dealloc, NULL, number_cmp, - number_hash + number_hash, + NULL }; /** diff --git a/base/string.c b/base/string.c index 49779fbac..87396dc53 100644 --- a/base/string.c +++ b/base/string.c @@ -91,7 +91,8 @@ struct heim_type_data _heim_string_object = { string_dealloc, NULL, string_cmp, - string_hash + string_hash, + NULL }; /** @@ -158,6 +159,45 @@ heim_string_create_with_bytes(const void *data, size_t len) return s; } +/* + * + */ + +static void +string_free(void *ptr) +{ + free(ptr); +} + +/** + * Create a string object using a format string + * + * @param fmt format string + * @param ... + * + * @return string object + */ + +heim_string_t +heim_string_create_with_format(const char *fmt, ...) +{ + heim_string_t s; + char *str = NULL; + va_list ap; + int ret; + + va_start(ap, fmt); + ret = vasprintf(&str, fmt, ap); + va_end(ap); + if (ret < 0 || str == NULL) + return NULL; + + s = heim_string_ref_create(str, string_dealloc); + if (s == NULL) + free(str); + return s; +} + /** * Return the type ID of string objects *