diff options
Diffstat (limited to 'c/pl.h')
-rw-r--r-- | c/pl.h | 99 |
1 files changed, 50 insertions, 49 deletions
@@ -5,7 +5,6 @@ #include <SWI-Prolog.h> int PL_get_tchars(term_t t, TCHAR** pTsz, int iFlags); -int Plx(const char* szMod, const char* szPred); struct Frame { @@ -38,68 +37,70 @@ private: }; /* Polymorphic aliases for PL_put_*, PL_get_*. */ -inline int PlPut(const term_t t, const int x) { return PL_put_integer(t, x); } -inline int PlPut(const term_t t, const long x) { return PL_put_integer(t, x); } -inline int PlPut(const term_t t, const long long x) { return PL_put_integer(t, x); } -inline int PlPut(const term_t t, const atom_t x) { return PL_put_atom(t, x); } -inline int PlPut(const term_t t, char* const x) { return PL_put_atom_chars(t, x); } -inline int PlPut(const term_t t, const char* const x) { return PL_put_atom_chars(t, x); } -inline int PlPut(const term_t t, wchar_t* const x) { return PL_unify_wchars(t, PL_ATOM, -1, x); } -inline int PlPut(const term_t t, const wchar_t* const x) { return PL_unify_wchars(t, PL_ATOM, -1, x); } -inline int PlPut(const term_t, int* const) { return -1; } -inline int PlPut(const term_t, long* const) { return -1; } -inline int PlPut(const term_t, long long* const) { return -1; } -inline int PlPut(const term_t, atom_t* const) { return -1; } -inline int PlPut(const term_t, char** const) { return -1; } -inline int PlPut(const term_t, wchar_t** const) { return -1; } +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_int64(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_chars(t, x); } +inline int PlPut(term_t t, const char* x) { return PL_put_atom_chars(t, x); } +inline int PlPut(term_t t, wchar_t* x) { return PL_unify_wchars(t, PL_ATOM, -1, x); } +inline int PlPut(term_t t, const wchar_t* x) { return PL_unify_wchars(t, PL_ATOM, -1, x); } +inline int PlPut(term_t, int*) { return -1; } +inline int PlPut(term_t, long*) { return -1; } +inline int PlPut(term_t, long long*) { return -1; } +inline int PlPut(term_t, atom_t*) { return -1; } +inline int PlPut(term_t, char**) { return -1; } +inline int PlPut(term_t, wchar_t**) { return -1; } -inline int PlGet(const term_t, const int) { return -1; } -inline int PlGet(const term_t, const long) { return -1; } -inline int PlGet(const term_t, const long long) { return -1; } -inline int PlGet(const term_t, const atom_t) { return -1; } -inline int PlGet(const term_t, char* const) { return -1; } -inline int PlGet(const term_t, const char* const) { return -1; } -inline int PlGet(const term_t t, int* const x) { return PL_get_integer(t, x); } -inline int PlGet(const term_t t, long* const x) { return PL_get_long(t, x); } -inline int PlGet(const term_t t, long long* const x) { return PL_get_int64(t, x); } -inline int PlGet(const term_t t, atom_t* const x) { return PL_get_atom(t, x); } -inline int PlGet(const term_t t, char** const x) { return PL_get_atom_chars(t, x); } -inline int PlGet(const term_t t, wchar_t** const x) { size_t len; return PL_get_wchars(t, &len, x, CVT_ATOM); } +inline int PlGet(term_t, int) { return -1; } +inline int PlGet(term_t, long) { return -1; } +inline int PlGet(term_t, long long) { return -1; } +inline int PlGet(term_t, atom_t) { return -1; } +inline int PlGet(term_t, char*) { return -1; } +inline int PlGet(term_t, const char*) { return -1; } +inline int PlGet(term_t, wchar_t*) { return -1; } +inline int PlGet(term_t, const wchar_t*) { 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); } +inline int PlGet(term_t t, wchar_t** x) { size_t len; return PL_get_wchars(t, &len, x, CVT_ATOM); } /* Helper templates for Plx, Pl. */ -template <class T> -int PlPutv(const term_t t, T arg) { return PlPut(t, arg); } -template <class T, class... R> -int PlPutv(const term_t t, T arg, R... rest) { return PlPut(t, arg)? PlPutv(t+1, rest...): 0; } -template <class T> -int PlGetv(const term_t t, T arg) { return PlGet(t, arg); } -template <class T, class... R> -int PlGetv(const term_t t, T arg, R... rest) { return PlGet(t, arg)? PlGetv(t+1, rest...): 0; } -template <class T> -int Countv(const int i, T) { return i+1; } -template <class T, class... R> -int Countv(const int i, T, R... rest) { return Countv(i+1, rest...); } +template <typename = void> +inline int PlPutN(term_t) { return -1; } +template <typename T> +inline int PlPutN(term_t t, T arg) { return PlPut(t, arg); } +template <typename T, typename... U> +inline int PlPutN(term_t t, T arg, U... xs) { return PlPut(t, arg) && PlPutN(t+1, xs...); } + +template <typename = void> +inline int PlGetN(term_t) { return -1; } +template <typename T> +inline int PlGetN(term_t t, T arg) { return PlGet(t, arg); } +template <typename T, typename... U> +inline int PlGetN(term_t t, T arg, U... xs) { return PlGet(t, arg) && PlGetN(t+1, xs...); } /* Call Prolog predicate, propagating Prolog exceptions. */ -template <class... T> -int Plx(const char* const szMod, const char* const szPred, T... args) +template <typename... T> +int Plx(const char* const szMod, const char* const szPred, T... xs) { Frame f; - const int iArity = Countv(0, args...); - const term_t t = PL_new_term_refs(iArity); - if (!PlPutv(t, args...)) return 0; - Query q(NULL, PL_predicate(szPred, iArity, szMod), t); + 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 (!PlGetv(t, args...)) return 0; + if (!PlGetN(t, xs...)) return 0; return 1; } /* Call Prolog predicate, ignoring Prolog exceptions. */ -template <class... T> -int Pl(const char* const szMod, const char* const szPred, T... args) +template <typename... T> +int Pl(const char* const szMod, const char* const szPred, T... xs) { try { - return Plx(szMod, szPred, args...); + return Plx(szMod, szPred, xs...); } catch (term_t& t) { return 0; } |