BeRTOS
cmd_hunk.h
Go to the documentation of this file.
00001 
00069 #ifndef CMD_HUNK_H
00070 #define CMD_HUNK_H
00071 
00072 #include "parser.h"
00073 
00074 // Bring in the Boost Preprocess Library
00075 #include <boost/preprocessor/library.hpp>
00076 
00077 #define HUNK_INDEX_FOR_NIL      0
00078 #define HUNK_INDEX_FOR_string   1
00079 #define HUNK_INDEX_FOR_long     2
00080 #define HUNK_ARRAY_LETTERS      (3, (NIL, s, l))
00081 #define HUNK_ARRAY_STRINGS      (3, ("", "s", "d"))
00082 
00083 // Transform int->l, float->f, etc.
00084 #define HUNK_TYPE_LETTER(s, _, type) \
00085     BOOST_PP_CAT(HUNK_INDEX_FOR_, type) \
00086     
00087 
00088 #define HUNK_TRANSFORMER(_, array, elem) \
00089     BOOST_PP_ARRAY_ELEM(elem, array) \
00090     
00091 
00092 #define HUNK_SEQ_TRANS_ARRAY(seq, array) \
00093     BOOST_PP_SEQ_TRANSFORM(HUNK_TRANSFORMER, array, seq) \
00094     
00095 
00096 #define HUNK_PARAM(_, n, seq)    \
00097     args_results[n+1]. BOOST_PP_SEQ_ELEM(n, seq) \
00098     
00099 
00100 #define HUNK_RESULT(_, n, seq)    \
00101     &args_results[n]. BOOST_PP_SEQ_ELEM(n, seq) \
00102     
00103 
00104 #define HUNK_IDENTITY(_, dummy, x)  x
00105 #define CMD_HUNK_TEMPLATE(func)         cmd_##func###_template
00106 
00107 #define DECLARE_CMD_HUNK_2(func, name, param_types, result_types, flags)    \
00108     static ResultCode cmd_##name##_hunk(parms args_results[]) \
00109     { \
00110         return cmd_##func( \
00111                BOOST_PP_ENUM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(param_types)),  HUNK_PARAM,  HUNK_SEQ_TRANS_ARRAY(param_types, HUNK_ARRAY_LETTERS)) \
00112                BOOST_PP_COMMA_IF(BOOST_PP_AND(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(param_types)), BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(result_types)))) \
00113                BOOST_PP_ENUM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(result_types)), HUNK_RESULT, HUNK_SEQ_TRANS_ARRAY(result_types, HUNK_ARRAY_LETTERS)) \
00114         ); \
00115     } \
00116     const struct CmdTemplate CMD_HUNK_TEMPLATE(name) = { \
00117         #name, \
00118         BOOST_PP_SEQ_FOR_EACH(HUNK_IDENTITY, _, HUNK_SEQ_TRANS_ARRAY(param_types, HUNK_ARRAY_STRINGS)),  \
00119         BOOST_PP_SEQ_FOR_EACH(HUNK_IDENTITY, _, HUNK_SEQ_TRANS_ARRAY(result_types, HUNK_ARRAY_STRINGS)), \
00120         cmd_##name##_hunk, \
00121         flags \
00122     } \
00123     
00124 
00125 #define DECLARE_CMD_HUNK(func, param_types, result_types)    \
00126     DECLARE_CMD_HUNK_2(func, func, \
00127                        BOOST_PP_SEQ_TRANSFORM(HUNK_TYPE_LETTER, _, param_types), \
00128                        BOOST_PP_SEQ_TRANSFORM(HUNK_TYPE_LETTER, _, result_types), \
00129                        0) \
00130     
00131 
00132 #define DECLARE_CMD_HUNK_NAME(func, name, param_types, result_types)    \
00133     DECLARE_CMD_HUNK_2(func, name, \
00134                        BOOST_PP_SEQ_TRANSFORM(HUNK_TYPE_LETTER, _, param_types), \
00135                        BOOST_PP_SEQ_TRANSFORM(HUNK_TYPE_LETTER, _, result_types), \
00136                        0) \
00137     
00138 
00139 #define DECLARE_CMD_HUNK_FLAGS(func, param_types, result_types, flags)    \
00140     DECLARE_CMD_HUNK_2(func, func, \
00141                        BOOST_PP_SEQ_TRANSFORM(HUNK_TYPE_LETTER, _, param_types), \
00142                        BOOST_PP_SEQ_TRANSFORM(HUNK_TYPE_LETTER, _, result_types), \
00143                        flags) \
00144     
00145 
00146 #endif