Teuchos - Trilinos Tools Package Version of the Day
|Constructor: inject dependency on TimeMonitor into CommandLineProcessor. |
Injects run-time dependency of a class on TimeMonitor.
Classes and functions with the name "TimeMonitorSurrogate" in them let CommandLineProcessor optionally call TimeMonitor::summarize(), without needing to know that the TimeMonitor class exists. This allows Teuchos to put CommandLineProcessor in a separate package from TimeMonitor. We want to do this because TimeMonitor depends on Comm, and is therefore in the TeuchosComm subpackage (which depends on TeuchosCore), but CommandLineProcessor is in a different subpackage which does not depend on Comm.
The TimeMonitorSurrogateImplInserter class' constructor ensures that CommandLineProcessor gets informed about TimeMonitor even before the program starts executing main(). This happens automatically, without changes to main(), because we declare an instance of this class in the header file. If the TeuchosComm subpackage was built and its libraries were linked in, CommandLineProcessor will know about TimeMonitor.
This is an instance of the Dependency injection design pattern. CommandLineProcessor is not supposed to know about TimeMonitor, because CommandLineProcessor's subpackage does not depend on TimeMonitor's subpackage. Thus, CommandLineProcessor interacts with TimeMonitor through the TimeMonitorSurrogate interface. TimeMonitorSurrogateImplInserter "injects" the dependency at run time, if the TeuchosComm subpackage was enabled and the application linked with its libraries.
Teuchos developers could imitate the pattern of this class in order to use TimeMonitor's class methods (such as summarize()) from any other class that does not depend on the TeuchosComm subpackage.