aboutsummaryrefslogtreecommitdiff
path: root/c/defs.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-15 02:03:30 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-15 02:03:30 +0200
commit2f6bd216d1411bd6ec96eaebf815dc3d5ab5a2f4 (patch)
treef4ab4b455502c558a3c855f0ea7e164b1e246e04 /c/defs.h
parent095954fe8c0d1a64d8696d0ad0cd0ed395dd6c54 (diff)
downloadEpisodeBrowser-2f6bd216d1411bd6ec96eaebf815dc3d5ab5a2f4.tar.gz
Reimplement Pl with variadic templates
This is a bit safer and about as complex.
Diffstat (limited to 'c/defs.h')
-rw-r--r--c/defs.h52
1 files changed, 49 insertions, 3 deletions
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 <typename T>
+int PlPutv(term_t t, T arg) { return PlPut(t, arg); }
+template <typename T, typename ...R>
+int PlPutv(term_t t, T arg, R... rest) { return PlPut(t, arg)? PlPutv(t+1, rest...): 0; }
+template <typename T>
+int PlGetv(term_t t, T arg) { return PlGet(t, arg); }
+template <typename T, typename ...R>
+int PlGetv(term_t t, T arg, R... rest) { return PlGet(t, arg)? PlGetv(t+1, rest...): 0; }
+template <typename T>
+int Countv(int i, T arg) { return i+1; }
+template <typename T, typename ...R>
+int Countv(int i, T arg, R... rest) { return Countv(i+1, rest...); }
+template <typename ...T>
+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