Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // InputCallback.h
- #pragma once
- #include <functional>
- #include <sol.hpp>
- template <typename... Args>
- class InputCallback {
- using CppCallbackFunction_t = std::function<void(Args...)>;
- template <typename CallerType>
- using CppMemberCallbackFunction_t = void (CallerType::*)(Args...);
- enum class Type {
- Cpp,
- Lua
- };
- public:
- InputCallback();
- template <typename CallerType>
- void setCallback(CallerType* const caller, CppMemberCallbackFunction_t<CallerType> f);
- void setCallback(sol::object c, sol::protected_function f);
- template <typename... FArgs>
- void call(FArgs&&... args);
- private:
- Type type;
- sol::protected_function luaCallback;
- sol::object caller;
- CppCallbackFunction_t cppCallback;
- };
- #include "InputCallback.inl"
- // InputCallback.inl
- #pragma once
- template <typename... Args>
- InputCallback<Args...>::InputCallback() :
- type(Type::Cpp)
- {}
- template <typename... Args>
- template <typename CallerType>
- void InputCallback<Args...>::setCallback(CallerType* const caller, CppMemberCallbackFunction_t<CallerType> f)
- {
- type = Type::Cpp;
- cppCallback = [caller, f](Args... args) { (caller->*f)(std::forward<Args>(args)...); };
- }
- template <typename... Args>
- void InputCallback<Args...>::setCallback(sol::object c, sol::protected_function f)
- {
- type = Type::Lua;
- caller = c;
- luaCallback = f;
- }
- #include <System/EngineSystem.h>
- #include <System/Managers/LogManager.h>
- template <typename... Args>
- template <typename... FArgs>
- void InputCallback<Args...>::call(FArgs&&... args)
- {
- switch (type) {
- case Type::Cpp:
- cppCallback(std::forward<FArgs>(args)...);
- break;
- case Type::Lua:
- sol::protected_function_result r =
- luaCallback(caller, std::forward<FArgs>(args)...);
- if (!r.valid()) {
- sol::error e = r;
- auto& log = engine_system.get<LogManager>();
- log(LogManager::MessageType::Lua_Exception) << e.what();
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement