From 7cc01057fdd29f6dce1c678f7d88c0c8232df835 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 2 Jan 2014 00:40:35 +0100 Subject: [PATCH] object: Added sdb_object_create_simple(). This function creates a "simple" object without custom initialization and destructor. --- src/core/object.c | 7 +++++++ src/include/core/object.h | 14 ++++++++++++++ t/core/object_test.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/core/object.c b/src/core/object.c index 43bb22a..bcefadb 100644 --- a/src/core/object.c +++ b/src/core/object.c @@ -120,6 +120,13 @@ sdb_object_create(const char *name, sdb_type_t type, ...) return obj; } /* sdb_object_create */ +sdb_object_t * +sdb_object_create_simple(const char *name, size_t size) +{ + sdb_type_t t = { size, NULL, NULL }; + return sdb_object_create(name, t); +} /* sdb_object_create_simple */ + sdb_object_t * sdb_object_create_wrapper(const char *name, void *data, void (*destructor)(void *)) diff --git a/src/include/core/object.h b/src/include/core/object.h index 9169c44..2f6470e 100644 --- a/src/include/core/object.h +++ b/src/include/core/object.h @@ -93,6 +93,20 @@ sdb_object_create(const char *name, sdb_type_t type, ...); sdb_object_t * sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap); +/* + * sdb_object_create_simple: + * Create a "simple" object without custom initialization and destructor. + * See the description of sdb_object_create for more details. + */ +sdb_object_t * +sdb_object_create_simple(const char *name, size_t size); + +/* + * sdb_object_create_T: + * Create a simple object of type 't'. + */ +#define sdb_object_create_T(n,t) sdb_object_create_simple((n), sizeof(t)) + /* * sdb_object_create_wrapper: * Create a new sdb_object_t wrapping some arbitrary other object. diff --git a/t/core/object_test.c b/t/core/object_test.c index e00d300..b741b6c 100644 --- a/t/core/object_test.c +++ b/t/core/object_test.c @@ -90,6 +90,12 @@ START_TEST(test_obj_create) fail_unless(obj->type.size == test_type.size, "after sdb_object_create(): type size mismatch; got: %zu; " "expected: %zu", obj->type.size, test_type.size); + fail_unless(obj->type.init == obj_init_noop, + "after sdb_object_create(): type init = %p; exptected: %p", + obj->type.init, obj_init_noop); + fail_unless(obj->type.destroy == obj_destroy_noop, + "after sdb_object_create(): type destroy = %p; exptected: %p", + obj->type.destroy, obj_destroy_noop); fail_unless(obj->ref_cnt == 1, "after sdb_object_create(): obj->ref_cnt = %d; expected: 1", obj->ref_cnt); @@ -156,6 +162,35 @@ START_TEST(test_obj_create) fail_unless(obj != NULL, "sdb_object_create() fails without destroy callback"); sdb_object_deref(obj); + + init_noop_called = 0; + obj = sdb_object_create_simple(name, sizeof(struct noop)); + fail_unless(obj != NULL, + "sdb_object_create_simple() = NULL; expected: "); + fail_unless(obj->type.size == sizeof(struct noop), + "sdb_object_create_simple() created object of size %zu; " + "expected: %zu", obj->type.size, sizeof(struct noop)); + fail_unless(obj->type.init == NULL, + "sdb_object_create_simple() did not set init=NULL"); + fail_unless(obj->type.destroy == NULL, + "sdb_object_create_simple() did not set destroy=NULL"); + fail_unless(init_noop_called == 0, + "sdb_object_create_simple() unexpectedly called noop's init"); + sdb_object_deref(obj); + + obj = sdb_object_create_T(NULL, struct noop); + fail_unless(obj != NULL, + "sdb_object_create_simple() = NULL; expected: "); + fail_unless(obj->type.size == sizeof(struct noop), + "sdb_object_create_simple() created object of size %zu; " + "expected: %zu", obj->type.size, sizeof(struct noop)); + fail_unless(obj->type.init == NULL, + "sdb_object_create_simple() did not set init=NULL"); + fail_unless(obj->type.destroy == NULL, + "sdb_object_create_simple() did not set destroy=NULL"); + fail_unless(init_noop_called == 0, + "sdb_object_create_simple() unexpectedly called noop's init"); + sdb_object_deref(obj); } END_TEST -- 2.30.2