Pyrogenesis  trunk
ScriptEngine.h
Go to the documentation of this file.
1 /* Copyright (C) 2016 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_SCRIPTENGINE
19 #define INCLUDED_SCRIPTENGINE
20 
21 #include "ScriptTypes.h"
22 #include "ps/Singleton.h"
23 
24 /**
25  * A class using the RAII (Resource Acquisition Is Initialization) idiom to manage initialization
26  * and shutdown of the SpiderMonkey script engine. It also keeps a count of active script runtimes
27  * in order to validate the following constraints:
28  * 1. JS_Init must be called before any ScriptRuntimes are initialized
29  * 2. JS_Shutdown must be called after all ScriptRuntimes have been destroyed
30  */
31 
32 class ScriptEngine : public Singleton<ScriptEngine>
33 {
34 public:
36  {
37  ENSURE(m_Runtimes.size() == 0 && "JS_Init must be called before any runtimes are created!");
38  JS_Init();
39  }
40 
42  {
43  ENSURE(m_Runtimes.size() == 0 && "All runtimes must be destroyed before calling JS_ShutDown!");
44  JS_ShutDown();
45  }
46 
47  void RegisterRuntime(const JSRuntime* rt) { m_Runtimes.push_back(rt); }
48  void UnRegisterRuntime(const JSRuntime* rt) { m_Runtimes.remove(rt); }
49 
50 private:
51 
52  std::list<const JSRuntime*> m_Runtimes;
53 };
54 
55 #endif // INCLUDED_SCRIPTENGINE
~ScriptEngine()
Definition: ScriptEngine.h:41
void RegisterRuntime(const JSRuntime *rt)
Definition: ScriptEngine.h:47
ScriptEngine()
Definition: ScriptEngine.h:35
A class using the RAII (Resource Acquisition Is Initialization) idiom to manage initialization and sh...
Definition: ScriptEngine.h:32
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:287
std::list< const JSRuntime * > m_Runtimes
Definition: ScriptEngine.h:52
void UnRegisterRuntime(const JSRuntime *rt)
Definition: ScriptEngine.h:48
Definition: Singleton.h:34