From 2f6bd216d1411bd6ec96eaebf815dc3d5ab5a2f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 15 Jul 2022 02:03:30 +0200 Subject: Reimplement Pl with variadic templates This is a bit safer and about as complex. --- c/defs.h | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'c/defs.h') diff --git a/c/defs.h b/c/defs.h index 06b1ef1..f57989c 100644 --- a/c/defs.h +++ b/c/defs.h @@ -66,9 +66,55 @@ struct DataListView : public ListView }; /* pl.cpp */ -int Pl(const char *, const char *, const char *, ...); -int Plp(term_t, const char *, ...); -int Plg(term_t, const char *, ...); +int Pl(const char *, const char *); + +inline int PlPut(term_t t, int x) { return PL_put_integer(t, x); } +inline int PlPut(term_t t, long x) { return PL_put_integer(t, x); } +inline int PlPut(term_t t, long long x) { return PL_put_integer(t, x); } +inline int PlPut(term_t t, atom_t x) { return PL_put_atom(t, x); } +inline int PlPut(term_t t, char *x) { return PL_put_atom(t, PL_new_atom(x)); } +inline int PlPut(term_t t, const char *x) { return PL_put_atom(t, PL_new_atom(x)); } +inline int PlPut(term_t t, int *x) { return -1; } +inline int PlPut(term_t t, long *x) { return -1; } +inline int PlPut(term_t t, long long *x) { return -1; } +inline int PlPut(term_t t, atom_t *x) { return -1; } +inline int PlPut(term_t t, char **x) { return -1; } + +inline int PlGet(term_t t, int x) { return -1; } +inline int PlGet(term_t t, long x) { return -1; } +inline int PlGet(term_t t, long long x) { return -1; } +inline int PlGet(term_t t, atom_t x) { return -1; } +inline int PlGet(term_t t, char *x) { return -1; } +inline int PlGet(term_t t, const char *x) { return -1; } +inline int PlGet(term_t t, int *x) { return PL_get_integer(t, x); } +inline int PlGet(term_t t, long *x) { return PL_get_long(t, x); } +inline int PlGet(term_t t, long long *x) { return PL_get_int64(t, x); } +inline int PlGet(term_t t, atom_t *x) { return PL_get_atom(t, x); } +inline int PlGet(term_t t, char **x) { return PL_get_atom_chars(t, x); } + +template +int PlPutv(term_t t, T arg) { return PlPut(t, arg); } +template +int PlPutv(term_t t, T arg, R... rest) { return PlPut(t, arg)? PlPutv(t+1, rest...): 0; } +template +int PlGetv(term_t t, T arg) { return PlGet(t, arg); } +template +int PlGetv(term_t t, T arg, R... rest) { return PlGet(t, arg)? PlGetv(t+1, rest...): 0; } +template +int Countv(int i, T arg) { return i+1; } +template +int Countv(int i, T arg, R... rest) { return Countv(i+1, rest...); } +template +int Pl(const char *szMod, const char *szPred, T... args) +{ + int iArity = Countv(0, args...); + term_t t = PL_new_term_refs(iArity); + if (!PlPutv(t, args...)) return 0; + if (!PL_call_predicate(NULL, PL_Q_NORMAL, PL_predicate(szPred, iArity, szMod), t)) + return 0; + if (!PlGetv(t, args...)) return 0; + return 1; +} /* defs.h */ #ifdef UNICODE -- cgit v1.2.3