help with automatic code generation

Discussion in 'C++' started by JoshforRefugee, Jan 31, 2007.

  1. heard that we can do automatic code generation using macros, but not
    sure how can I pursue this. Here is my problem.

    In my env, I have class A,B and C. All of them has constructors, and
    few common methods, like reset, and execute.

    now my env(main) class actually is where I am creating this objects.
    in .h
    A myA;
    B myB;
    C myC;

    and later I am calling methods in my .C file
    myA.reset();
    myB.reset();
    myC.reset();
    myA.execute();
    .....
    myC.execute();

    now let say if I add Class D, I have to modify my env.h and env.C. And
    I am trying to prevent that.
    one way i was thinking of doing this, is to write macros:
    so I can do:
    REGISTER_OBJECT("A","myA");
    REGISTER_RESET("A","myA",reset);
    REGISTER_EXECUTE("A", "myA", execute)
    .............

    May be using macros might be one way to do this. I am really confuse
    how I can I write this macros. Please guide me.

    Thank you,
     
    JoshforRefugee, Jan 31, 2007
    #1
    1. Advertising

  2. JoshforRefugee

    Ole Nielsby Guest

    JoshforRefugee <> wrote:

    > heard that we can do automatic code generation using macros, but not
    > sure how can I pursue this. Here is my problem.
    >
    > In my env, I have class A,B and C. All of them has constructors, and
    > few common methods, like reset, and execute.


    Don't bother with macros for things that can be done with simple
    OO techniques. Your classes should probably derive from a base
    class that declares the common methods as virtuals.
     
    Ole Nielsby, Jan 31, 2007
    #2
    1. Advertising

  3. problem is I own env class, and my group owns other classes. and I
    don't want to be bother by creating class and registering its
    function, every time someone adds new class or wants to create new
    object for same class.

    how can I do this?
     
    JoshforRefugee, Jan 31, 2007
    #3
  4. JoshforRefugee

    Grizlyk Guest

    JoshforRefugee wrote:
    >
    > how can I do this?
    >

    What do you want - new names for single class? Or what?

    --
    Maksim A Polyanin
     
    Grizlyk, Jan 31, 2007
    #4
  5. just an interface for users so that they can register their classes
    with my "main/env" class.

    What I am doing, is collecting all classes out there, written by users
    and constructing objects for them, and calling their methods (reset,
    run). Users derived their class from base object class. Right now,
    when someone adds new class, I have to open up "main/env" class and
    create object for new class, and call common methods (reset, run) for
    that new object.

    My goal is to not manually touch "main/evn" class, everytime someone
    creates new class. Let have some hooks that users can use so that they
    can create these class and reset/run methods will be call for
    them.
     
    JoshforRefugee, Jan 31, 2007
    #5
  6. On Jan 31, 7:16 pm, "JoshforRefugee" <> wrote:
    > just an interface for users so that they can register their classes
    > with my "main/env" class.
    >
    > What I am doing, is collecting all classes out there, written by users
    > and constructing objects for them, and calling their methods (reset,
    > run). Users derived their class from base object class. Right now,
    > when someone adds new class, I have to open up "main/env" class and
    > create object for new class, and call common methods (reset, run) for
    > that new object.
    >
    > My goal is to not manually touch "main/evn" class, everytime someone
    > creates new class. Let have some hooks that users can use so that they
    > can create these class and reset/run methods will be call for
    > them.


    Can't you do something like this:

    class env {
    std::vector<Base*> classes;
    public:
    void register(Base& b) {
    classes.push_back(&b);
    }
    void resetAll() {
    for (size_t i = 0; i < classes.size(); ++i)
    classes->reset();
    }
    };

    I kind of assume here that you'll only have one instance of env (a
    singleton comes to mind) but even if you don't you might be able to
    use this approach. The idea is that the new classes registers
    themselves with your env instance either when constructed or soon
    after.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 1, 2007
    #6
  7. that is good help. Thank you Erik.
    that will take care of registering functions for new classes.
    How about creating object for this class. Can I automate that?

    so now in my top class, where I am doing

    class top {
    env myEnv;
    A myA;
    B myB;
    C myC; //can I automate this creating
    public:
    void run() {
    myEnv.resetAll();
    myEnv.execute();
    }

    };

    something like this in my classes will be very useful:

    DECLARE (A, "myA");
    DECLARE (A, "myAA");
    class A {
    .......

    }

    DECLARE(B, "myB");
    class B {
    ....
    }

    so basically user has control how many objects to create.
     
    JoshforRefugee, Feb 1, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasmus Fogh

    Automatic Code Generation

    Rasmus Fogh, Oct 5, 2003, in forum: Python
    Replies:
    0
    Views:
    327
    Rasmus Fogh
    Oct 5, 2003
  2. Rasmus Fogh

    Re: Automatic Code Generation

    Rasmus Fogh, Oct 6, 2003, in forum: Python
    Replies:
    1
    Views:
    318
    John J. Lee
    Oct 6, 2003
  3. JoshforRefugee

    help with automatic code generation

    JoshforRefugee, Jan 31, 2007, in forum: C++
    Replies:
    0
    Views:
    411
    JoshforRefugee
    Jan 31, 2007
  4. fpga_designe

    Automatic VHDL generation from C code

    fpga_designe, May 15, 2009, in forum: VHDL
    Replies:
    1
    Views:
    689
    fpga_designe
    May 15, 2009
  5. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    350
    John W. Long
    Nov 24, 2003
Loading...

Share This Page