Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScriptVal.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_SCRIPTVAL
19 #define INCLUDED_SCRIPTVAL
20 
21 #include "ScriptTypes.h"
22 #include <boost/shared_ptr.hpp>
23 
24 /**
25  * A trivial wrapper around a jsval. Used to avoid template overload ambiguities
26  * with jsval (which is just an integer), for any code that uses
27  * ScriptInterface::ToJSVal or ScriptInterface::FromJSVal
28  */
30 {
31 public:
32  CScriptVal() : m_Val(JSVAL_VOID) { }
33  CScriptVal(jsval val) : m_Val(val) { }
34 
35  /**
36  * Returns the current value.
37  */
38  const jsval& get() const { return m_Val; }
39 
40  /**
41  * Returns whether the value is JSVAL_VOID.
42  */
43  bool undefined() const { return JSVAL_IS_VOID(m_Val) ? true : false; }
44 
45 private:
46  jsval m_Val;
47 };
48 
50 {
51 public:
53  CScriptValRooted(JSContext* cx, jsval val);
54  CScriptValRooted(JSContext* cx, CScriptVal val);
55 
56  /**
57  * Returns the current value (or JSVAL_VOID if uninitialised).
58  */
59  jsval get() const;
60 
61  /**
62  * Returns reference to the current value.
63  * Fails if the value is not yet initialised.
64  */
65  jsval& getRef() const;
66 
67  /**
68  * Returns whether the value is uninitialised or is JSVAL_VOID.
69  */
70  bool undefined() const;
71 
72  /**
73  * Returns whether the value is uninitialised.
74  */
75  bool uninitialised() const;
76 
77 private:
78  boost::shared_ptr<jsval> m_Val;
79 };
80 
81 /**
82  * RAII wrapper for JSIdArray*
83  */
85 {
87 public:
88  AutoJSIdArray(JSContext* cx, JSIdArray* ida);
90 
91  JSIdArray* get() const;
92  size_t length() const;
93  jsid operator[](size_t i) const;
94 
95 private:
96  JSContext* m_Context;
97  JSIdArray* m_IdArray;
98 };
99 
100 #endif // INCLUDED_SCRIPTVAL
bool uninitialised() const
Returns whether the value is uninitialised.
Definition: ScriptVal.cpp:63
jsval m_Val
Definition: ScriptVal.h:46
boost::shared_ptr< jsval > m_Val
Definition: ScriptVal.h:78
JSContext * m_Context
Definition: ScriptVal.h:96
A trivial wrapper around a jsval.
Definition: ScriptVal.h:29
AutoJSIdArray(JSContext *cx, JSIdArray *ida)
Definition: ScriptVal.cpp:68
CScriptVal(jsval val)
Definition: ScriptVal.h:33
JSIdArray * m_IdArray
Definition: ScriptVal.h:97
bool undefined() const
Returns whether the value is uninitialised or is JSVAL_VOID.
Definition: ScriptVal.cpp:58
NONCOPYABLE(AutoJSIdArray)
size_t length() const
Definition: ScriptVal.cpp:84
CScriptVal()
Definition: ScriptVal.h:32
jsid operator[](size_t i) const
Definition: ScriptVal.cpp:91
jsval & getRef() const
Returns reference to the current value.
Definition: ScriptVal.cpp:52
bool undefined() const
Returns whether the value is JSVAL_VOID.
Definition: ScriptVal.h:43
RAII wrapper for JSIdArray*.
Definition: ScriptVal.h:84