Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AutoRooters.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 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_AUTOROOTERS
19 #define INCLUDED_AUTOROOTERS
20 
22 
23 #include "js/jsapi.h"
24 
25 /**
26  * Helper for rooting large groups of script values.
27  * Construct this object, push values into it, and they will all be rooted until this
28  * object is destroyed.
29  * Many of these objects can be used at once, but their lifetimes must be correctly nested.
30  */
32 {
34 public:
35  AutoGCRooter(ScriptInterface& scriptInterface);
36  ~AutoGCRooter();
37 
38  void Push(JSObject* obj) { m_Objects.push_back(obj); }
39  void Push(jsval val) { m_Vals.push_back(val); }
40  void Push(JSIdArray* ida) { m_IdArrays.push_back(ida); }
41 
42  void Trace(JSTracer* trc);
43 private:
46 
47  std::vector<JSObject*> m_Objects;
48  std::vector<jsval> m_Vals;
49  std::vector<JSIdArray*> m_IdArrays;
50  // TODO: add vectors of other value types
51 };
52 
53 #endif // INCLUDED_AUTOROOTERS
void Push(JSIdArray *ida)
Definition: AutoRooters.h:40
void Push(jsval val)
Definition: AutoRooters.h:39
AutoGCRooter(ScriptInterface &scriptInterface)
Definition: AutoRooters.cpp:24
std::vector< JSIdArray * > m_IdArrays
Definition: AutoRooters.h:49
ScriptInterface & m_ScriptInterface
Definition: AutoRooters.h:44
std::vector< jsval > m_Vals
Definition: AutoRooters.h:48
AutoGCRooter * m_Previous
Definition: AutoRooters.h:45
std::vector< JSObject * > m_Objects
Definition: AutoRooters.h:47
NONCOPYABLE(AutoGCRooter)
void Push(JSObject *obj)
Definition: AutoRooters.h:38
Abstraction around a SpiderMonkey JSContext.
void Trace(JSTracer *trc)
Definition: AutoRooters.cpp:36
Helper for rooting large groups of script values.
Definition: AutoRooters.h:31