Code

object: Added sdb_object_create_simple().
authorSebastian Harl <sh@tokkee.org>
Wed, 1 Jan 2014 23:40:35 +0000 (00:40 +0100)
committerSebastian Harl <sh@tokkee.org>
Wed, 1 Jan 2014 23:40:35 +0000 (00:40 +0100)
This function creates a "simple" object without custom initialization and
destructor.

src/core/object.c
src/include/core/object.h
t/core/object_test.c

index 43bb22a8fbf83ee39cb781beab8e34426f9505e9..bcefadbc9911570e837867213d843179a6579de8 100644 (file)
@@ -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 *))
index 9169c441383b292803a67efc35e91147528aa1e2..2f6470e01c8ee5350b75a7300029e88162b759a3 100644 (file)
@@ -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.
index e00d300177b4694a5fd2b6d90a1ee9d9a3e72c84..b741b6cb6bb88b6f0b8e844cfc75c16207a20414 100644 (file)
@@ -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: <obj>");
+       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: <obj>");
+       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