1 /* Copyright (C) 2015 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_SCRIPTVAL
19 #define INCLUDED_SCRIPTVAL
21 #include "ScriptTypes.h"
24 * A default constructible wrapper around JS::PersistentRootedValue
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.
33 class DefPersistentRooted
40 DefPersistentRooted(JSRuntime
* rt
)
42 m_Val
.reset(new JS::PersistentRooted
<T
>(rt
));
45 DefPersistentRooted(JSRuntime
* rt
, JS::HandleValue val
)
47 m_Val
.reset(new JS::PersistentRooted
<T
>(rt
, val
));
50 DefPersistentRooted(JSContext
* cx
, JS::Handle
<T
> val
)
52 m_Val
.reset(new JS::PersistentRooted
<T
>(cx
, val
));
60 inline bool uninitialized()
62 return m_Val
== nullptr;
65 inline JS::PersistentRooted
<T
>& get() const
71 inline void set(JSRuntime
* rt
, T val
)
73 m_Val
.reset(new JS::PersistentRooted
<T
>(rt
, val
));
76 inline void set(JSContext
* cx
, T val
)
78 m_Val
.reset(new JS::PersistentRooted
<T
>(cx
, val
));
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
85 DefPersistentRooted
<T
>& operator=(DefPersistentRooted
<T
>&& other
)
87 m_Val
= std::move(other
.m_Val
);
91 DefPersistentRooted
<T
>(DefPersistentRooted
<T
>&& other
)
93 m_Val
= std::move(other
.m_Val
);
97 std::unique_ptr
<JS::PersistentRooted
<T
> > m_Val
;
100 #endif // INCLUDED_SCRIPTVAL