1 /*
2                                     __
3                                    / _|
4   __ _ _   _ _ __ ___  _ __ __ _  | |_ ___  ___ ___
5  / _` | | | | '__/ _ \| '__/ _` | |  _/ _ \/ __/ __|
6 | (_| | |_| | | | (_) | | | (_| | | || (_) \__ \__ \
7  \__,_|\__,_|_|  \___/|_|  \__,_| |_| \___/|___/___/
8 
9 Copyright (C) 2018-2019 Aurora Free Open Source Software.
10 
11 This file is part of the Aurora Free Open Source Software. This
12 organization promote free and open source software that you can
13 redistribute and/or modify under the terms of the GNU Lesser General
14 Public License Version 3 as published by the Free Software Foundation or
15 (at your option) any later version approved by the Aurora Free Open Source
16 Software Organization. The license is available in the package root path
17 as 'LICENSE' file. Please review the following information to ensure the
18 GNU Lesser General Public License version 3 requirements will be met:
19 https://www.gnu.org/licenses/lgpl.html .
20 
21 Alternatively, this file may be used under the terms of the GNU General
22 Public License version 3 or later as published by the Free Software
23 Foundation. Please review the following information to ensure the GNU
24 General Public License requirements will be met:
25 http://www.gnu.org/licenses/gpl-3.0.html.
26 
27 NOTE: All products, services or anything associated to trademarks and
28 service marks used or referenced on this file are the property of their
29 respective companies/owners or its subsidiaries. Other names and brands
30 may be claimed as the property of others.
31 
32 For more info about intellectual property visit: aurorafoss.org or
33 directly send an email to: contact (at) aurorafoss.org .
34 */
35 
36 module aurorafw.core.appcontext;
37 
38 import aurorafw.core.application;
39 
40 abstract class ApplicationContext : Application
41 {
42 public:
43 	import core.runtime : Runtime;
44 
45 	this(immutable string packageName, string[] args = Runtime.args())
46 	{
47 		super(packageName, args);
48 	}
49 
50 	protected void onStart()
51 	{
52 	}
53 
54 	protected void onClose()
55 	{
56 	}
57 
58 	pragma(inline)
59 	protected void loop()
60 	{
61 		stop();
62 	}
63 
64 	final public void start()
65 	{
66 		onStart();
67 
68 		running = true;
69 		while (running)
70 			loop();
71 
72 		onClose();
73 	}
74 
75 	pragma(inline)
76 	final public void stop()
77 	{
78 		running = false;
79 	}
80 
81 	final public @property bool isRunning()
82 	{
83 		return running;
84 	}
85 
86 	private bool running;
87 	// private OptionHandler _opts;
88 }
89 
90 @system
91 @("Application: context")
92 unittest
93 {
94 	class MyApplicationContext : ApplicationContext
95 	{
96 		this(immutable string packageName, string[] args)
97 		{
98 			super(packageName, args);
99 
100 			assert(packageName == "test");
101 			assert(args == ["app", "--args"]);
102 		}
103 
104 		override void loop()
105 		{
106 			assert(this.isRunning);
107 			this.stop();
108 		}
109 	}
110 
111 	MyApplicationContext app = new MyApplicationContext("test", ["app", "--args"]);
112 	app.start(); //loop just stop the app by default
113 
114 	assert(!app.isRunning);
115 }