On 25 sep, 14:11, ravi <gototh...@gmail.com> wrote:
> Thanks Deniau.
>
> On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.com> wrote:
>
>
>
> > On 25 sep, 07:03, ravi <gototh...@gmail.com> wrote:
>
> > > Hello everybody,
>
> > > I am writing a small application which does some work before the user
> > > main function starts execution.
>
> > > I am trying to #define the main function.
>
> > > But the problem is that,
>
> > > the main () function by user may be of different types
> > > 1. main()
>
> > not conformant -> int main(void)
>
> > > 2.main(int argc)
>
> > not conformant
>
> > > 3. main(int argc, char *argv[])
>
> > // prelude, already posted here many times
> > #define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
> > #define COS_PP_CAT_(a,b) a ## b
> > #define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
> > #define COS_PP_NARG_N_( \
> > _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
> > _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
> > _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
> > _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
> > _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
> > _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
> > _61,_62,_63,N,...) N
>
> Where r u calling this macro COS_PP_NARG_N_(__VA_ARGS__).
> How does it replace _1/_2/_3......... how it detects the count of
> arguments?
> please clarify me.
Oops, I usual with untested code some macros are missing (googleing
this newsgroup or comp.std.c would have pointed my OP on this macro
some years ago):
#define COS_PP_NARG(...) COS_PP_NARG_(__VA_ARGS__,COS_PP_REVSEQ_N(),)
#define COS_PP_REVSEQ_N() \
63,62,61, \
60,59,58,57,56,55,54,53,52,51, \
50,49,48,47,46,45,44,43,42,41, \
40,39,38,37,36,35,34,33,32,31, \
30,29,28,27,26,25,24,23,22,21, \
20,19,18,17,16,15,14,13,12,11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
>
> > #define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))
>
> > // your problem
> > #define main(...) \
> > COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)
>
> > #define MAIN_1() \
> > main(void) { \
> > int user_main(void); \
> > your_main(0,0); \
> > user_main(); \
> > } \
> > int user_main(void)
>
> > #define MAIN_2(ARGC,ARGV) \
> > main(int argc, char *argv[]) { \
> > int user_main(ARGC,ARGV); \
> > your_main(argc,argv); \
> > user_main(argc,argv); \
> > }
> > int user_main(ARGC,ARGV)
>
> > int main(int my_argc, char *my_argc[])
> > {
> > // code using my_argc and my_argv after your_main() has been called.
>
> > }
>
> > I just wrote this code on the fly, so it is untested and assumes the
> > availability of c99 variadic macros.
>
> > I use very often this kind of macro dispatch based on its number of
> > argument in COS (macros with optional arguments). PP_NARG has been
> > left as posted here in the past, but you could reduce the maximum
> > number of argument for you case.
>
> > a+, ld.