dtobject
This is a facade layer for basic object operations such as creation, copying, disposal and string rendering.
Many diverse objects in the system implement this facade to provide a common interface for basic object management. It is espeicially useful for generic containers and utilities that need to manipulate objects without knowing their concrete types.
This facade is vtable-dispatched. Please refer to dtvtable for more information on vtable dispatching.
Mini-guide
- For a class to implement this facade, it must register a
dtobject_vt_tvtable for its model number and implement all the functions below. - To call the facade functions, pass the handle to the function cast as
dtobject_handle.
Example Creation From Model Number
#include <dtcore/dtobject.h>
my_object_handle h = NULL;
dtobject_create(MODEL_NUMBER, (dtobject_handle_t*)&h);
dtobject_to_string((dtobject_handle_t)h, buffer, buffer_size);
dtobject_dispose((dtobject_handle_t)h);
Example Handle Sharing
#include <dtcore/dtobject.h>
#include <dtcore/dtrandomizer_uniform.h>
dtrandomizer_handle r = NULL;
// create a dtrandomizer_uniform object, which we know implements dtobject facade
dtrandomizer_uniform_create(&r);
// use dtobject facade to render it to string
dtobject_to_string((dtobject_handle_t)r, buffer, buffer_size);
Example Implementation
#include <dtcore/dtobject.h>
#define MODEL_NUMBER 1
static dterr_t* my_create(void* handle) { (void)handle; return NULL; }
static void my_copy(void* self, void* that) { (void)self; (void)that; }
static void my_dispose(void* self) { (void)self; }
static bool my_equals(void* self, void* that) { (void)self; (void)that; return true; }
static const char* my_get_class(void* self) { (void)self; return "my_t"; }
static bool my_is_iface(void* self, const char* iface_name) { (void)self; (void)iface_name; return false; }
static void my_to_string(void* self, char* buffer, size_t buffer_size) { (void)self; (void)buffer; (void)buffer_size; }
int main(void)
{
dtobject_vt_t vt = {
.create = my_create,
.copy = my_copy,
.dispose = my_dispose,
.equals = my_equals,
.get_class = my_get_class,
.is_iface = my_is_iface,
.to_string = my_to_string,
};
(void)dtobject_set_vtable(MODEL_NUMBER, &vt);
dtobject_handle h = NULL;
(void)dtobject_create(MODEL_NUMBER, &h);
dtobject_dispose(h);
return 0;
}
Data structures
dtobject_handle
Defines an opaque handle used for dispatch calls.
dtobject_vt_t
Defines the vtable used to dispatch object operations.
Members:
dtobject_create_fn createCreate delegate for the model.
dtobject_copy_fn copyCopy delegate for the model.
dtobject_dispose_fn disposeDispose delegate for the model.
dtobject_equals_fn equalsEquality delegate for the model.
dtobject_get_class_fn get_classClass-name delegate for the model.
dtobject_is_iface_fn is_ifaceInterface test delegate for the model.
dtobject_to_string_fn to_stringString rendering delegate for the model.
Macros
DTOBJECT_COMMON_MEMBERS
Defines common members required at the start of implementation structures.
DTOBJECT_DECLARE_API
DTOBJECT_DECLARE_API(NAME)
Declares a typed object API that maps to the common dispatch surface.
DTOBJECT_INIT_VTABLE
DTOBJECT_INIT_VTABLE(NAME)
Defines a vtable initializer that binds a typed API to the common vtable shape.
Functions
dtobject_copy
Dispatches a copy operation through the vtable for the handle model number.
Params:
dtobject_handle handleObject handle to copy into.
dtobject_handle thatObject handle to copy from.
Return: void No return value.
dtobject_create
Dispatches a create operation through the vtable for a model number.
Params:
int32_t model_numberModel number used to select a registered vtable.
dtobject_handle* handleOutput location for the created handle.
Return: dterr_t* Error on failure, or NULL on success.
dtobject_dispose
Dispatches a dispose operation through the vtable for the handle model number.
Params:
dtobject_handle handleObject handle to dispose.
Return: void No return value.
dtobject_equals
Dispatches an equality operation through the vtable for the handle model number.
Params:
dtobject_handle handleFirst object handle to compare.
dtobject_handle thatSecond object handle to compare.
Return: bool True if equal, otherwise false.
dtobject_get_class
Dispatches a class-name query through the vtable for the handle model number.
Params:
dtobject_handle handleObject handle to query.
Return: const char* Class name string, or NULL on failure.
dtobject_get_vtable
Retrieves a registered vtable for a model number.
Params:
int32_t model_numberModel number used to select a registered vtable.
dtobject_vt_t** vtableOutput location for the vtable pointer.
Return: dterr_t* Error on failure, or NULL on success.
dtobject_is_iface
Dispatches an interface-membership query through the vtable for the handle model number.
Params:
dtobject_handle handleObject handle to query.
const char* iface_nameInterface name to test.
Return: bool True if the interface is supported, otherwise false.
dtobject_set_vtable
Registers a vtable for a model number in the dispatch registry.
Params:
int32_t model_numberModel number used to identify the vtable.
dtobject_vt_t* vtableVtable to register for dispatch.
Return: dterr_t* Error on failure, or NULL on success.
dtobject_to_string
Dispatches a string rendering operation through the vtable for the handle model number.
Params:
dtobject_handle handleObject handle to render.
char *bufferOutput buffer for the rendered string.
size_t buffer_sizeSize of the output buffer in bytes.
Return: void No return value.