Results 1 to 11 of 11

Thread: Best way to design a simulation class?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to design a simulation class?

    In the simulation class, I store a pointer to a facade object in which I call a virtual function whenever I want to provide the application with output.
    Actually, I would try to make it more generic than this. The simulation class doesn't really need to know about external classes; it simply needs callback function pointers. These could all belong to the same facade class, could be multiple classes, could be standalone C++ functions, could be function objects (each of which implements a single callback method). The simulation shouldn't know or care, if you want to make things as decoupled as possible.

    But if you have control over both ends of the code (simulation and controller), you can make whatever level of dependence you want. If you are interfacing to a third-party library where you have no ability to change the simulation code, then you are forced to decouple in one of the ways above.

  2. #2
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best way to design a simulation class?

    Okay, let's assume that we are using a third party library and can only provide it with a function pointer for each callback. What would such a callback function look like if it needs to access an object that can't be accessed globally?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to design a simulation class?

    What would such a callback function look like if it needs to access an object that can't be accessed globally?
    Once upon a time I solved this problem, but I can't remember at the moment. Something to do with a function object class that stored a pointer to another class, but the mapping from a static method (the callback) to a non-static member escapes me.

  4. #4
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best way to design a simulation class?

    And you didn't use a singleton?

  5. #5
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best way to design a simulation class?

    I took a look at an SDL function (SDL_AddTimer) that has to be provided with a callback function, and apparently it takes an extra parameter except from the function pointer, and that parameter is a void pointer. This extra argument is probably intended to be used (if necessary) as a pointer to an object in order to make it possible for the callback function work with more than just static member variables. You can at least bake in as much information as you possibly want into this pointer since it can point to any kind of object.

    Now, SDL is written in C, and I don't know if there is any better way to do this in if you use C++, but at least this is one way in which it can be done.

    Edit: Apparently, this way isn't typesafe, which is explained pretty nicely here. That is why the concept of a slot is introduced, which is already a part of Qt. I started to wonder if there is any way to use Qt to make a slot in some way, which I can then pass to my simulation class in order to make the programming typesafe?
    Last edited by Yes; 27th November 2011 at 00:35.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Best way to design a simulation class?

    If you are going down a non-Qt callback route then you should look at the Boost bind library. Call free functions, static members, member functions, flexible argument handling etc.

  7. #7
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best way to design a simulation class?

    I'm sure Boost bind is great; I have previously been looking at a few other Boost classes and even worked with one of them, and they all seem to be really useful. It's just that – if Boost is used in a project, it becomes yet another large library that you need to have installed on your every computer you want to compile the project on... It seems unnecessarily large if you are only going to use one of its classes in your project, why I would rather use some other signal/slot library instead, like sigslot or libsigc++ (although I have never used these two in particular before), that doesn't take up so much space on you hard disc. Or is there some way to download only the header and source files needed for Boost bind and use them in your project?

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Best way to design a simulation class?

    Boost bind is header only. No extra library needs to be shipped with your program. If you want to avoid Boost then look at how much of TR1 is implemented by your compiler: std::tr1:: function and bind.

Similar Threads

  1. alt enter simulation in qstring
    By Dilshad in forum Newbie
    Replies: 6
    Last Post: 29th December 2010, 05:59
  2. Multilotek - simulation of lottery game
    By Fazer in forum Qt-based Software
    Replies: 0
    Last Post: 8th September 2009, 14:19
  3. physics simulation
    By scrasun in forum General Programming
    Replies: 6
    Last Post: 11th June 2009, 02:19
  4. Replies: 1
    Last Post: 15th April 2008, 23:15
  5. QTimer or recursive calls for simulation?
    By Morea in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2006, 00:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.