diff options
-rw-r--r-- | c/common.h | 22 | ||||
-rw-r--r-- | c/pl.cpp | 19 | ||||
-rw-r--r-- | c/pl.h | 99 |
3 files changed, 61 insertions, 79 deletions
@@ -34,7 +34,7 @@ private: HMODULE m_hModule; }; -template <class T> +template <typename T> T* Library::GetProcAddress(const char* const szProc) { return (T*)(void*)::GetProcAddress(m_hModule, szProc); @@ -43,32 +43,32 @@ T* Library::GetProcAddress(const char* const szProc) /* Create and return an object of type C. If construction fails, * return nothing. The returned value must be checked before being * used, as dereferencing is undefined if the value is empty. */ -template <class C, class... T> -std::optional<C> maybe_make(T... args) +template <typename T, typename... U> +std::optional<T> maybe_make(U... xs) { try { - return C(args...); + return T(xs...); } catch (...) { return {}; } } /* Call Windows API function, throwing error on NULL. */ -template <auto f, class... T> -inline auto throw_nil(T... args) +template <auto F, typename... T> +inline auto throw_nil(T... xs) { - auto r = f(args...); + auto r = F(xs...); if (!r) throw Win32Error(GetLastError()); return r; } /* Call Windows API function, showing a warning on NULL. */ -template <auto f, class... T> -inline auto warn_nil(T... args) +template <auto F, typename... T> +inline auto warn_nil(T... xs) { - decltype(f(std::declval<T>()...)) r; + decltype(F(std::declval<T>()...)) r; try { - r = throw_nil<f>(args...); + r = throw_nil<F>(xs...); } catch (Win32Error& e) { EBMessageBox(e.twhat(), TEXT("System Error"), MB_ICONWARNING); } @@ -91,22 +91,3 @@ int PL_get_tchars(const term_t t, TCHAR** const pTsz, const int iFlags) return -1; #endif } - -/* Call nullary Prolog predicate, propagating Prolog exceptions. */ -int Plx(const char* const szMod, const char* const szPred) -{ - Frame f; - const term_t t = PL_new_term_refs(0); - Query q(NULL, PL_predicate(szPred, 0, szMod), t); - return q.NextSolution(); -} - -/* Call nullary Prolog predicate, ignoring Prolog exceptions. */ -int Pl(const char* const szMod, const char* const szPred) -{ - try { - return Plx(szMod, szPred); - } catch (term_t& t) { - return 0; - } -} @@ -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; } |