Hi, I'm writing a numerical simulation and I'm trying to keep the code as generic as possible in order to be able to adapt it and reuse parts of it later. I'm looking for a suitable way to manage the output of intermediate results and the progress of my algorithm.
My first approach was just to have one class that organizes the output streams. However, this class needs to collect data from several classes that are used in the algorithm - so the output commands start to clutter my whole code.
Can someone suggest a design or a tool that would help me manage the output?
They will all share a common interface now, and you could even make a vector of pointers to each algorithm and treat them in the same way. FOr example:
int main()
{
typedef std::vector<Algorithm*> Algorithms;
Algorithms algorithms;
algorithms.push_back( new NewtonsMethod() );
algorithms.push_back( new SecantMethod() );
algorithms.push_back( new FirstOrderFilter() );
while (true)
{
double in;
std::cin >> in;
if (in == 0.0) break;
for (Algorithms::iterator it = algorithms.begin(); it !+ algorithms.end(); ++it)
(*it)->AddInput(in);
}
for (Algorithms::iterator it =algorithms.begin(); it != algorithms.end(); ++it)
(*it)->Process();
}
One more question regarding this design: I would like my simulation code to save detailed intermediate results to text files. I need more information than just the output vetors for each algorithm step, i.e. I'd like each algorithm to save certain intermediate steps.
Of course I could have an ostream that is passed as a reference to each algorithm; then the algorithm simply writes to the stream. However, I don't think this is a good choice - maybe at some point I'd like to reuse the algorithms without any output which would require me to change the implementation of the whole algorithm.
Can you suggest a nice way to do this and keep the implementation of the algorithm independent from the choice which results I'd like to save to a text file? Should I use some kind of logging tool (I've never done this)?
I would define my own class for IO. I made one at one point, but can't find it now. I overloaded the << and >> operators so it could be used just like an ostream object. It was something like this:
> Of course I could have an ostream that is passed as a reference to each algorithm;
> then the algorithm simply writes to the stream.
> However, I don't think this is a good choice - maybe at some point I'd like to reuse the algorithms
> without any output which would require me to change the implementation of the whole algorithm.