allow lambda captures on commands

master
Daniel Kolesa 2016-08-01 19:17:13 +01:00
parent c6db0900ac
commit d979a51c88
2 changed files with 11 additions and 20 deletions

View File

@ -121,7 +121,7 @@ static inline bool cs_check_num(ostd::ConstCharRange s) {
Ident::Ident(int t, ostd::ConstCharRange n, int m, int x, int *s,
VarCb f, int flagsv)
: type(t), flags(flagsv | (m > x ? IDF_READONLY : 0)), name(n),
minval(m), maxval(x), cb_var(ostd::move(f)), cb_cftv(nullptr) {
minval(m), maxval(x), cb_var(ostd::move(f)) {
storage.ip = s;
}
@ -129,13 +129,13 @@ Ident::Ident(int t, ostd::ConstCharRange n, int m, int x, int *s,
Ident::Ident(int t, ostd::ConstCharRange n, float m, float x, float *s,
VarCb f, int flagsv)
: type(t), flags(flagsv | (m > x ? IDF_READONLY : 0)), name(n),
minvalf(m), maxvalf(x), cb_var(ostd::move(f)), cb_cftv(nullptr) {
minvalf(m), maxvalf(x), cb_var(ostd::move(f)) {
storage.fp = s;
}
/* ID_SVAR */
Ident::Ident(int t, ostd::ConstCharRange n, char **s, VarCb f, int flagsv)
: type(t), flags(flagsv), name(n), cb_var(ostd::move(f)), cb_cftv(nullptr) {
: type(t), flags(flagsv), name(n), cb_var(ostd::move(f)) {
storage.sp = s;
}
@ -167,10 +167,10 @@ Ident::Ident(int t, ostd::ConstCharRange n, TaggedValue const &v, int flagsv)
/* ID_COMMAND */
Ident::Ident(int t, ostd::ConstCharRange n, ostd::ConstCharRange args,
ostd::Uint32 argmask, int numargs, CommandFuncTv f, int flagsv)
ostd::Uint32 argmask, int numargs, CmdFunc f, int flagsv)
: type(t), numargs(numargs), flags(flagsv), name(n),
args(!args.empty() ? cs_dup_ostr(args) : nullptr),
argmask(argmask), cb_var(), cb_cftv(f) {
argmask(argmask), cb_cftv(ostd::move(f)) {
}
struct NullValue: TaggedValue {
@ -1004,7 +1004,7 @@ void CsState::set_var_str_checked(Ident *id, ostd::ConstCharRange v) {
}
bool CsState::add_command(ostd::ConstCharRange name, ostd::ConstCharRange args,
CommandFuncTv func, int type, int flags) {
CmdFunc func, int type, int flags) {
ostd::Uint32 argmask = 0;
int nargs = 0;
bool limit = true;
@ -1054,7 +1054,7 @@ bool CsState::add_command(ostd::ConstCharRange name, ostd::ConstCharRange args,
name, nargs);
return false;
}
add_ident(type, name, args, argmask, nargs, func, flags);
add_ident(type, name, args, argmask, nargs, ostd::move(func), flags);
return true;
}

View File

@ -159,8 +159,7 @@ union IdentValuePtr {
struct CsState;
using VarCb = ostd::Function<void(CsState &, Ident &)>;
using CommandFuncTv = void (*)(CsState &, TvalRange);
using CmdFunc = ostd::Function<void(CsState &, TvalRange)>;
struct OSTD_EXPORT Ident {
ostd::byte type; /* ID_something */
@ -195,7 +194,7 @@ struct OSTD_EXPORT Ident {
};
};
VarCb cb_var;
CommandFuncTv cb_cftv;
CmdFunc cb_cftv;
Ident(): type(ID_UNKNOWN) {}
@ -220,7 +219,7 @@ struct OSTD_EXPORT Ident {
/* ID_COMMAND */
Ident(int t, ostd::ConstCharRange n, ostd::ConstCharRange args,
ostd::Uint32 argmask, int numargs, CommandFuncTv f = nullptr,
ostd::Uint32 argmask, int numargs, CmdFunc f = CmdFunc(),
int flags = 0);
void changed(CsState &cs) {
@ -326,15 +325,7 @@ struct OSTD_EXPORT CsState {
void touch_var(ostd::ConstCharRange name);
bool add_command(ostd::ConstCharRange name, ostd::ConstCharRange args,
CommandFuncTv func, int type = ID_COMMAND, int flags = 0);
template<typename F>
bool add_command(ostd::ConstCharRange name, ostd::ConstCharRange args,
F func, int type = ID_COMMAND, int flags = 0) {
return add_command(name, args,
CommandFuncTv(ostd::FunctionMakeDefaultConstructible<F>(func)),
type, flags);
}
CmdFunc func, int type = ID_COMMAND, int flags = 0);
ostd::String run_str(ostd::Uint32 const *code);
ostd::String run_str(ostd::ConstCharRange code);