xoreos  0.0.5
functionman.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/util.h"
26 #include "src/common/error.h"
27 #include "src/common/debug.h"
28 
30 
32 
33 namespace Aurora {
34 
35 namespace NWScript {
36 
38  empty(true), ctx(name) {
39 }
40 
41 
43 }
44 
46 }
47 
49  _functionMap.clear();
50  _functionArray.clear();
51 }
52 
54  const Function &func, const Signature &signature) {
55  Parameters defaults;
56 
57  registerFunction(name, id, func, signature, defaults);
58 }
59 
61  const Function &func, const Signature &signature,
62  const Parameters &defaults) {
63 
64  std::pair<FunctionMap::iterator, bool> result;
65 
66  result = _functionMap.insert(std::make_pair(name, FunctionEntry(name)));
67  if (!result.second)
68  throw Common::Exception("Failed to register NWScript function \"%s\"", name.c_str());
69 
70  FunctionEntry &f = result.first->second;
71 
72  f.func = func;
73  f.ctx.setSignature(signature);
74  f.ctx.setDefaults(defaults);
75  f.empty = false;
76 
77  if (_functionArray.size() <= id)
78  _functionArray.resize(id + 1);
79 
80  _functionArray[id] = f;
81 }
82 
84  return find(function).ctx;
85 }
86 
87 void FunctionManager::call(const Common::UString &function, FunctionContext &ctx) const {
88  debugCN(Common::kDebugEngineScripts, 5, "%s %s(%s)", formatType(ctx.getReturn().getType()).c_str(),
89  ctx.getName().c_str(), formatParams(ctx).c_str());
90 
91  find(function).func(ctx);
92 
93  const Common::UString r = formatReturn(ctx);
94  debugC(Common::kDebugEngineScripts, 5, "%s%s", r.empty() ? "" : " => ", r.c_str());
95 
96  if (DebugMan.getVerbosityLevel(Common::kDebugEngineScripts) < 5)
97  debugC(Common::kDebugEngineScripts, 2, "%s %s(%s)%s%s", formatType(ctx.getReturn().getType()).c_str(),
98  ctx.getName().c_str(), formatParams(ctx).c_str(), r.empty() ? "" : " => ", r.c_str());
99 }
100 
102  return find(function).ctx;
103 }
104 
105 void FunctionManager::call(uint32 function, FunctionContext &ctx) const {
106  debugCN(Common::kDebugEngineScripts, 5, "%s %s(%s)", formatType(ctx.getReturn().getType()).c_str(),
107  ctx.getName().c_str(), formatParams(ctx).c_str());
108 
109  find(function).func(ctx);
110 
111  const Common::UString r = formatReturn(ctx);
112  debugC(Common::kDebugEngineScripts, 5, "%s%s", r.empty() ? "" : " => ", r.c_str());
113 
114  if (DebugMan.getVerbosityLevel(Common::kDebugEngineScripts) < 5)
115  debugC(Common::kDebugEngineScripts, 2, "%s %s(%s)%s%s", formatType(ctx.getReturn().getType()).c_str(),
116  ctx.getName().c_str(), formatParams(ctx).c_str(), r.empty() ? "" : " => ", r.c_str());
117 }
118 
120  FunctionMap::const_iterator f = _functionMap.find(function);
121  if ((f == _functionMap.end()) || f->second.empty)
122  throw Common::Exception("No such NWScript function \"%s\"", function.c_str());
123 
124  return f->second;
125 }
126 
128  if ((function >= _functionArray.size()) || _functionArray[function].empty)
129  throw Common::Exception("No such NWScript function %d", function);
130 
131  return _functionArray[function];
132 }
133 
134 } // End of namespace NWScript
135 
136 } // End of namespace Aurora
std::vector< class Variable > Parameters
Definition: types.h:54
void debugC(Common::DebugChannel channel, uint32 level, const char *s,...)
Definition: debug.cpp:34
A class holding an UTF-8 string.
Definition: ustring.h:48
Utility functions for debug output.
Common::UString formatType(Type type)
Construct a string describing this variable type.
Definition: util.cpp:130
"EScripts", engine scripts.
Definition: debugman.h:59
The NWScript function manager.
void call(const Common::UString &function, FunctionContext &ctx) const
Definition: functionman.cpp:87
Basic exceptions to throw.
FunctionContext createContext(const Common::UString &function) const
Definition: functionman.cpp:83
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Common::UString formatReturn(const FunctionContext &ctx)
Construct a string describing the return value of this function.
Definition: util.cpp:121
Utility templates and functions.
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
FunctionEntry(const Common::UString &name="")
Definition: functionman.cpp:37
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
std::vector< Type > Signature
Definition: types.h:52
const Common::UString & getName() const
StackException Exception
Definition: error.h:59
void debugCN(Common::DebugChannel channel, uint32 level, const char *s,...)
Definition: debug.cpp:54
#define DebugMan
Shortcut for accessing the debug manager.
Definition: debugman.h:195
void registerFunction(const Common::UString &name, uint32 id, const Function &func, const Signature &signature)
Definition: functionman.cpp:53
const FunctionEntry & find(const Common::UString &function) const
uint32_t uint32
Definition: types.h:204
Common::UString formatParams(const FunctionContext &ctx)
Construct a string describing parameters of this function.
Definition: util.cpp:109
boost::function< void(class FunctionContext &ctx)> Function
Definition: types.h:56