views:

59

answers:

2

Hi,

I am developing a C++ application used to simulate a real world scenario. Based on this simulation our team is going to develop, test and evaluate different algorithms working within such a real world scenrio.

We need the possibility to define several scenarios (they might differ in a few parameters, but a future scenario might also require creating objects of new classes) and the possibility to maintain a set of algorithms (which is, again, a set of parameters but also the definition which classes are to be created). Parameters are passed to the classes in the constructor.

I am wondering which is the best way to manage all the scenario and algorithm configurations. It should be easily possible to have one developer work on one scenario with "his" algorithm and another developer working on another scenario with "his" different algorithm. Still, the parameter sets might be huge and should be "sharable" (if I defined a set of parameters for a certain algorithm in Scenario A, it should be possible to use the algorithm in Scenario B without copy&paste).

It seems like there are two main ways to accomplish my task:

  • Define a configuration file format that can handle my requirements. This format might be XML based or custom. As there is no C#-like reflection in C++, it seems like I have to update the config-file parser each time a new algorithm class is added to project (in order to convert a string like "MyClass" into a new instance of MyClass). I could create a name for every setup and pass this name as command line argument.

    • The pros are: no compilation required to change a parameter and re-run, I can easily store the whole config file with the simulation results
    • contra: seems like a lot of effort, especially hard because I am using a lot of template classes that have to be instantiated with given template arguments. No IDE support for writing the file (at least without creating a whole XSD which I would have to update everytime a parameter/class is added)
  • Wire everything up in C++ code. I am not completely sure how I would do this to separate all the different creation logic but still be able to reuse parameters across scenarios. I think I'd also try to give every setup a (string) name and use this name to select the setup via command line arg.

    • pro: type safety, IDE support, no parser needed
    • con: how can I easily store the setup with the results (maybe some serialization?)?, needs compilation after every parameter change

Now here are my questions: - What is your opinion? Did I miss important pros/cons? - did I miss a third option? - Is there a simple way to implement the config file approach that gives me enough flexibility? - How would you organize all the factory code in the seconde approach? Are there any good C++ examples for something like this out there?

Thanks a lot!

+1  A: 

If you don't use XML, it's possible that boost::spirit could short-circuit at least some of the problems you are facing. Here's a simple example of how config data could be parsed directly into a class instance.

Steve Townsend
Thanks, Steve. I haven't seen this before and it looks pretty good for having parameters in config files, but it still seems like a lot of work to get my template arguments in...
Philipp
A: 

I found this website with a nice template supporting factory which I think will be used in my code.

Philipp