/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup net
* @{
*/
/** @file
* Generic type field.
*/
#ifndef __GENERIC_FIELD_H__
#define __GENERIC_FIELD_H__
#include <errno.h>
#include <malloc.h>
#include <mem.h>
#include <unistd.h>
/** Internal magic value for a field consistency check.
*/
#define GENERIC_FIELD_MAGIC_VALUE 0x55667788
/** Generic type field declaration.
* @param name Name of the field. Input parameter.
* @param type Inner object type. Input parameter
*/
#define GENERIC_FIELD_DECLARE( name, type ) \
\
typedef struct name name##_t; \
typedef name##_t * name##_ref; \
\
struct name{ \
size_t size; \
int next; \
type ** items; \
int magic; \
}; \
\
int name##_add( name##_ref field, type * value ); \
int name##_count( name##_ref field ); \
void name##_destroy( name##_ref field ); \
void name##_exclude_index( name##_ref field, int index ); \
type ** name##_get_field( name##_ref field ); \
type * name##_get_index( name##_ref field, int index ); \
int name##_initialize( name##_ref field ); \
int name##_is_valid( name##_ref field );
/** Generic type field implementation.
* Should follow declaration with the same parameters.
* @param name Name of the field. Input parameter.
* @param type Inner object type. Input parameter
*/
#define GENERIC_FIELD_IMPLEMENT( name, type ) \
\
int name##_add( name##_ref field, type * value ){ \
if( name##_is_valid( field )){ \
if( field->next == ( field->size - 1 )){ \
type ** tmp; \
\
tmp = ( type ** ) realloc( field->items, sizeof( type * ) * 2 * field->size ); \
if( ! tmp ) return ENOMEM; \
field->size *= 2; \
field->items = tmp; \
} \
field->items[ field->next ] = value; \
++ field->next; \
field->items[ field->next ] = NULL; \
return field->next - 1; \
} \
return EINVAL; \
} \
\
int name##_count( name##_ref field ){ \
return name##_is_valid( field ) ? field->next : -1; \
} \
\
void name##_destroy( name##_ref field ){ \
if( name##_is_valid( field )){ \
int index; \
\
field->magic = 0; \
for( index = 0; index < field->next; ++ index ){ \
free( field->items[ index ] ); \
} \
free( field->items ); \
} \
} \
\
void name##_exclude_index( name##_ref field, int index ){ \
if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \
free( field->items[ index ] ); \
field->items[ index ] = NULL; \
} \
} \
\
type * name##_get_index( name##_ref field, int index ){ \
if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \
return field->items[ index ]; \
} \
return NULL; \
} \
\
type ** name##_get_field( name##_ref field ){ \
return name##_is_valid( field ) ? field->items : NULL; \
} \
\
int name##_initialize( name##_ref field ){ \
if( ! field ) return EINVAL; \
field->size = 2; \
field->next = 0; \
field->items = ( type ** ) malloc( sizeof( type * ) * field->size ); \
if( ! field->items ) return ENOMEM; \
field->items[ field->next ] = NULL; \
field->magic = GENERIC_FIELD_MAGIC_VALUE; \
return EOK; \
} \
\
int name##_is_valid( name##_ref field ){ \
return field && ( field->magic == GENERIC_FIELD_MAGIC_VALUE ); \
}
#endif
/** @}
*/