Add variables() method and supporting code.

This commit is contained in:
King_DuckZ 2020-04-28 14:24:50 +02:00
parent 0d0ea0a0cc
commit 67a5fab3c4
2 changed files with 103 additions and 0 deletions

View file

@ -68,6 +68,17 @@ namespace wren {
retval.finalize = funcs.finalize; retval.finalize = funcs.finalize;
return retval; return retval;
} }
template <typename T> T slot_specialized (WrenVM* vm, int slot_num);
template <> bool slot_specialized (WrenVM* vm, int slot_num) {
return wrenGetSlotBool(vm, slot_num);
}
template <> std::string slot_specialized (WrenVM* vm, int slot_num) {
return wrenGetSlotString(vm, slot_num);
}
template <> double slot_specialized (WrenVM* vm, int slot_num) {
return wrenGetSlotDouble(vm, slot_num);
}
} //unnamed namespace } //unnamed namespace
struct VM::LocalData { struct VM::LocalData {
@ -152,4 +163,27 @@ namespace wren {
void VM::interpret (const std::string& module_name, const std::string& script) { void VM::interpret (const std::string& module_name, const std::string& script) {
return interpret(module_name.c_str(), script.c_str()); return interpret(module_name.c_str(), script.c_str());
} }
template <typename T>
T VM::slot (int slot_num) {
return slot_specialized<T>(m_local->wvm, slot_num);
}
template <>
int VM::slot (int slot_num) {
return static_cast<int>(slot<double>(slot_num));
}
void VM::ensure_slots (int num_slots) {
wrenEnsureSlots(m_local->wvm, num_slots);
}
void VM::variable(const char* module, const char* name, int slot) {
wrenGetVariable(m_local->wvm, module, name, slot);
}
template bool VM::slot<bool>(int);
template std::string VM::slot<std::string>(int);
template double VM::slot<double>(int);
template int VM::slot<int>(int);
} //namespace wren } //namespace wren

View file

@ -4,6 +4,11 @@
#include "error_type.hpp" #include "error_type.hpp"
#include <string> #include <string>
#include <memory> #include <memory>
#include <tuple>
#include <utility>
#if __cpp_concepts >= 201907
# include <concepts>
#endif
namespace wren { namespace wren {
class Configuration; class Configuration;
@ -12,6 +17,7 @@ namespace wren {
typedef void(*foreign_method_t)(VM*); typedef void(*foreign_method_t)(VM*);
typedef void(*finalizer_t)(void*); typedef void(*finalizer_t)(void*);
typedef std::tuple<const char*, const char*> ModuleAndName;
struct foreign_class_t { struct foreign_class_t {
foreign_method_t allocate; foreign_method_t allocate;
@ -32,6 +38,10 @@ namespace wren {
VM* owner {nullptr}; VM* owner {nullptr};
DynafuncMaker* dynafunc {nullptr}; DynafuncMaker* dynafunc {nullptr};
}; };
#if __cpp_concepts >= 201907
template <typename T> concept ConstCharTuple = std::same_as<std::remove_cv_t<T>, ModuleAndName>;
#endif
} //namespace detail } //namespace detail
class VM { class VM {
@ -45,6 +55,18 @@ namespace wren {
void interpret (const char* module_name, const char* script); void interpret (const char* module_name, const char* script);
void interpret (const std::string& module_name, const std::string& script); void interpret (const std::string& module_name, const std::string& script);
template <typename T> T slot (int slot_num);
#if __cpp_concepts >= 201907
template <typename... Outs, detail::ConstCharTuple... Params>
#else
template <typename... Outs, typename... Params>
#endif
std::tuple<Outs...> variables(Params&&... modules_names);
//template <typename... Args> std::tuple<Args...> variables(const Handle& handle);
void ensure_slots(int num_slots);
void variable(const char* module, const char* name, int slot);
private: private:
struct LocalData; struct LocalData;
@ -54,6 +76,13 @@ namespace wren {
template <typename T> template <typename T>
detail::Callbacks to_callbacks (T& conf); detail::Callbacks to_callbacks (T& conf);
#if __cpp_concepts >= 201907
template <typename... Outs, int... Indices, detail::ConstCharTuple... Params>
#else
template <typename... Outs, int... Indices, typename... Params>
#endif
std::tuple<Outs...> get_variables_impl (std::integer_sequence<int, Indices...>, Params&&... modules_names);
std::unique_ptr<LocalData> m_local; std::unique_ptr<LocalData> m_local;
}; };
@ -122,4 +151,44 @@ namespace wren {
VM(static_cast<Configuration*>(conf), to_callbacks(*conf)) VM(static_cast<Configuration*>(conf), to_callbacks(*conf))
{ {
} }
#if __cpp_concepts >= 201907
template <typename... Outs, detail::ConstCharTuple... Params>
#else
template <typename... Outs, typename... Params>
#endif
std::tuple<Outs...> VM::variables(Params&&... modules_names) {
return get_variables_impl<Outs...>(std::make_integer_sequence<int, sizeof...(Outs)>(), std::forward<Params>(modules_names)...);
}
#if __cpp_concepts >= 201907
template <typename... Outs, int... Indices, detail::ConstCharTuple... Params>
#else
template <typename... Outs, int... Indices, typename... Params>
#endif
inline std::tuple<Outs...> VM::get_variables_impl (
std::integer_sequence<int, Indices...>,
Params&&... modules_names
) {
if constexpr (sizeof...(Outs) == 0) {
return {};
}
else {
using std::get;
using std::tuple;
static_assert(sizeof...(Params) == sizeof...(Outs), "Expected a module/name pair per requested output");
static_assert(sizeof...(Outs) == sizeof...(Indices), "Mismatching index count");
ensure_slots(sizeof...(Outs));
(this->variable(get<0>(modules_names), get<1>(modules_names), Indices), ...);
return tuple<Outs...>(this->slot<Outs>(Indices)...);
}
}
//template <typename... Args>
//inline std::tuple<Args...> VM::variables(const Handle& handle) {
// if constexpr (sizeof...(Args) == 0)
// return {};
//}
} //namespace wren } //namespace wren