PageRenderTime 14ms CodeModel.GetById 6ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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