var callbacks can now be lambdas

master
Daniel Kolesa 2016-08-01 01:10:21 +01:00
parent 14678e0861
commit a12f01a8a2
2 changed files with 22 additions and 21 deletions

View File

@ -119,23 +119,23 @@ static inline bool cs_check_num(ostd::ConstCharRange s) {
/* ID_VAR */
Ident::Ident(int t, ostd::ConstCharRange n, int m, int x, int *s,
IdentFunc f, int flagsv)
VarCb f, int flagsv)
: type(t), flags(flagsv | (m > x ? IDF_READONLY : 0)), name(n),
minval(m), maxval(x), cb_var(f) {
minval(m), maxval(x), cb_var(ostd::move(f)), cb_cftv(nullptr) {
storage.ip = s;
}
/* ID_FVAR */
Ident::Ident(int t, ostd::ConstCharRange n, float m, float x, float *s,
IdentFunc f, int flagsv)
VarCb f, int flagsv)
: type(t), flags(flagsv | (m > x ? IDF_READONLY : 0)), name(n),
minvalf(m), maxvalf(x), cb_var(f) {
minvalf(m), maxvalf(x), cb_var(ostd::move(f)), cb_cftv(nullptr) {
storage.fp = s;
}
/* ID_SVAR */
Ident::Ident(int t, ostd::ConstCharRange n, char **s, IdentFunc f, int flagsv)
: type(t), flags(flagsv), name(n), cb_var(f) {
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) {
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, IdentFunc f, int flagsv)
ostd::Uint32 argmask, int numargs, CommandFuncTv f, int flagsv)
: type(t), numargs(numargs), flags(flagsv), name(n),
args(!args.empty() ? cs_dup_ostr(args) : nullptr),
argmask(argmask), cb_var(f) {
argmask(argmask), cb_var(), cb_cftv(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,
IdentFunc func, int type, int flags) {
CommandFuncTv func, int type, int flags) {
ostd::Uint32 argmask = 0;
int nargs = 0;
bool limit = true;
@ -3361,7 +3361,7 @@ noid:
result.force_null();
switch (id->type) {
default:
if (!id->cb_var) FORCERESULT;
if (!id->cb_cftv) FORCERESULT;
/* fallthrough */
case ID_COMMAND:
idarg.cleanup();
@ -3430,7 +3430,7 @@ void CsState::run_ret(Ident *id, TvalRange args, TaggedValue &ret) {
if (rundepth > MaxRunDepth) cs_debug_code(*this, "exceeded recursion limit");
else if (id) switch (id->type) {
default:
if (!id->cb_var) break;
if (!id->cb_cftv) break;
/* fallthrough */
case ID_COMMAND:
if (nargs < id->numargs) {

View File

@ -159,7 +159,8 @@ union IdentValuePtr {
struct CsState;
using IdentFunc = void (*)(CsState &, Ident *);
using VarCb = ostd::Function<void(CsState &, Ident &)>;
using CommandFunc = void (*)(CsState &);
using CommandFunc1 = void (*)(CsState &, void *);
using CommandFunc2 = void (*)(CsState &, void *, void *);
@ -207,8 +208,8 @@ struct OSTD_EXPORT Ident {
ostd::Uint32 argmask;
};
};
VarCb cb_var;
union {
IdentFunc cb_var;
CommandFunc cb_cf0;
CommandFunc1 cb_cf1;
CommandFunc2 cb_cf2;
@ -229,14 +230,14 @@ struct OSTD_EXPORT Ident {
/* ID_VAR */
Ident(int t, ostd::ConstCharRange n, int m, int x, int *s,
IdentFunc f = nullptr, int flags = 0);
VarCb f = VarCb(), int flags = 0);
/* ID_FVAR */
Ident(int t, ostd::ConstCharRange n, float m, float x, float *s,
IdentFunc f = nullptr, int flags = 0);
VarCb f = VarCb(), int flags = 0);
/* ID_SVAR */
Ident(int t, ostd::ConstCharRange n, char **s, IdentFunc f = nullptr,
Ident(int t, ostd::ConstCharRange n, char **s, VarCb f = VarCb(),
int flags = 0);
/* ID_ALIAS */
@ -248,11 +249,11 @@ struct OSTD_EXPORT Ident {
/* ID_COMMAND */
Ident(int t, ostd::ConstCharRange n, ostd::ConstCharRange args,
ostd::Uint32 argmask, int numargs, IdentFunc f = nullptr,
ostd::Uint32 argmask, int numargs, CommandFuncTv f = nullptr,
int flags = 0);
void changed(CsState &cs) {
if (cb_var) cb_var(cs, this);
if (cb_var) cb_var(cs, *this);
}
void set_value(TaggedValue const &v) {
@ -354,13 +355,13 @@ struct OSTD_EXPORT CsState {
void touch_var(ostd::ConstCharRange name);
bool add_command(ostd::ConstCharRange name, ostd::ConstCharRange args,
IdentFunc func, int type = ID_COMMAND, int flags = 0);
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,
IdentFunc(ostd::FunctionMakeDefaultConstructible<F>(func)),
CommandFuncTv(ostd::FunctionMakeDefaultConstructible<F>(func)),
type, flags | IDF_NOEXPAND);
}
@ -368,7 +369,7 @@ struct OSTD_EXPORT CsState {
bool add_commandn(ostd::ConstCharRange name, ostd::ConstCharRange args,
F func, int type = ID_COMMAND, int flags = 0) {
return add_command(name, args,
IdentFunc(ostd::FunctionMakeDefaultConstructible<F>(func)),
CommandFuncTv(ostd::FunctionMakeDefaultConstructible<F>(func)),
type, flags);
}