Some work on the template code in LuaStack
This commit is contained in:
@@ -146,6 +146,7 @@
|
|||||||
#include "wrap-string.hpp"
|
#include "wrap-string.hpp"
|
||||||
#include "wrap-set.hpp"
|
#include "wrap-set.hpp"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -277,6 +278,34 @@ class LuaCoreStack : public eng::nevernew {
|
|||||||
protected:
|
protected:
|
||||||
lua_State *L_;
|
lua_State *L_;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
struct Counts {
|
||||||
|
int nret;
|
||||||
|
int narg;
|
||||||
|
int nvar;
|
||||||
|
constexpr Counts(int r, int a, int v) : nret(r), narg(a), nvar(v) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<int NRET, int NARG, int NVAR, class... SS>
|
||||||
|
constexpr static Counts count_slots(LuaRet &v, SS & ... stackslots)
|
||||||
|
{
|
||||||
|
return count_slots<NRET+1, NARG, NVAR>(stackslots...);
|
||||||
|
}
|
||||||
|
template<int NRET, int NARG, int NVAR, class... SS>
|
||||||
|
constexpr static Counts count_slots(LuaArg &v, SS & ... stackslots)
|
||||||
|
{
|
||||||
|
return count_slots<NRET, NARG+1, NVAR>(stackslots...);
|
||||||
|
}
|
||||||
|
template<int NRET, int NARG, int NVAR, class... SS>
|
||||||
|
constexpr static Counts count_slots(LuaVar &v, SS & ... stackslots)
|
||||||
|
{
|
||||||
|
return count_slots<NRET, NARG, NVAR+1>(stackslots...);
|
||||||
|
}
|
||||||
|
template<int NRET, int NARG, int NVAR>
|
||||||
|
constexpr static Counts count_slots() {
|
||||||
|
return Counts(NRET, NARG, NVAR);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Push any value on the stack, by type.
|
// Push any value on the stack, by type.
|
||||||
void push_any_value(LuaNewTableMarker s) const { lua_newtable(L_); }
|
void push_any_value(LuaNewTableMarker s) const { lua_newtable(L_); }
|
||||||
@@ -529,23 +558,23 @@ private:
|
|||||||
void assign_slots(int argp, int varp, int retp) {}
|
void assign_slots(int argp, int varp, int retp) {}
|
||||||
|
|
||||||
template<int NARG, int NVAR, int NRET, class... SS>
|
template<int NARG, int NVAR, int NRET, class... SS>
|
||||||
void count_slots(LuaArg &v, SS & ... stackslots)
|
void count_slotsx(LuaArg &v, SS & ... stackslots)
|
||||||
{
|
{
|
||||||
count_slots<NARG+1, NVAR, NRET>(stackslots...);
|
count_slotsx<NARG+1, NVAR, NRET>(stackslots...);
|
||||||
}
|
}
|
||||||
template<int NARG, int NVAR, int NRET, class... SS>
|
template<int NARG, int NVAR, int NRET, class... SS>
|
||||||
void count_slots(LuaVar &v, SS & ... stackslots)
|
void count_slotsx(LuaVar &v, SS & ... stackslots)
|
||||||
{
|
{
|
||||||
count_slots<NARG, NVAR+1, NRET>(stackslots...);
|
count_slotsx<NARG, NVAR+1, NRET>(stackslots...);
|
||||||
}
|
}
|
||||||
template<int NARG, int NVAR, int NRET, class... SS>
|
template<int NARG, int NVAR, int NRET, class... SS>
|
||||||
void count_slots(LuaRet &v, SS & ... stackslots)
|
void count_slotsx(LuaRet &v, SS & ... stackslots)
|
||||||
{
|
{
|
||||||
count_slots<NARG, NVAR, NRET+1>(stackslots...);
|
count_slotsx<NARG, NVAR, NRET+1>(stackslots...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int NARG, int NVAR, int NRET>
|
template<int NARG, int NVAR, int NRET>
|
||||||
void count_slots() {
|
void count_slotsx() {
|
||||||
narg_ = NARG;
|
narg_ = NARG;
|
||||||
nret_ = NRET;
|
nret_ = NRET;
|
||||||
nvar_ = NVAR;
|
nvar_ = NVAR;
|
||||||
@@ -565,7 +594,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
template<class... SS>
|
template<class... SS>
|
||||||
LuaOldStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
|
LuaOldStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
|
||||||
count_slots<0, 0, 0>(stackslots...);
|
count_slotsx<0, 0, 0>(stackslots...);
|
||||||
if (lua_gettop(L) < narg_) {
|
if (lua_gettop(L) < narg_) {
|
||||||
luaL_error(L, "not enough arguments to function");
|
luaL_error(L, "not enough arguments to function");
|
||||||
}
|
}
|
||||||
@@ -621,65 +650,42 @@ public:
|
|||||||
class LuaDefStack : public LuaCoreStack {
|
class LuaDefStack : public LuaCoreStack {
|
||||||
private:
|
private:
|
||||||
int nret_;
|
int nret_;
|
||||||
int narg_;
|
|
||||||
int nvar_;
|
|
||||||
|
|
||||||
template<class... SS>
|
template<int RETP, int ARGP, int VARP, class... SS>
|
||||||
void assign_slots(int retp, int argp, int varp, LuaRet &v, SS & ... stackslots) {
|
void assign_slots(LuaRet &v, SS & ... stackslots) {
|
||||||
v.index_ = retp;
|
v.index_ = RETP;
|
||||||
assign_slots(retp+1, argp, varp, stackslots...);
|
assign_slots<RETP+1, ARGP, VARP>(stackslots...);
|
||||||
}
|
}
|
||||||
template<class... SS>
|
template<int RETP, int ARGP, int VARP, class... SS>
|
||||||
void assign_slots(int retp, int argp, int varp, LuaArg &v, SS & ... stackslots) {
|
void assign_slots(LuaArg &v, SS & ... stackslots) {
|
||||||
v.index_ = argp;
|
v.index_ = ARGP;
|
||||||
assign_slots(retp, argp + 1, varp, stackslots...);
|
assign_slots<RETP, ARGP+1, VARP>(stackslots...);
|
||||||
}
|
}
|
||||||
template<class... SS>
|
template<int RETP, int ARGP, int VARP, class... SS>
|
||||||
void assign_slots(int retp, int argp, int varp, LuaVar &v, SS & ... stackslots) {
|
void assign_slots(LuaVar &v, SS & ... stackslots) {
|
||||||
v.index_ = varp;
|
v.index_ = VARP;
|
||||||
assign_slots(retp, argp, varp+1, stackslots...);
|
assign_slots<RETP, ARGP, VARP+1>(stackslots...);
|
||||||
}
|
|
||||||
void assign_slots(int retp, int argp, int varp) {}
|
|
||||||
|
|
||||||
template<int NRET, int NARG, int NVAR, class... SS>
|
|
||||||
void count_slots(LuaRet &v, SS & ... stackslots)
|
|
||||||
{
|
|
||||||
count_slots<NRET+1, NARG, NVAR>(stackslots...);
|
|
||||||
}
|
|
||||||
template<int NRET, int NARG, int NVAR, class... SS>
|
|
||||||
void count_slots(LuaArg &v, SS & ... stackslots)
|
|
||||||
{
|
|
||||||
count_slots<NRET, NARG+1, NVAR>(stackslots...);
|
|
||||||
}
|
|
||||||
template<int NRET, int NARG, int NVAR, class... SS>
|
|
||||||
void count_slots(LuaVar &v, SS & ... stackslots)
|
|
||||||
{
|
|
||||||
count_slots<NRET, NARG, NVAR+1>(stackslots...);
|
|
||||||
}
|
}
|
||||||
template<int NRET, int NARG, int NVAR>
|
template<int NRET, int NARG, int NVAR>
|
||||||
void count_slots() {
|
void assign_slots() {}
|
||||||
nret_ = NRET;
|
|
||||||
narg_ = NARG;
|
|
||||||
nvar_ = NVAR;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<class... SS>
|
template<class... SS>
|
||||||
LuaDefStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
|
inline LuaDefStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
|
||||||
count_slots<0, 0, 0>(stackslots...);
|
constexpr Counts counts = count_slots<0, 0, 0>(stackslots...);
|
||||||
if (lua_gettop(L_) != narg_) {
|
if (lua_gettop(L_) != counts.narg) {
|
||||||
luaL_error(L_, "function expects exactly %d arguments", narg_);
|
luaL_error(L_, "function expects exactly %d arguments", counts.narg);
|
||||||
}
|
}
|
||||||
int tot = narg_ + nvar_ + nret_;
|
lua_checkstack(L, counts.narg + counts.nvar + counts.nret + 20);
|
||||||
lua_checkstack(L, tot + 20);
|
for (int i = 0; i < counts.nret; i ++) {
|
||||||
for (int i = 0; i < nret_; i ++) {
|
|
||||||
lua_pushnil(L_);
|
lua_pushnil(L_);
|
||||||
lua_insert(L_, i + 1);
|
lua_insert(L_, i + 1);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < nvar_; i++) {
|
for (int i = 0; i < counts.nvar; i++) {
|
||||||
lua_pushnil(L_);
|
lua_pushnil(L_);
|
||||||
}
|
}
|
||||||
assign_slots(1, 1 + nret_, 1 + nret_ + narg_, stackslots...);
|
assign_slots<1, 1 + counts.nret, 1 + counts.nret + counts.narg>(stackslots...);
|
||||||
|
nret_ = counts.nret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int result() {
|
int result() {
|
||||||
@@ -713,7 +719,6 @@ public:
|
|||||||
class LuaExtStack : public LuaCoreStack {
|
class LuaExtStack : public LuaCoreStack {
|
||||||
private:
|
private:
|
||||||
int oldtop_;
|
int oldtop_;
|
||||||
int nvar_;
|
|
||||||
|
|
||||||
template<class... SS>
|
template<class... SS>
|
||||||
void assign_slots(int varp, LuaVar &v, SS & ... stackslots) {
|
void assign_slots(int varp, LuaVar &v, SS & ... stackslots) {
|
||||||
@@ -722,38 +727,22 @@ private:
|
|||||||
}
|
}
|
||||||
void assign_slots(int varp) {}
|
void assign_slots(int varp) {}
|
||||||
|
|
||||||
template<int NVAR, class... SS>
|
|
||||||
void count_slots(LuaVar &v, SS & ... stackslots)
|
|
||||||
{
|
|
||||||
count_slots<NVAR+1>(stackslots...);
|
|
||||||
}
|
|
||||||
template<int NVAR>
|
|
||||||
void count_slots() {
|
|
||||||
nvar_ = NVAR;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<class... SS>
|
template<class... SS>
|
||||||
LuaExtStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
|
LuaExtStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
|
||||||
count_slots<0>(stackslots...);
|
constexpr Counts counts = count_slots<0, 0, 0>(stackslots...);
|
||||||
lua_checkstack(L_, nvar_ + 20);
|
static_assert(counts.narg == 0, "LuaExtStack doesn't allow LuaArg parameters");
|
||||||
|
static_assert(counts.nret == 0, "LuaExtStack doesn't allow LuaRet parameters");
|
||||||
|
lua_checkstack(L_, counts.nvar + 20);
|
||||||
oldtop_ = lua_gettop(L_);
|
oldtop_ = lua_gettop(L_);
|
||||||
for (int i = 0; i < nvar_; i++) {
|
for (int i = 0; i < counts.nvar; i++) {
|
||||||
lua_pushnil(L_);
|
lua_pushnil(L_);
|
||||||
}
|
}
|
||||||
assign_slots(oldtop_ + 1, stackslots...);
|
assign_slots(oldtop_ + 1, stackslots...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class... SS>
|
template<class... SS>
|
||||||
LuaExtStack(const LuaCoreStack &LS0, SS & ... stackslots) : LuaCoreStack(LS0.state()) {
|
LuaExtStack(const LuaCoreStack &LS0, SS & ... stackslots) : LuaCoreStack(LS0.state(), stackslots...) {}
|
||||||
count_slots<0>(stackslots...);
|
|
||||||
lua_checkstack(L_, nvar_ + 20);
|
|
||||||
oldtop_ = lua_gettop(L_);
|
|
||||||
for (int i = 0; i < nvar_; i++) {
|
|
||||||
lua_pushnil(L_);
|
|
||||||
}
|
|
||||||
assign_slots(oldtop_ + 1, stackslots...);
|
|
||||||
}
|
|
||||||
|
|
||||||
~LuaExtStack() {
|
~LuaExtStack() {
|
||||||
if (!lua_isthrowing(L_)) {
|
if (!lua_isthrowing(L_)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user