macro to call lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sim fasaei
    New Member
    • Jan 2012
    • 4

    macro to call lists

    Hej Guys
    I have problem to make some macros to my own project I want to make some
    Macro which it can make a several list automatically as example:
    Pseudo code:
    #define A 0x01
    #define B 0x02
    #define C 0x04
    Etc..

    #define ENABEL_A 1
    #define ENABEL_B 0
    #define ENABEL _C 2
    etc…

    If (ENABEL_A == 1 ) ADD_TO_LIST_1(A );
    else ADD_TO_LIST_2(A );
    etc…

    result shud bee

    LIST_1 {A,B,C,D}
    LIST_2 {B,C,D,F}
    LIST_3 {E,F,D,G} etc….

    And when I call another macro RUN_TRUE_LIST_1

    It should runs true each list and call a function like:
    Calling(A);
    calling(B);
    calling (C); etc.

    So first a macro to make my lists
    Second a macro to runs trues those lists. Please help me.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    What is the problem again?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What language are you using?

      Comment

      • sim fasaei
        New Member
        • Jan 2012
        • 4

        #4
        Im using C and the problem is :

        Comment

        • sim fasaei
          New Member
          • Jan 2012
          • 4

          #5
          Sorry about last message her is the problem?

          I need a macro to make my_list, so when i call MAKE_MYLIST(Lis t1) it makes list1.
          my_list{

          list_1{elm1,elm 2,elm3},
          list_2{elm4,elm 5,elm6},
          list_3{elm5,elm 7,elm8},
          }
          Then i need a macro to add elements to specific list, so when i call ADD_ELM_TO_MYLI ST(List1,elm4) it adds elm4 to List_1.
          And i need a macro to removes elements from specific list, so when i call REMOVE_ELM_FROM _MYLIST(List1,e lm4) it removes elm4 from List_1.
          Then i need a macro to put an element from random list as parameter in a function , so when I call ACT_ON_THIS(Lis t1,elm2) it calls a function "action(elm 2)".

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Do you expect MAKE_MYLIST to be called from executable code or do you expect this macro to expand into into a variable definition with an initializer?

            I need a macro to make my_list, so when i call MAKE_MYLIST(Lis t1) it makes list1.
            Code:
            my_list{
            list_1{elm1,elm2,elm3},
            list_2{elm4,elm5,elm6},
            list_3{elm5,elm7,elm8},
            }
            Are you sure? The macro expansion you show here is not valid C.

            Some further questions:
            • What is the data type for my_list?
            • How did the macro know to use the name my_list?
            • How did the macro know there were three items, and that they were named list_1, list_2, list_3?
            • How did the macro know there were three subitems for each item, and how did it know what their names were?


            And the big question -- why do you wish to do this with macros instead of C instructions?

            Comment

            • sim fasaei
              New Member
              • Jan 2012
              • 4

              #7
              Iexpect this macro to expand into into a variable definition with an initializer:


              enum{
              elm1=1,
              elm2,
              elm3,
              elm4,
              .
              .
              .
              last_elm
              } MY_ELM_ID


              typedef struct
              {
              MY_ELEM_ID lists[];
              } MY_ELM_LIST;

              #define MAKE_MYLIST(nam e) \
              const MY_ELM_LIST name[] = {

              };
              And yes thats a god question why not C?
              My intention was to make those lists or list at compiling time, so I easily can add or removes list\lists or elements from a header file.
              let us sig like a group of switches.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                list_1, list_2, and list_3 from your reply #5 correspond to that description of MAKE_MYLIST.
                • But where did my_list come from? It is an array of arrays.
                • How did the macro know how many elms to put in each list?
                • How did the macro know which elms to put in each list?

                I can't be sure until I understand exactly what you're trying to do, but I suspect you would find it easier to accomplish this at execution time. Take a look at the Arrays Revealed article to see how to dynamically allocate a one-dimensional or two-dimensional array. (You will have to replace the C++ new keyword with a call to the malloc library function.)

                Comment

                Working...