Emit information from a library without run-time cost if none is wanted

Viewed 40

I am author of a library with a number of optimization algorithms, in which I already invested quite a bit of profiling/tuning. I am currently writing front-end programs for this library.

At the moment, the library routines themselves are pretty much black boxes. Consider a method along the lines of bool fit(vector_t const& data, double targetError). They work and do what I want them to do, but for the frontends, a bit of runtime information would be nice. For example, it would be nice if information like "current error" or "number of iterations left" could be displayed. I do not want a simple if (verbose) cerr << "Info\n"; pattern, since the library should be equally usable in a GUI environment.

I deliberately write could, because I want to keep the impact of this information emission as low as possible. How can I define and implement an interface, that

  • Emits an object with run-time information whenever an observer is registered
  • has minimal run-time costs with regards to the algorithm if an information object is emitted, and
  • has close to no run-time costs if no observer is registered?

Basically, the run-time costs of this optional introspection should be as low as possible, and close to zero if no introspection is wanted. How can this be achieved? Do libraries exist for this? (I guess the answer is yes, and probably hidden in the Boost project, but without known what to look for…)

2 Answers
Related