/core/application.d

http://github.com/wilkie/djehuty · D · 149 lines · 87 code · 33 blank · 29 comment · 4 complexity · 94c7e72d07a562bbe402c810f78b86d9 MD5 · raw file

  1. /*
  2. * application.d
  3. *
  4. * This file contains the logic behind the main Application class.
  5. *
  6. * Author: Dave Wilkinson
  7. * Originated: May 20th, 2009
  8. *
  9. */
  10. module core.application;
  11. import core.string;
  12. import core.unicode;
  13. import core.system;
  14. import core.main;
  15. import core.arguments;
  16. import core.event;
  17. import core.definitions;
  18. import platform.application;
  19. import io.console;
  20. import analyzing.debugger;
  21. // Description: This class represents the application instance.
  22. abstract class Application : Responder {
  23. this() {
  24. // go by classinfo to the application name
  25. ClassInfo ci = this.classinfo;
  26. string className = ci.name.dup;
  27. string[] foo = split(className, '.');
  28. className = foo[$-1];
  29. this(className);
  30. }
  31. this(string appName) {
  32. this._appName = appName.dup;
  33. Djehuty.application = this;
  34. }
  35. // Properties //
  36. // Description: This function will return the name of the application, which is used to signify directory structures and executable names.
  37. // Returns: The application name.
  38. string name() {
  39. return _appName.dup;
  40. }
  41. // Description: This function will return true when the application being executed has been installed and is running from the installation directory.
  42. // Returns: Will return true when the app being ran has been installed, and false otherwise.
  43. bool isInstalled() {
  44. // return true when the executable currently being executed is
  45. // located in the filesystem's installed binaries directory
  46. return (System.FileSystem.binaryDir() == System.FileSystem.applicationDir());
  47. }
  48. void arguments(Arguments argInstance) {
  49. _arguments = argInstance;
  50. }
  51. Arguments arguments() {
  52. return _arguments;
  53. }
  54. // Overrides //
  55. override char[] toString() {
  56. return _appName;
  57. }
  58. // Events //
  59. void run() {
  60. static bool _run = false;
  61. if (!_run) {
  62. Djehuty.start();
  63. _run = true;
  64. start();
  65. // If no event controllers are in play, then end
  66. if (isZombie) {
  67. exit(_platformAppController.exitCode);
  68. }
  69. }
  70. }
  71. // Description: This event will be fired when the application has
  72. // finished loading.
  73. void onApplicationStart() {
  74. }
  75. // Description: This event will be fired when the application is about
  76. // to close.
  77. void onApplicationEnd() {
  78. }
  79. // Description: Detects whether or not the application is a Zombie app;
  80. // that is, whether or not it is in a state of no improvement and is
  81. // merely sucking up resources.
  82. bool isZombie() {
  83. return true;
  84. }
  85. void exit(uint code) {
  86. shutdown();
  87. Djehuty.end(code);
  88. }
  89. protected:
  90. string _appName;
  91. Arguments _arguments;
  92. override bool raiseSignal(uint signal) {
  93. Debugger.raiseSignal(signal);
  94. return false;
  95. }
  96. void shutdown() {
  97. }
  98. void start() {
  99. }
  100. void end(uint exitCode) {
  101. }
  102. private:
  103. ApplicationController _platformAppController;
  104. // Silly wrapper to call start() due to a compiler bug
  105. package final void onPreApplicationStart() {
  106. _platformAppController = ApplicationController.instance;
  107. _platformAppController.start();
  108. }
  109. package final void onPostApplicationEnd(uint exitCode) {
  110. end(exitCode);
  111. if (_platformAppController !is null) {
  112. _platformAppController.exitCode = exitCode;
  113. _platformAppController.end();
  114. }
  115. }
  116. }