Simple coroutine library / objects in plain C Snapshot
val.h
Go to the documentation of this file.
00001 #ifndef _VALUES_H_
00002 #define _VALUES_H_
00003 
00004 #include <cutils/array.h>
00005 #include <stdarg.h>
00006 
00007 /** 
00008  * @defgroup VAL
00009  * @brief one typed value
00010  * A typed value, a tuple of typed values is conveniently passed around by the VALUES class
00011  * @{
00012  */
00013  
00014 typedef enum {
00015   VAL_TYPE_UINT8,
00016   VAL_TYPE_INT8,
00017   VAL_TYPE_UINT16,
00018   VAL_TYPE_INT16,
00019   VAL_TYPE_UINT32,
00020   VAL_TYPE_INT32,
00021   VAL_TYPE_UINT64,
00022   VAL_TYPE_INT64,
00023   VAL_TYPE_STRING,
00024   VAL_TYPE_PTR,
00025 } VAL_TYPE;
00026 
00027 typedef struct tagVAL {
00028   VAL_TYPE   type;
00029   union {
00030     uint8_t  u8;
00031     int8_t   n8;
00032     uint16_t u16;
00033     int16_t  n16;
00034     uint32_t u32;
00035     int32_t  n32;
00036     uint64_t u64;
00037     int64_t  n64;
00038 
00039     char    *sval;
00040     void    *pval;
00041 
00042   } u;
00043 
00044 } VAL;
00045 
00046 /**
00047  * @}
00048  */
00049  
00050 /** 
00051  * @defgroup VALUES
00052  * @brief A class for conveniently passing of typed tuple of values 
00053  *
00054  * A typed tuple of values is stored into the VALUES object by a printf like function.
00055  *
00056  * VALUES_print( values, "%hhu%hd%d%qd%s", n8, n16, n32, n64, sval ); 
00057  
00058  * Once inserted, the values can be retrieved back, also by a scanf like function
00059 
00060  * VALUES_scan( &val, "%hhu%hd%d%qd%s", &_n8, &_n16, &_n32, &_n64, &_sval );
00061 
00062  * Note that the type specifiers of values retrieved must be strictly the same as that of the values inserted previously.
00063 
00064  * Type specifiers:
00065 
00066  * %hhd - signed byte (int8_t)
00067  * %hhu - unsigned byte (uint8_t)
00068  * %hd  - signed short (int16_t)
00069  * %hhu - unsigned short (uint16_t)
00070  * %d   - signed integer (int32_t)
00071  * %u   - unsigned integer (uint32_t)
00072  * %qd  - signed long long (int64_t)
00073  * %qu  - unsiend long long (uint64_t
00074  * %s   - null terminated string pointer
00075  * %p   - void pointer.
00076  *
00077  * @{
00078  */
00079 
00080  typedef struct tagVALUES {
00081   ARRAY values;
00082 } VALUES;
00083 
00084 
00085 /**
00086  * @brief initialises a typed tuple of values object
00087  */
00088 M_INLINE int VALUES_init( VALUES *val)
00089 {
00090   return ARRAY_init( &val->values, sizeof(VAL), 10);
00091 }
00092  
00093 /**
00094  * @brief frees a typed tuple of values object
00095  */
00096 M_INLINE void VALUES_free( VALUES *val)
00097 {
00098   ARRAY_free( &val->values );
00099 }
00100 
00101 /**
00102  * @brief insert typed tuple of values
00103  * @param val -  the VALUES object that is modified
00104  * @param format - Format specifier (for details see documentation of VALUES class)
00105  * @param ap -
00106  */
00107 int VALUES_printv( VALUES *val, const char *format, va_list ap );
00108 
00109 /**
00110  * @brief retrieve typed tuple of values; like scanf receives varying number of arguments
00111  * @param val -   the VALUES object that is modified 
00112  * @param format -  Format specifier (for details see documentation of VALUES class) 
00113  * @param ap -  va_list - pointer to stack frame of variadic function. 
00114  */
00115 int VALUES_scanv( VALUES *val, const char *format, va_list ap );
00116 
00117 /**
00118  * @brief set typed tuple of values; like prinf receives varying number of arguments 
00119  * @param val -  the VALUES object that is modified 
00120  * @param format - Format specifier (for details see documentation of VALUES class) 
00121  * @param ap - va_list - pointer to stack frame of variadic function.
00122  */
00123 M_INLINE int VALUES_print( VALUES *val, const char *format, ... )
00124 {
00125   va_list vlist;
00126   va_start( vlist, format );
00127   return VALUES_printv( val,  format, vlist );
00128 }
00129 
00130 /**
00131  * @brief retrieve typed tuple of values; like scanf receives varying number of arguments 
00132  * @param val -  the VALUES object that is modified 
00133  * @param format - Format specifier (for details see documentation of VALUES class) 
00134  */
00135 M_INLINE int VALUES_scan(  VALUES *val, const char *format, ... )
00136 {
00137   va_list vlist;
00138   va_start( vlist, format );
00139   return VALUES_scanv( val, format, vlist );
00140 }
00141 
00142 M_INLINE VAL * VALUES_at( VALUES *val, size_t pos)
00143 {
00144   return (VAL *) ARRAY_at( &val->values, pos );
00145 }
00146 
00147 
00148 M_INLINE size_t VALUES_size( VALUES *val)
00149 {
00150   return ARRAY_size( &val->values );
00151 }
00152 
00153 /**
00154  * @}
00155  */
00156 
00157 /*
00158   %format 
00159 
00160          hhd - int8_t
00161          hhu - uint8_t
00162 
00163          hd -  int16_t
00164          hu -  uint16_t
00165 
00166          d  -  int32_t
00167          u  -  uint32_t
00168 
00169          qd -  int64_t
00170          qu -  uint64_t
00171 
00172          s  -  null terminated string
00173          p  -  pointer value.
00174 */
00175          
00176 
00177 #endif
00178 
00179