Alternative to Transport Variables

Discussion in 'C++' started by jimmy, Jun 8, 2005.

  1. jimmy

    jimmy Guest

    Hi,

    I have a main loop like this:

    while (true) {
    receive();
    update();
    draw();
    }

    Each stage (i.e. receive, update, draw) uses info from the previous
    stage. I am currently passing this info with "transport variables".
    These variables are set in one stage, and in the next, read and
    cleared.

    I find this very cumbersome and prone to errors (especially if I forget
    to clear a variable). Is there some sort of design pattern or idiom
    that could help?

    Note: Each stage is infact traversing a tree so someLeaf.draw() would
    not be called immediately after someLeaf.update().

    -Jimmy
     
    jimmy, Jun 8, 2005
    #1
    1. Advertising

  2. jimmy

    msalters Guest

    jimmy schreef:
    > Hi,
    >
    > I have a main loop like this:
    >
    > while (true) {
    > receive();
    > update();
    > draw();
    > }
    >
    > Each stage (i.e. receive, update, draw) uses info from the previous
    > stage. I am currently passing this info with "transport variables".
    > These variables are set in one stage, and in the next, read and
    > cleared.
    >
    > I find this very cumbersome and prone to errors (especially if I forget
    > to clear a variable). Is there some sort of design pattern or idiom
    > that could help?


    What's wrong with objects?

    Assuming your transport var hass class C,
    why not write
    C receive();
    C update( C c ); // returns c;
    void draw( C c );

    C::~C will of course clear the variable. If copying C is too expensive,
    you can implement C as a C_impl* and a share count.

    HTH,
    Michiel Salters
     
    msalters, Jun 8, 2005
    #2
    1. Advertising

  3. jimmy

    Kristo Guest

    msalters wrote:
    > jimmy schreef:
    > > Hi,
    > >
    > > I have a main loop like this:
    > >
    > > while (true) {
    > > receive();
    > > update();
    > > draw();
    > > }
    > >
    > > Each stage (i.e. receive, update, draw) uses info from the previous
    > > stage. I am currently passing this info with "transport variables".
    > > These variables are set in one stage, and in the next, read and
    > > cleared.
    > >
    > > I find this very cumbersome and prone to errors (especially if I forget
    > > to clear a variable). Is there some sort of design pattern or idiom
    > > that could help?

    >
    > What's wrong with objects?
    >
    > Assuming your transport var hass class C,
    > why not write
    > C receive();
    > C update( C c ); // returns c;
    > void draw( C c );
    >
    > C::~C will of course clear the variable. If copying C is too expensive,
    > you can implement C as a C_impl* and a share count.


    Why copy at all then? I suggest passing by reference instead. The
    declarations for the functions might look like this:

    C receive();
    void update(C &c);
    void draw(C &c);

    And the resulting code would be:

    while (true)
    {
    C c = receive();
    update(c);
    draw(c);
    }

    The variable c goes out of scope at the end of the loop body and its
    destructor can clean up whatever it needs to. Also, if copying a C
    object is expensive, try to facilitate the return-by-value optimization
    in the receive function. See FAQ 10.9 for details if you don't know
    what that is.

    Kristo
     
    Kristo, Jun 8, 2005
    #3
    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. Neil Zanella
    Replies:
    2
    Views:
    10,297
  2. Thomas K. Balthasar

    Timing Models; here Transport

    Thomas K. Balthasar, Feb 10, 2004, in forum: VHDL
    Replies:
    1
    Views:
    523
    fabbl
    Feb 11, 2004
  3. kitcha
    Replies:
    3
    Views:
    2,991
    Charles Bailey
    Nov 24, 2005
  4. Jason
    Replies:
    1
    Views:
    3,793
    Latha Rajeev
    Jun 25, 2003
  5. Arthur Nesterovsky

    ANN: How to create alternative transport via MQ for web services.

    Arthur Nesterovsky, Oct 2, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    118
    Arthur Nesterovsky
    Oct 2, 2003
Loading...

Share This Page