aboutsummaryrefslogtreecommitdiff
path: root/c/defs.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-15 13:13:50 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-15 13:13:50 +0200
commit8f1ead168558dac9cf4c10e269f568e15b5f8faf (patch)
treeb749f5abfa5720f491a56740f61f6de00b9e0bfc /c/defs.h
parent9d5ee7f8bd62f11cb0d9019cc4a715f265452bb7 (diff)
downloadEpisodeBrowser-8f1ead168558dac9cf4c10e269f568e15b5f8faf.tar.gz
Add support for Prolog exceptions.
Diffstat (limited to 'c/defs.h')
-rw-r--r--c/defs.h41
1 files changed, 37 insertions, 4 deletions
diff --git a/c/defs.h b/c/defs.h
index c8d76b3..5a6a25a 100644
--- a/c/defs.h
+++ b/c/defs.h
@@ -66,8 +66,19 @@ struct DataListView : public ListView
};
/* pl.cpp */
-int Pl(const char *, const char *);
+int Plx(const char *, const char *);
+struct Query
+{
+ Query(module_t ctx, predicate_t p, term_t t0);
+ ~Query();
+ int Cut();
+ int Close();
+ int NextSolution();
+private:
+ qid_t m_q;
+};
+/* 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); }
@@ -92,6 +103,7 @@ 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>
@@ -104,18 +116,39 @@ 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 Pl(const char *szMod, const char *szPred, T... args)
+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;
- if (!PL_call_predicate(NULL, PL_Q_CATCH_EXCEPTION, PL_predicate(szPred, iArity, szMod), t))
- 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;
+ }
+}
+
/* defs.h */
#ifdef UNICODE
#define WA "W"