aboutsummaryrefslogtreecommitdiff
path: root/c/pl.h
diff options
context:
space:
mode:
Diffstat (limited to 'c/pl.h')
-rw-r--r--c/pl.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/c/pl.h b/c/pl.h
new file mode 100644
index 0000000..f0edfb8
--- /dev/null
+++ b/c/pl.h
@@ -0,0 +1,92 @@
+#ifndef PL_H
+#define PL_H
+
+#include <SWI-Prolog.h>
+
+int Plx(const char *, const char *);
+struct Query
+{
+ Query(module_t ctx, predicate_t p, term_t t0);
+ ~Query(void);
+ int Cut();
+ int Close();
+ int NextSolution();
+private:
+ qid_t m_q;
+};
+
+#define DLVSIKEY 0
+#define DLVSIVALUE 1
+
+/* Polymorphic aliases for PL_put_*, PL_get_*. */
+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); }
+
+/* Helper templates for Plx, Pl. */
+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...); }
+
+/* Call Prolog predicate, propagating Prolog exceptions. */
+template <typename ...T>
+int Plx(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;
+ Query q(NULL, PL_predicate(szPred, iArity, szMod), t);
+ if (!q.NextSolution()) return 0;
+ if (!PlGetv(t, args...)) return 0;
+ return 1;
+}
+
+/* Call Prolog predicate, ignoring Prolog exceptions. */
+inline int Pl(const char *szMod, const char *szPred)
+{
+ try {
+ return Plx(szMod, szPred);
+ } catch (term_t &t) {
+ return 0;
+ }
+}
+template <typename ...T>
+int Pl(const char *szMod, const char *szPred, T... args)
+{
+ try {
+ return Plx(szMod, szPred, args...);
+ } catch (term_t &t) {
+ return 0;
+ }
+}
+
+#endif