1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#ifndef PL_H
#define PL_H
#include <windows.h>
#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
{
Frame();
~Frame();
void Close();
void Discard();
void Rewind();
private:
fid_t m_f;
};
struct Mark
{
Mark();
~Mark();
private:
buf_mark_t m_m;
};
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(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 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); }
/* 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...); }
/* Call Prolog predicate, propagating Prolog exceptions. */
template <class... T>
int Plx(const char* const szMod, const char* const szPred, T... args)
{
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);
if (!q.NextSolution()) return 0;
if (!PlGetv(t, args...)) 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)
{
try {
return Plx(szMod, szPred, args...);
} catch (term_t& t) {
return 0;
}
}
#endif
|