aboutsummaryrefslogtreecommitdiff
path: root/c/pl.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-30 00:23:51 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-30 00:23:51 +0200
commit8e7ce9d430b439995ceddca2c4fb9e24ed323d3a (patch)
tree15c6b856e304db5c097cf67f52f85160adce82e9 /c/pl.h
parentbabbd4c9c26d997683280ac663e03b04910b2826 (diff)
downloadEpisodeBrowser-8e7ce9d430b439995ceddca2c4fb9e24ed323d3a.tar.gz
Add non-throwing variants of Query methods.
Pl has also been refactored, so that the unnecessary throw and catch have been removed.
Diffstat (limited to 'c/pl.h')
-rw-r--r--c/pl.h28
1 files changed, 17 insertions, 11 deletions
diff --git a/c/pl.h b/c/pl.h
index 4a5ea41..c0af4bc 100644
--- a/c/pl.h
+++ b/c/pl.h
@@ -33,8 +33,11 @@ struct Query
Query(module_t ctx, predicate_t p, term_t t0);
~Query();
int Cut();
+ int Cut(std::nothrow_t);
int Close();
+ int Close(std::nothrow_t);
int NextSolution();
+ int NextSolution(std::nothrow_t);
private:
qid_t m_q;
};
@@ -92,28 +95,31 @@ inline bool PlGetN(term_t) { return true; }
template <typename T, typename... U>
inline bool PlGetN(term_t t, T x, U... xs) { return PlGet(t, x) && PlGetN(t+1, xs...); }
-/* Call Prolog predicate, propagating Prolog exceptions. */
-template <typename... T>
-int Plx(const char* const szMod, const char* const szPred, T... xs)
+/* Call Prolog predicate. */
+template <bool Except = false, typename... T>
+int Pl(const char* const szMod, const char* const szPred, T... xs)
{
Frame f;
const term_t t = PL_new_term_refs(sizeof...(T));
+
if (!PlPutN(t, xs...)) return 0;
Query q(NULL, PL_predicate(szPred, sizeof...(T), szMod), t);
- if (!q.NextSolution()) return 0;
+
+ if constexpr (Except) {
+ if (!q.NextSolution()) return 0;
+ } else {
+ if (!q.NextSolution(std::nothrow)) return 0;
+ }
+
if (!PlGetN(t, xs...)) return 0;
return 1;
}
-/* Call Prolog predicate, ignoring Prolog exceptions. */
+/* Call Prolog predicate, propagating Prolog exceptions. */
template <typename... T>
-int Pl(const char* const szMod, const char* const szPred, T... xs)
+int Plx(const char* const szMod, const char* const szPred, T... xs)
{
- try {
- return Plx(szMod, szPred, xs...);
- } catch (const term_t& t) {
- return 0;
- }
+ return Pl<true>(szMod, szPred, xs...);
}
#endif