Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Script.inl
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#ifndef DVD_SCRIPTING_SCRIPT_INL_
33#define DVD_SCRIPTING_SCRIPT_INL_
34
35#include <chaiscript/utility/utility.hpp>
36#include <chaiscript/chaiscript.hpp>
37
38namespace Divide {
39
40template<typename T>
41void Script::addGlobal(const T& var, const char* name, const bool asConst, const bool overwrite) {
42 if (overwrite) {
43 _script->set_global(chaiscript::var(var), name); // verwrites existing object
44 } else {
45 if (asConst) {
46 _script->add(chaiscript::const_var(var), name); // copied in and made const
47 } else {
48 _script->add(chaiscript::var(var), name); // copied in
49 }
50 }
51}
52
53template <typename T>
54void Script::registerType(const char* typeName) {
55 _script->add(chaiscript::user_type<T>(), typeName);
56}
57
58template <typename Func >
59void Script::registerFunction(const Func& function, const char* functionName) {
60 _script->add(chaiscript::fun(function), functionName);
61}
62
63template<typename T>
65 assert(!_scriptSource.empty());
66 try {
67 return _script->eval<T>(_scriptSource);
68 } catch (const chaiscript::exception::eval_error &e) {
69 caughtException(e.pretty_print().c_str(), true);
70 }
71
72 return {};
73}
74
75template<>
76inline void Script::eval() {
77 assert(!_scriptSource.empty());
78 [[maybe_unused]] chaiscript::Boxed_Value ret = {};
79 try {
80 ret = _script->eval(_scriptSource);
81 } catch (const chaiscript::exception::eval_error &e) {
82 caughtException(e.pretty_print().c_str(), true);
83 }
84}
85
86} //namespace Divide
87
88#endif //DVD_SCRIPTING_SCRIPT_INL_
void caughtException(const char *message, bool isEvalException) const
Definition: Script.cpp:214
std::string _scriptSource
Definition: Script.h:84
void registerType(const char *typeName)
Definition: Script.inl:54
void addGlobal(const T &var, const char *name, bool asConst, bool overwrite)
Definition: Script.inl:41
chaiscript::ChaiScript_uptr _script
Definition: Script.h:83
void registerFunction(const Func &function, const char *functionName)
Definition: Script.inl:59
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7