Typeadapt-Lifetime

Defines generic interface typeadapt_lifetime_it supporting lifetime functions for an object type that wants to be stored in a container.

Summary
Typeadapt-LifetimeDefines generic interface typeadapt_lifetime_it supporting lifetime functions for an object type that wants to be stored in a container.
CopyrightThis program is free software.
Files
C-kern/api/ds/typeadapt/lifetime.hHeader file Typeadapt-Lifetime.
C-kern/ds/typeadapt/lifetime.cImplementation file Typeadapt-Lifetime impl.
Types
struct typeadapt_lifetime_itExport typeadapt_lifetime_it into global namespace.
Functions
test
unittest_ds_typeadapt_lifetimeTest typeadapt_lifetime_it functionality.
typeadapt_lifetime_itDeclares interface (function table) for managing the lifetime of objects.
newcopy_objectFunction copies an object.
delete_objectFunction frees memory and associated resources of object.
lifetime
typeadapt_lifetime_FREEStatic initializer.
typeadapt_lifetime_INITStatic initializer.
query
call-service
callnewcopy_typeadaptlifetimeCalls function typeadapt_lifetime_it.newcopy_object.
calldelete_typeadaptlifetimeCalls function typeadapt_lifetime_it.delete_object.
generic
genericcast_typeadaptlifetimeCasts parameter adplife into pointer to typeadapt_lifetime_it.
typeadapt_lifetime_DECLAREDeclares a derived interface from generic typeadapt_lifetime_it.
typeadapt_lifetime_EMBEDDeclares the function table used to implement typeadapt_lifetime_DECLARE Use typeadapt_lifetime_EMBED if you want to embed the function table directly into another structure.
inline implementation
Macros
genericcast_typeadaptlifetimeImplements typeadapt_lifetime_it.genericcast_typeadaptlifetime.
callnewcopy_typeadaptlifetimeImplements typeadapt_lifetime_it.callnewcopy_typeadaptlifetime.
calldelete_typeadaptlifetimeImplements typeadapt_lifetime_it.calldelete_typeadaptlifetime.

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

© 2012 Jörg Seebohn

Files

C-kern/api/ds/typeadapt/lifetime.h

Header file Typeadapt-Lifetime.

C-kern/ds/typeadapt/lifetime.c

Implementation file Typeadapt-Lifetime impl.

Types

struct typeadapt_lifetime_it

typedef struct typeadapt_lifetime_it typeadapt_lifetime_it

Export typeadapt_lifetime_it into global namespace.

test

unittest_ds_typeadapt_lifetime

int unittest_ds_typeadapt_lifetime(void)

Test typeadapt_lifetime_it functionality.

typeadapt_lifetime_it

struct typeadapt_lifetime_it

Declares interface (function table) for managing the lifetime of objects.  If you change this interface do not forget to adapt typeadapt_lifetime_EMBED and genericcast_typeadaptlifetime.

Summary
newcopy_objectFunction copies an object.
delete_objectFunction frees memory and associated resources of object.
lifetime
typeadapt_lifetime_FREEStatic initializer.
typeadapt_lifetime_INITStatic initializer.
query
call-service
callnewcopy_typeadaptlifetimeCalls function typeadapt_lifetime_it.newcopy_object.
calldelete_typeadaptlifetimeCalls function typeadapt_lifetime_it.delete_object.
generic
genericcast_typeadaptlifetimeCasts parameter adplife into pointer to typeadapt_lifetime_it.
typeadapt_lifetime_DECLAREDeclares a derived interface from generic typeadapt_lifetime_it.
typeadapt_lifetime_EMBEDDeclares the function table used to implement typeadapt_lifetime_DECLARE Use typeadapt_lifetime_EMBED if you want to embed the function table directly into another structure.

newcopy_object

int (
   *newcopy_object
) (struct typeadapt_t * typeadp, /*out*/struct typeadapt_object_t ** destobject, const struct typeadapt_object_t * srcobject)

Function copies an object.  The called function makes a copy of srcobject and returns it in destobject.  Memory for the new object is allocated.  ENOMEM is returned in case there is not enough memory for a copy.

delete_object

int (
   *delete_object
) (struct typeadapt_t * typeadp, struct typeadapt_object_t ** object)

Function frees memory and associated resources of object.  Even in case of an error it tries to free all remaining resources and the object is marked as freed after return nonetheless.  Pointer object is set to NULL after return.

lifetime

typeadapt_lifetime_FREE

#define typeadapt_lifetime_FREE { 0, 0 }

Static initializer.  Sets all functions pointers of typeadapt_lifetime_it to 0.

typeadapt_lifetime_INIT

#define typeadapt_lifetime_INIT(newcopyobj_f,
deleteobj_f) { (newcopyobj_f), (deleteobj_f) }

Static initializer.  Sets all function pointers to the provided values.

Parameters

newcopyobj_fFunction pointer to copy object function.  See typeadapt_lifetime_it.newcopy_object.
deleteobj_fFunction pointer to free object function.  See typeadapt_lifetime_it.delete_object.

query

call-service

callnewcopy_typeadaptlifetime

int callnewcopy_typeadaptlifetime(
   typeadapt_lifetime_it *adplife,
   struct typeadapt_t *typeadp,
   /*out*/struct typeadapt_object_t **destobject,
   const struct typeadapt_object_t *srcobject
)

Calls function typeadapt_lifetime_it.newcopy_object.  The first parameter is of type typeadapt_lifetime_it the others are the same as in typeadapt_lifetime_it.delete_object.  This function is implemented as macro and supports types derived from typeadapt_lifetime_it - see typeadapt_lifetime_DECLARE.

calldelete_typeadaptlifetime

int calldelete_typeadaptlifetime(typeadapt_lifetime_it *adplife,
struct typeadapt_t *typeadp,
struct typeadapt_object_t **object)

Calls function typeadapt_lifetime_it.delete_object.  The first parameter is of type typeadapt_lifetime_it the others are the same as in typeadapt_lifetime_it.delete_object.  This function is implemented as macro and supports types derived from typeadapt_lifetime_it - see typeadapt_lifetime_DECLARE.

generic

genericcast_typeadaptlifetime

typeadapt_lifetime_it * genericcast_typeadaptlifetime(void *adplife,
TYPENAME typeadapter_t,
TYPENAME object_t) ;

Casts parameter adplife into pointer to typeadapt_lifetime_it.  The parameter adplife has to be of type “pointer to declared_it” where declared_it is the name used as first parameter in typeadapt_lifetime_DECLARE.  The second and third parameter must be the same as in typeadapt_lifetime_DECLARE.

typeadapt_lifetime_DECLARE

#define typeadapt_lifetime_DECLARE(
   declared_it,
   typeadapter_t,
   object_t
) typedef struct declared_it declared_it ; struct declared_it { typeadapt_lifetime_EMBED(typeadapter_t, object_t) ; }

Declares a derived interface from generic typeadapt_lifetime_it.  It is structural compatible with typeadapt_lifetime_it.  See typeadapt_lifetime_it for a list of contained functions.

Parameter

declared_itThe name of the structure which is declared as the interface.  The name should have the suffix “_it”.
typeadapter_tThe adapter type which implements all interface functions.  The first parameter in every function is a pointer to this type.
object_tThe object type that typeadapt_lifetime_it supports.

typeadapt_lifetime_EMBED

#define typeadapt_lifetime_EMBED(
   typeadapter_t,
   object_t
) int (* newcopy_object) (typeadapter_t * typeadp, /*out*/object_t ** destobject, const object_t * srcobject) ; int (* delete_object) (typeadapter_t * typeadp, object_t ** object)

Declares the function table used to implement typeadapt_lifetime_DECLARE Use typeadapt_lifetime_EMBED if you want to embed the function table directly into another structure.  See typeadapt_lifetime_it for a list of the declared functions.

Parameter

typeadapter_tThe adapter type which implements all interface functions.  The first parameter in every function is a pointer to this type.
object_tThe object type that typeadapt_lifetime_it supports.

Macros

genericcast_typeadaptlifetime

#define genericcast_typeadaptlifetime(
   adplife,
   typeadapter_t,
   object_t
) ( __extension__ ({ static_assert( offsetof(typeadapt_lifetime_it, newcopy_object) == offsetof(typeof(*(adplife)), newcopy_object) && offsetof(typeadapt_lifetime_it, delete_object) == offsetof(typeof(*(adplife)), delete_object), "ensure same structure") ; if (0) { int _err = (adplife)->newcopy_object((typeadapter_t*)0, (object_t**)0, (const object_t*)0) ; _err += (adplife)->delete_object((typeadapter_t*)0, (object_t**)0) ; (void) _err ; } (typeadapt_lifetime_it*) (adplife) ; }))

Implements typeadapt_lifetime_it.genericcast_typeadaptlifetime.

callnewcopy_typeadaptlifetime

#define callnewcopy_typeadaptlifetime(
   adplife,
   typeadp,
   destobject,
   srcobject
) ((adplife)->newcopy_object((typeadp), (destobject), (srcobject)))

Implements typeadapt_lifetime_it.callnewcopy_typeadaptlifetime.

calldelete_typeadaptlifetime

#define calldelete_typeadaptlifetime(
   adplife,
   typeadp,
   object
) ((adplife)->delete_object((typeadp), (object)))

Implements typeadapt_lifetime_it.calldelete_typeadaptlifetime.

struct typeadapt_lifetime_it
Declares interface (function table) for managing the lifetime of objects.
Defines generic interface typeadapt_lifetime_it supporting lifetime functions for an object type that wants to be stored in a container.
Implements Typeadapt-Lifetime.
typedef struct typeadapt_lifetime_it typeadapt_lifetime_it
Export typeadapt_lifetime_it into global namespace.
int unittest_ds_typeadapt_lifetime(void)
Test typeadapt_lifetime_it functionality.
int (
   *newcopy_object
) (struct typeadapt_t * typeadp, /*out*/struct typeadapt_object_t ** destobject, const struct typeadapt_object_t * srcobject)
Function copies an object.
int (
   *delete_object
) (struct typeadapt_t * typeadp, struct typeadapt_object_t ** object)
Function frees memory and associated resources of object.
#define typeadapt_lifetime_FREE { 0, 0 }
Static initializer.
#define typeadapt_lifetime_INIT(newcopyobj_f,
deleteobj_f) { (newcopyobj_f), (deleteobj_f) }
Static initializer.
int callnewcopy_typeadaptlifetime(
   typeadapt_lifetime_it *adplife,
   struct typeadapt_t *typeadp,
   /*out*/struct typeadapt_object_t **destobject,
   const struct typeadapt_object_t *srcobject
)
Calls function typeadapt_lifetime_it.newcopy_object.
int calldelete_typeadaptlifetime(typeadapt_lifetime_it *adplife,
struct typeadapt_t *typeadp,
struct typeadapt_object_t **object)
Calls function typeadapt_lifetime_it.delete_object.
typeadapt_lifetime_it * genericcast_typeadaptlifetime(void *adplife,
TYPENAME typeadapter_t,
TYPENAME object_t) ;
Casts parameter adplife into pointer to typeadapt_lifetime_it.
#define typeadapt_lifetime_DECLARE(
   declared_it,
   typeadapter_t,
   object_t
) typedef struct declared_it declared_it ; struct declared_it { typeadapt_lifetime_EMBED(typeadapter_t, object_t) ; }
Declares a derived interface from generic typeadapt_lifetime_it.
#define typeadapt_lifetime_EMBED(
   typeadapter_t,
   object_t
) int (* newcopy_object) (typeadapter_t * typeadp, /*out*/object_t ** destobject, const object_t * srcobject) ; int (* delete_object) (typeadapter_t * typeadp, object_t ** object)
Declares the function table used to implement typeadapt_lifetime_DECLARE Use typeadapt_lifetime_EMBED if you want to embed the function table directly into another structure.
#define genericcast_typeadaptlifetime(
   adplife,
   typeadapter_t,
   object_t
) ( __extension__ ({ static_assert( offsetof(typeadapt_lifetime_it, newcopy_object) == offsetof(typeof(*(adplife)), newcopy_object) && offsetof(typeadapt_lifetime_it, delete_object) == offsetof(typeof(*(adplife)), delete_object), "ensure same structure") ; if (0) { int _err = (adplife)->newcopy_object((typeadapter_t*)0, (object_t**)0, (const object_t*)0) ; _err += (adplife)->delete_object((typeadapter_t*)0, (object_t**)0) ; (void) _err ; } (typeadapt_lifetime_it*) (adplife) ; }))
Implements typeadapt_lifetime_it.genericcast_typeadaptlifetime.
#define callnewcopy_typeadaptlifetime(
   adplife,
   typeadp,
   destobject,
   srcobject
) ((adplife)->newcopy_object((typeadp), (destobject), (srcobject)))
Implements typeadapt_lifetime_it.callnewcopy_typeadaptlifetime.
#define calldelete_typeadaptlifetime(
   adplife,
   typeadp,
   object
) ((adplife)->delete_object((typeadp), (object)))
Implements typeadapt_lifetime_it.calldelete_typeadaptlifetime.
Close