Pyrogenesis  trunk
ScriptVal.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 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_SCRIPTVAL
19 #define INCLUDED_SCRIPTVAL
20 
21 #include "ScriptTypes.h"
22 
23 /**
24  * A default constructible wrapper around JS::PersistentRootedValue
25  *
26  * It's a very common case that we need to store JS::Values on the heap as
27  * class members and only need them conditionally or want to initialize
28  * them after the constructor because we don't have the runtime available yet.
29  * Use it in these cases, but prefer to use JS::PersistentRootedValue directly
30  * if initializing it with a runtime/context in the constructor isn't a problem.
31  */
32  template <typename T>
34 {
35 public:
37  {
38  }
39 
40  DefPersistentRooted(JSRuntime* rt)
41  {
42  m_Val.reset(new JS::PersistentRooted<T>(rt));
43  }
44 
45  DefPersistentRooted(JSRuntime* rt, JS::HandleValue val)
46  {
47  m_Val.reset(new JS::PersistentRooted<T>(rt, val));
48  }
49 
50  DefPersistentRooted(JSContext* cx, JS::Handle<T> val)
51  {
52  m_Val.reset(new JS::PersistentRooted<T>(cx, val));
53  }
54 
55  void clear()
56  {
57  m_Val = nullptr;
58  }
59 
60  inline bool uninitialized()
61  {
62  return m_Val == nullptr;
63  }
64 
65  inline JS::PersistentRooted<T>& get() const
66  {
67  ENSURE(m_Val);
68  return *m_Val;
69  }
70 
71  inline void set(JSRuntime* rt, T val)
72  {
73  m_Val.reset(new JS::PersistentRooted<T>(rt, val));
74  }
75 
76  inline void set(JSContext* cx, T val)
77  {
78  m_Val.reset(new JS::PersistentRooted<T>(cx, val));
79  }
80 
81  // TODO: Move assignment operator and move constructor only have to be
82  // explicitly defined for Visual Studio. VS2013 is still behind on C++11 support
83  // What's missing is what they call "Rvalue references v3.0", see
84  // https://msdn.microsoft.com/en-us/library/hh567368.aspx#rvref
86  {
87  m_Val = std::move(other.m_Val);
88  return *this;
89  }
90 
92  {
93  m_Val = std::move(other.m_Val);
94  }
95 
96 private:
97  std::unique_ptr<JS::PersistentRooted<T> > m_Val;
98 };
99 
100 #endif // INCLUDED_SCRIPTVAL
DefPersistentRooted< T > & operator=(DefPersistentRooted< T > &&other)
Definition: ScriptVal.h:85
DefPersistentRooted(JSRuntime *rt)
Definition: ScriptVal.h:40
A default constructible wrapper around JS::PersistentRootedValue.
Definition: ScriptVal.h:33
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:287
#define T(string_literal)
Definition: secure_crt.cpp:76
DefPersistentRooted(JSRuntime *rt, JS::HandleValue val)
Definition: ScriptVal.h:45
void clear()
Definition: ScriptVal.h:55
DefPersistentRooted(JSContext *cx, JS::Handle< T > val)
Definition: ScriptVal.h:50
std::unique_ptr< JS::PersistentRooted< T > > m_Val
Definition: ScriptVal.h:97
bool uninitialized()
Definition: ScriptVal.h:60
DefPersistentRooted()
Definition: ScriptVal.h:36