annotate mde/scheduler/InitFunctions.d @ 26:611f7b9063c6

Changed the licensing and removed a few dead files. Changed licensing to "GPL version 2 or later" to avoid future compatibility issues. Also a unittest fix to the previous commit. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 03 Apr 2008 18:15:02 +0100
parents 2c28ee04a4ed
children 0aa621b3e070
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
1 /* LICENSE BLOCK
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
2 Part of mde: a Modular D game-oriented Engine
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
4
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
5 This program is free software: you can redistribute it and/or modify it under the terms
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
6 of the GNU General Public License as published by the Free Software Foundation, either
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
7 version 2 of the License, or (at your option) any later version.
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
8
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
11 See the GNU General Public License for more details.
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
12
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
13 You should have received a copy of the GNU General Public License
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
15
25
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 /** This module is responsible for calling all init functions.
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 *
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18 * It is also responsible for setting up all scheduled functions for now. */
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19 module mde.scheduler.InitFunctions;
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21 static import mde.gl;
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23 import tango.util.log.Log : Log, Logger;
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24 static this() {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
25 logger = Log.getLogger ("mde.scheduler.InitFunctions");
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26 }
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
27
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28 /** Should be called by an init function when a failure occurs. */
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 void setInitFailure () {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30 initFailure = true;
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 }
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 package:
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 /** Represents all functions to be called for a particular init stage. */
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 struct InitStage
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38 alias void function() InitFunction; /// Alias
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 /** Add a function to be called during this init stage.
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 *
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42 * Called in order added when not threaded (reverse order for cleanup).
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 *
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44 * Exceptions should never be thrown, since each function may run as a thread, and catching
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 * thread exceptions is not guaranteed to work. Log a message, call setFailure() and return
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 * instead. */
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47 void addFunc (InitFunction f) {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48 funcs ~= f;
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 }
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51 InitFunction[] funcs = [];
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 }
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
54 InitStage init; // all functions called during init (all should be thread-safe)
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
55 InitStage cleanup; // all functions called during cleanup (all should be thread-safe)
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 bool initFailure = false; // set on failure (throwing through threads isn't a good idea)
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
59 private:
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60 Logger logger;
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61 const FAIL_MSG = "Init function failed: ";
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 // Template to call function, catching exceptions:
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63 void initInput(alias Func) () {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
64 try {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
65 Func();
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
66 } catch (Exception e) {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
67 logger.fatal (FAIL_MSG ~ e.msg);
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68 initFailure = true;
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
69 }
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
70 }