在我正在编写的一些代码中,我有一堆C++函数,我试图以一种通用的方式将它们绑定到lua。(然而,这个问题实际上与lua无关,它实际上是一个C++设计问题。)
我的想法是,我可能有一个带签名的C++函数
int my_function(lua_State * L, std::string server, std::string message);例如,我希望能够将其推送到lua,并将其公开给用户脚本。
但是lua只能直接接收signature int (lua_State *)的函数。因此,我有一些模板,它们采用类似上面的签名的函数指针,并生成签名int(lua_State *)函数,该函数尝试从lua堆栈中读取相应的参数,如果成功,则使用参数调用目标函数,如果不成功,则向用户发出lua错误信号。
这一部分正在发挥作用,需要做一些工作,这个问题不是关于如何做到这一点。(请不要告诉我有关luabind、luabridge或其他现有库的信息,因为我无法深入了解那些不适合我的项目的库。)
相反,我现在遇到的问题是,有时我希望输入参数具有稍微不同的语义。
例如,有时参数应该是可选的。为了处理这种情况,我专门为boost::optional设计了模板。因此,我可以在函数签名中使用boost::optional标记可选参数,包装器将知道如果该参数丢失,它就不是错误,它应该只传递boost::none。示例:
int my_function(lua_State * L, boost::optional<std::string>, std::string message);因此,boost::optional模板在这里被用作输入的“策略”,我基本上喜欢它的工作方式。
这里有一个我不太确定的问题:bool的处理。在lua中,有一个合适的boolean类型,然而,lua也有一个contextually boolean的概念,类似于C++中contextually convertible to bool的概念,在lua中,值false和nil是假的,其他值都是真的。
通常,当您有一个接受bool参数的c++函数时,用户会希望他们可以向它传递任何值,并且您的接口将遵守truthiness,即使严格地说它不是布尔值。然而,在其他情况下,您可能真的希望将其严格解释为bool,并且如果它们不传递true或false,则将其视为用户错误。
我想要做的是在函数声明中标记“严格性”策略,这样看起来就像
int my_function(lua_State * L, strict<bool> b, std::string message);其中,strict是像这样的模板
template <typename T>
struct strict {
T value;
};这个模板只有在我的包装器机器中才有意义。
令人恼火的是,你必须在任何地方输入b.value。
我是这样想的:
template <typename T>
struct strict {
T value;
operator T & () & { return this->value; }
operator const T & () const & { return this->value; }
operator T && () && { return std::move(this->value); }
};以允许从strict<T>到对值的引用的一组引用限定的隐式转换。
这有多不安全?虽然我一直坚持“隐式转换是邪恶的”这句话,但我并不认为这是一个重大的安全漏洞。我在测试代码中使用了一下它,它似乎不会产生歧义或问题,但也许有一个聪明的方法可以让它做一些我没有想到的非常糟糕的事情。
如果这不是一个好主意,有没有比到处输入b.value更好的策略,或者用某种不同的方法来组合参数策略,而不会干扰类型?
发布于 2016-06-24 06:28:37
像这样的东西应该就行了。
完成这项工作的是visit的重载。注意来自optional版本的递归调用。
#include <iostream>
#include <string>
#include <utility>
#include <iomanip>
#include <boost/optional.hpp>
// some boilerplate
template <typename T, template <typename, typename...> class Tmpl> // #1 see note
struct is_derived_from_template
{
typedef char yes[1];
typedef char no[2];
static no & test(...);
template <typename ...U>
static yes & test(Tmpl<U...> const &);
static bool constexpr value = sizeof(test(std::declval<T>())) == sizeof(yes);
};
template<typename T, template <typename, typename...> class Tmpl>
static constexpr bool is_derived_from_template_v = is_derived_from_template<T, Tmpl>::value;
// i dont know much about a lua_state but I guess it's a bit like this...
struct lua_state {
void set_string(std::size_t index, const std::string& s) {
std::cout << "index " << index << " setting string " << std::quoted(s) << std::endl;
}
void set_missing(std::size_t index) {
std::cout << "index " << index << " setting missing" << std::endl;
}
void set_int(std::size_t index, int i) {
std::cout << "index " << index << " setting int " << i << std::endl;
}
};
// policies
template<class T, std::enable_if_t<std::is_same<std::decay_t<T>, std::string>::value>* = nullptr>
void visit(std::size_t index, lua_state* pstate, T&& value)
{
pstate->set_string(index, std::forward<T>(value));
}
template<class T, std::enable_if_t<std::is_same<std::decay_t<T>, int>::value>* = nullptr>
void visit(std::size_t index, lua_state* pstate, T&& value)
{
pstate->set_int(index, std::forward<T>(value));
}
// special policy for optional
template<class T,
std::enable_if_t<is_derived_from_template_v<std::decay_t<T>, boost::optional>>* = nullptr>
void visit(std::size_t index, lua_state* pstate, T&& value)
{
if (value)
{
visit(index, pstate, std::forward<T>(value).value());
}
else {
pstate->set_missing(index);
}
}
// helper function
template<std::size_t...Is, class Tuple>
void set_vars_impl(lua_state* pstate, std::index_sequence<Is...>, Tuple&& tuple)
{
using expand = int [];
void(expand{ 0,
((visit(Is, pstate, std::get<Is>(std::forward<Tuple>(tuple)))),0)...
});
}
template<class...Ts>
void set_vars(lua_state* pstate, Ts&&...ts)
{
set_vars_impl(pstate,
std::make_index_sequence<sizeof...(Ts)>(),
std::make_tuple(std::forward<Ts>(ts)...));
}
int main(int argc, const char * argv[]) {
lua_state ls;
boost::optional<std::string> a { };
boost::optional<std::string> b { std::string { "hello" }};
std::string c = "world";
int d = 0;
boost::optional<int> e;
boost::optional<int> f { 1 };
set_vars(std::addressof(ls), a, b, c, d, e, f);
return 0;
}预期结果:
index 0 setting missing
index 1 setting string "hello"
index 2 setting string "world"
index 3 setting int 0
index 4 setting missing
index 5 setting int 1https://stackoverflow.com/questions/38002402
复制相似问题