Class Context
Synopsis
#include <Source/Falcor/Utils/Scripting/Scripting.h>
class Context
Description
Represents a context for executing scripts. Wraps the globals dictionary that is passed to the script on execution. The context can be used to pass/retrieve variables to/from the executing script.
Structures
ObjectDesc |
Methods
Context overload | ||
containsObject | ||
getObject | ||
getObjects | ||
setObject |
Source
Lines 47-105 in Source/Falcor/Utils/Scripting/Scripting.h.
class Context
{
public:
Context(pybind11::dict globals) : mGlobals(globals) {}
Context()
{
// Copy __builtins__ to our empty globals dictionary.
mGlobals["__builtins__"] = pybind11::globals()["__builtins__"];
}
template<typename T>
struct ObjectDesc
{
ObjectDesc(const std::string& name_, const T& obj_) : name(name_), obj(obj_) {}
operator const T&() const { return obj; }
std::string name;
T obj;
};
template<typename T>
std::vector<ObjectDesc<T>> getObjects()
{
std::vector<ObjectDesc<T>> v;
for (const auto& l : mGlobals)
{
try
{
if(!l.second.is_none())
{
v.push_back(ObjectDesc<T>(l.first.cast<std::string>(), l.second.cast<T>()));
}
}
catch (const std::exception&) {}
}
return v;
}
template<typename T>
void setObject(const std::string& name, T obj)
{
mGlobals[name.c_str()] = obj;
}
template<typename T>
T getObject(const std::string& name) const
{
return mGlobals[name.c_str()].cast<T>();
}
bool containsObject(const std::string& name) const
{
return mGlobals.contains(name.c_str());
}
private:
friend class Scripting;
pybind11::dict mGlobals;
};