InterfaceableObject

Declares iobj_t which defines the standard structure of an interfaceable object.

Summary
InterfaceableObjectDeclares iobj_t which defines the standard structure of an interfaceable object.
CopyrightThis program is free software.
Files
C-kern/api/context/iobj.hHeader file InterfaceableObject.
C-kern/context/iobj.cImplementation file InterfaceableObject impl.
C-kern/konfig.hIncluded from header Konfiguration.
Types
struct iobj_tExport iobj_t into global namespace.
struct iobj_itDefine generic type <iobj_it> which is only used in the declaration of iobj_t.
Functions
test
unittest_context_iobjTest iobj_t functionality.
iobj_tThis type has only template character.
objectA pointer to the object data.
iimplA pointer to the implementation of the interface <iobj_it>.
lifetime
iobj_FREEStatic initializer.
iobj_INITStatic initializer.
init_iobjGeneric initialization.
initcopy_iobjGeneric initialization.
free_iobjGeneric free operation.
generic
genericcast_iobjCasts pointer to an interfaceable object to its standard type name.
iobj_DECLAREDeclares an interfaceable object of type declared_t.
inline implementation
Macros
free_iobjImplements iobj_t.free_iobj.
genericcast_iobjImplements iobj_t.genericcast_iobj.
init_iobjImplements iobj_t.init_iobj.
initcopy_iobjImplements iobj_t.initcopy_iobj.
iobj_DECLAREImplements iobj_t.iobj_DECLARE.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2013 Jörg Seebohn

Files

C-kern/api/context/iobj.h

Header file InterfaceableObject.

C-kern/context/iobj.c

Implementation file InterfaceableObject impl.

C-kern/konfig.h

Included from header Konfiguration.

Types

struct iobj_t

typedef struct iobj_t iobj_t

Export iobj_t into global namespace.

struct iobj_it

typedef struct iobj_it iobj_it

Define generic type <iobj_it> which is only used in the declaration of iobj_t.

Functions

Summary

test

unittest_context_iobj

int unittest_context_iobj(void)

Test iobj_t functionality.

iobj_t

struct iobj_t

This type has only template character.  It defines the general structure of an interfaceable object.  Such an object consists of a pointer to the object data and another pointer to the function table implementing the interface.  The object is accessed only through the interface.

Usage

Use iobj_DECLARE to declare your own interfaceable object type.  Use genericcast_iobj to a type into its standard type.

Example

Consider an interfaceable object example_t with its interface example_it.

struct example_it {
   int (*fct1) (struct example_t * obj, ...) ;
   int (*fct2) (struct example_t * obj, ...) ;
} ;

struct example_t {
   struct example_t *  object ;
   struct example_it * iimpl ;
} ;

The interfaceable object example_t can be declared with

iobj_DECLARE(example_t, example) ;

An anonymous type which is compatible with example_t can be declared with:

struct mystructure_t {
   iobj_DECLARE(, example) iexample ;
} ;
// this declares the structure
struct mystructure_t {
   struct { struct example_t * object ; struct example_it * iimpl } iexample ;
} ;

The anonymous type is useful to prevent header inclusion and makes compilation faster.  To cast such an anonymous type to the standard type use

struct mystructure_t my ;
struct example_t * iexample = genericcast_iobj(&my.iexample, example) ;

The macro genericcast_iobj checks that my.iexample is compatible with example_t.

Summary
objectA pointer to the object data.
iimplA pointer to the implementation of the interface <iobj_it>.
lifetime
iobj_FREEStatic initializer.
iobj_INITStatic initializer.
init_iobjGeneric initialization.
initcopy_iobjGeneric initialization.
free_iobjGeneric free operation.
generic
genericcast_iobjCasts pointer to an interfaceable object to its standard type name.
iobj_DECLAREDeclares an interfaceable object of type declared_t.

object

iobj_t * object

A pointer to the object data.  The object data is accessed through interface <iobj_it>.  The pointer to type iobj_t is casted into a custom type in the implementation of the interface <iobj_it>.

iimpl

iobj_it * iimpl

A pointer to the implementation of the interface <iobj_it>.  This pointer to a function table is used to access the functionality of the object.

lifetime

iobj_FREE

#define iobj_FREE { 0, 0 }

Static initializer.  Sets both pointer to NULL.

iobj_INIT

#define iobj_INIT(object,
iimpl) { (object), (iimpl) }

Static initializer.

Parameter

objectPointer to object data.
iimplPointer to function table/interface implementation of object.

init_iobj

void init_iobj(/*out*/iobj_t *iobj,
void *object,
void *iimpl)

Generic initialization.  Same as assigning iobj_INIT.  Can be used for any declared interfaceable object (see iobj_DECLARE).

initcopy_iobj

void initcopy_iobj(/*out*/iobj_t *destiobj,
const iobj_t *srciobj)

Generic initialization.  Same as assigning <iobj_INIT>(srciobj->object, srciobj->iimpl).  Can be used for any declared interfaceable object (see iobj_DECLARE).

free_iobj

void free_iobj(iobj_t *iobj)

Generic free operation.  Same as assigning iobj_FREE.  Can be used for any declared interfaceable object (see iobj_DECLARE).

generic

genericcast_iobj

void * genericcast_iobj(void *iobj,
IDNAME typenameprefix)

Casts pointer to an interfaceable object to its standard type name.  The standard type name is the prefix “typenameprefix” with the suffix “_t”.  The first parameter is the pointer to a compatible type and the second is the same as used in iobj_DECLARE.

iobj_DECLARE

void iobj_DECLARE(TYPENAME declared_t,
IDNAME typenameprefix)

Declares an interfaceable object of type declared_t.  See iobj_t for the generic structure.  The object type and interface type are derived from parameter typenameprefix.  The object type name has the prefix typenameprefix and the suffix “_t”.  The interface type name has the prefix typenameprefix and the suffix “_it”.

Macros

free_iobj

#define free_iobj(
   iobj
) do { typeof(iobj) _obj = (iobj) ; _obj->object = 0 ; _obj->iimpl = 0 ; } while (0)

Implements iobj_t.free_iobj.

genericcast_iobj

#define genericcast_iobj(
   iobj,
   typenameprefix
) ( __extension__ ({ static_assert( &((typeof(iobj))0)->object == ((struct typenameprefix##_t**) offsetof(iobj_t, object)) && &((typeof(iobj))0)->iimpl == ((struct typenameprefix##_it**) offsetof(iobj_t, iimpl)), "compatible structure") ; (typenameprefix##_t*) (iobj) ; }))

Implements iobj_t.genericcast_iobj.

init_iobj

#define init_iobj(
   iobj,
   _object,
   _iimpl
) do { typeof(iobj) _obj = (iobj) ; _obj->object = (_object) ; _obj->iimpl = (_iimpl) ; } while (0)

Implements iobj_t.init_iobj.

initcopy_iobj

#define initcopy_iobj(
   destiobj,
   srciobj
) do { typeof(srciobj) _src = (srciobj) ; typeof(destiobj) _dest = (destiobj) ; _dest->object = _src->object ; _dest->iimpl = _src->iimpl ; } while (0)

Implements iobj_t.initcopy_iobj.

iobj_DECLARE

#define iobj_DECLARE(
   declared_t,
   typenameprefix
) struct declared_t { struct typenameprefix##_t * object ; struct typenameprefix##_it * iimpl ; }

Implements iobj_t.iobj_DECLARE.

struct iobj_t
This type has only template character.
Declares iobj_t which defines the standard structure of an interfaceable object.
Implements InterfaceableObject.
Global generic configurations.
typedef struct iobj_t iobj_t
Export iobj_t into global namespace.
typedef struct iobj_it iobj_it
Define generic type iobj_it which is only used in the declaration of iobj_t.
int unittest_context_iobj(void)
Test iobj_t functionality.
iobj_t * object
A pointer to the object data.
iobj_it * iimpl
A pointer to the implementation of the interface iobj_it.
#define iobj_FREE { 0, 0 }
Static initializer.
#define iobj_INIT(object,
iimpl) { (object), (iimpl) }
Static initializer.
void init_iobj(/*out*/iobj_t *iobj,
void *object,
void *iimpl)
Generic initialization.
void initcopy_iobj(/*out*/iobj_t *destiobj,
const iobj_t *srciobj)
Generic initialization.
void free_iobj(iobj_t *iobj)
Generic free operation.
void * genericcast_iobj(void *iobj,
IDNAME typenameprefix)
Casts pointer to an interfaceable object to its standard type name.
void iobj_DECLARE(TYPENAME declared_t,
IDNAME typenameprefix)
Declares an interfaceable object of type declared_t.
#define free_iobj(
   iobj
) do { typeof(iobj) _obj = (iobj) ; _obj->object = 0 ; _obj->iimpl = 0 ; } while (0)
Implements iobj_t.free_iobj.
#define genericcast_iobj(
   iobj,
   typenameprefix
) ( __extension__ ({ static_assert( &((typeof(iobj))0)->object == ((struct typenameprefix##_t**) offsetof(iobj_t, object)) && &((typeof(iobj))0)->iimpl == ((struct typenameprefix##_it**) offsetof(iobj_t, iimpl)), "compatible structure") ; (typenameprefix##_t*) (iobj) ; }))
Implements iobj_t.genericcast_iobj.
#define init_iobj(
   iobj,
   _object,
   _iimpl
) do { typeof(iobj) _obj = (iobj) ; _obj->object = (_object) ; _obj->iimpl = (_iimpl) ; } while (0)
Implements iobj_t.init_iobj.
#define initcopy_iobj(
   destiobj,
   srciobj
) do { typeof(srciobj) _src = (srciobj) ; typeof(destiobj) _dest = (destiobj) ; _dest->object = _src->object ; _dest->iimpl = _src->iimpl ; } while (0)
Implements iobj_t.initcopy_iobj.
#define iobj_DECLARE(
   declared_t,
   typenameprefix
) struct declared_t { struct typenameprefix##_t * object ; struct typenameprefix##_it * iimpl ; }
Implements iobj_t.iobj_DECLARE.
Close