aboutsummaryrefslogtreecommitdiff
path: root/c/pl.h
blob: 8325c866df884683846eb25a156988b0c1d0df2d (plain)
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
#ifndef PL_H
#define PL_H

#include <windows.h>
#include <SWI-Prolog.h>

int PL_get_tchars(term_t t, TCHAR** pTsz, int iFlags);

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(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(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); }

/* Put in or get from a term reference an arbitrary number of values,
 * returning false if any value could not be put/got. */
template <typename = void>
inline bool PlPutN(term_t) { return true; }
template <typename T, typename... U>
inline bool PlPutN(term_t t, T x, U... xs) { return PlPut(t, x) && PlPutN(t+1, xs...); }

template <typename = void>
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)
{
	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 (!PlGetN(t, xs...)) return 0;
	return 1;
}

/* Call Prolog predicate, ignoring Prolog exceptions. */
template <typename... T>
int Pl(const char* const szMod, const char* const szPred, T... xs)
{
	try {
		return Plx(szMod, szPred, xs...);
	} catch (term_t& t) {
		return 0;
	}
}

#endif