aboutsummaryrefslogtreecommitdiff
path: root/c
diff options
context:
space:
mode:
Diffstat (limited to 'c')
-rw-r--r--c/common.cpp3
-rw-r--r--c/defs.h41
-rw-r--r--c/pl.cpp39
3 files changed, 74 insertions, 9 deletions
diff --git a/c/common.cpp b/c/common.cpp
index 89cc426..dd84742 100644
--- a/c/common.cpp
+++ b/c/common.cpp
@@ -31,7 +31,8 @@ TCHAR *TszFromSz(const char *sz, int iCp)
Library::Library(const TCHAR *tszLibrary)
{
- if (!(m_hModule = LoadLibrary(tszLibrary)))
+ m_hModule = LoadLibrary(tszLibrary);
+ if (!m_hModule)
throw std::invalid_argument("Library not found.");
}
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"
diff --git a/c/pl.cpp b/c/pl.cpp
index 3172090..1fbc4e7 100644
--- a/c/pl.cpp
+++ b/c/pl.cpp
@@ -1,9 +1,40 @@
#include <SWI-Prolog.h>
+#include "defs.h"
-int Pl(const char *szMod, const char *szPred)
+Query::Query(module_t ctx, predicate_t p, term_t t0)
+{
+ m_q = PL_open_query(ctx, PL_Q_CATCH_EXCEPTION, p, t0);
+}
+
+Query::~Query()
+{
+ PL_cut_query(m_q);
+}
+
+int Query::Cut()
+{
+ if (PL_cut_query(m_q)) return 1;
+ if (term_t t = PL_exception(m_q)) throw t;
+ return 0;
+}
+
+int Query::Close()
+{
+ if (PL_close_query(m_q)) return 1;
+ if (term_t t = PL_exception(m_q)) throw t;
+ return 0;
+}
+
+int Query::NextSolution()
+{
+ if (PL_next_solution(m_q)) return 1;
+ if (term_t t = PL_exception(m_q)) throw t;
+ return 0;
+}
+
+int Plx(const char *szMod, const char *szPred)
{
term_t t = PL_new_term_refs(0);
- if (!PL_call_predicate(NULL, PL_Q_CATCH_EXCEPTION, PL_predicate(szPred, 0, szMod), t))
- return 0;
- return 1;
+ Query q(NULL, PL_predicate(szPred, 0, szMod), t);
+ return q.NextSolution();
}