shell.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file shell.h
  3. /// \brief The "shell" is the model's interface to the world
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date: 2018-02-02 18:01:35 +0100 (ven, 02 fév 2018) $
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #ifndef LPJ_GUESS_SHELL_H
  10. #define LPJ_GUESS_SHELL_H
  11. #include "gutil.h"
  12. /// A printf-style function for sending messages from LPJ-GUESS to the user.
  13. /**
  14. * The text is typically sent to the screen and/or a log file. To maintain
  15. * portability of the modular code, please use this function for general
  16. * output instead of the standard C++ printf function.
  17. */
  18. void dprintf(xtring format,...);
  19. /// A printf-style function that sends the user a message and then terminates.
  20. void fail(xtring format,...);
  21. /// Adds data point (x,y) to series 'series_name' of line graph 'window_name'.
  22. /**
  23. * If the series and/or line graph do not yet exist, they are created.
  24. * Functional only when the framework is built as a DLL and linked to the
  25. * LPJ-GUESS Windows Shell (the function may still be called in other
  26. * implementations, but will have no effect).
  27. */
  28. void plot(xtring window_name,xtring series_name,double x,double y);
  29. /// 'Forgets' series and data for line graph 'window_name'.
  30. /**
  31. * Functional only when the framework is built as a DLL and linked to the
  32. * LPJ-GUESS Windows Shell.
  33. */
  34. void resetwindow(xtring window_name);
  35. /// 'Forgets' series and data for all currently-defined line graphs.
  36. /**
  37. * Functional only when the framework is built as a DLL and linked to the
  38. * LPJ-GUESS Windows Shell.
  39. */
  40. void clear_all_graphs();
  41. /// Initiates a 3D view of stand vegetation in the Windows shell
  42. /**
  43. * Functional only when the framework is built as a DLL and linked to the
  44. * LPJ-GUESS Windows Shell.
  45. */
  46. void open3d();
  47. /// Sends data on current stand structure to 3D vegetation plot in the Windows shell
  48. /**
  49. * Functional only when the framework is built as a DLL and linked to the
  50. * LPJ-GUESS Windows Shell.
  51. */
  52. void plot3d(const char* filename);
  53. /// May be called by framework to respond to abort request from the user.
  54. /**
  55. * \returns true if shell has sent an abort request, otherwise false.
  56. */
  57. bool abort_request_received();
  58. /// The interface LPJ-GUESS uses to communicate with the world
  59. /**
  60. * This is an abstract base class, which is sub-classed by
  61. * concrete implementations for different ways of running LPJ-GUESS.
  62. *
  63. * See CommandLineShell which implements this interface for when
  64. * LPJ-GUESS runs as a regular command line client.
  65. */
  66. class Shell {
  67. public:
  68. virtual ~Shell() {}
  69. /// Sends a message to the user somehow and terminates the program
  70. virtual void fail(const char* message) = 0;
  71. /// Sends a message to the user somehow
  72. virtual void log_message(const char* message) = 0;
  73. /// Adds data point (x,y) to series 'series_name' of line graph 'window_name'.
  74. virtual void plot(const char* window_name,
  75. const char* series_name,
  76. double x,
  77. double y) = 0;
  78. /// 'Forgets' series and data for line graph 'window_name'.
  79. virtual void resetwindow(const char* window_name) = 0;
  80. /// 'Forgets' series and data for all currently-defined line graphs.
  81. virtual void clear_all_graphs() = 0;
  82. /// May be called by framework to respond to abort request from the user.
  83. virtual bool abort_request_received() = 0;
  84. /// Initiates a 3D view of stand vegetation in the Windows shell
  85. virtual void open3d() = 0;
  86. /// Sends data on current stand structure to 3D vegetation plot in the Windows shell
  87. virtual void plot3d(const char* filename) = 0;
  88. };
  89. /// A Shell which sends messages to the terminal and log file
  90. /**
  91. * This class ignores the plotting related functions.
  92. */
  93. class CommandLineShell : public Shell {
  94. public:
  95. CommandLineShell(const char* logfile_path);
  96. ~CommandLineShell();
  97. void fail(const char* message);
  98. void log_message(const char* message);
  99. void plot(const char* window_name,
  100. const char* series_name,
  101. double x,
  102. double y);
  103. void resetwindow(const char* window_name);
  104. void open3d();
  105. void plot3d(const char* filename);
  106. void clear_all_graphs();
  107. bool abort_request_received();
  108. private:
  109. FILE* logfile;
  110. };
  111. /// Chooses a global shell object
  112. /**
  113. * Should be called at program start up, before the model starts running.
  114. *
  115. * If LPJ-GUESS is running as a stand-alone program, this is called from
  116. * main().
  117. */
  118. void set_shell(Shell* s);
  119. #endif // LPJ_GUESS_SHELL_H