-
Notifications
You must be signed in to change notification settings - Fork 14
/
SWI-cpp.h
1379 lines (1079 loc) · 29.1 KB
/
SWI-cpp.h
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2000-2024, University of Amsterdam
VU University Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#warning "SWI-cpp.h is obsolete and replaced by SWI-cpp2.h"
#ifndef _SWI_CPP_H
#define _SWI_CPP_H
#include <SWI-Prolog.h>
#include <string.h>
#include <wchar.h>
#if !(defined(__APPLE__) || defined(__FreeBSD__))
#include <malloc.h>
#endif
#ifdef __BORLANDC__
#define __inline inline
#endif
/* Define as 1 if undefined or defined as empty */
#if !defined(PL_ARITY_AS_SIZE) || (0-PL_ARITY_AS_SIZE-1)==1
#undef PL_ARITY_AS_SIZE
#define PL_ARITY_AS_SIZE 1
#endif
#ifndef ARITY_T
#if PL_ARITY_AS_SIZE
#define ARITY_T size_t
#else
#define ARITY_T int
#endif
#endif
class PlTerm;
class PlTermv;
/*******************************
* PROLOG CONSTANTS *
*******************************/
class PlFunctor
{
public:
functor_t functor;
PlFunctor(const char *name, ARITY_T arity)
{ functor = PL_new_functor(PL_new_atom(name), arity);
}
PlFunctor(const wchar_t *name, ARITY_T arity)
{ functor = PL_new_functor(PL_new_atom_wchars(wcslen(name), name), arity);
}
operator functor_t(void) const
{ return functor;
}
int operator ==(functor_t to) const
{ return functor == to;
}
};
class PlAtom
{
public:
atom_t handle;
PlAtom(atom_t h)
{ handle = h;
}
PlAtom(const char *text)
{ handle = PL_new_atom(text);
}
PlAtom(const wchar_t *text)
{ handle = PL_new_atom_wchars(wcslen(text), text);
}
PlAtom(const PlTerm &t);
operator const char *(void) const
{ return PL_atom_chars(handle);
}
operator const wchar_t *(void) const
{ return PL_atom_wchars(handle, NULL);
}
int operator ==(const char *s) const
{ return strcmp(s, PL_atom_chars(handle)) == 0;
}
int operator ==(const wchar_t *s) const
{ return wcscmp(s, PL_atom_wchars(handle, NULL)) == 0;
}
int operator ==(const PlAtom &a) const
{ return handle == a.handle;
}
int operator ==(atom_t to) const
{ return handle == to;
}
};
/*******************************
* GENERIC PROLOG TERM *
*******************************/
class PlTerm
{
public:
term_t ref;
PlTerm();
PlTerm(const PlTerm &other) : ref(other.ref) {}
PlTerm(term_t t)
{ ref = t;
}
/* C --> PlTerm */
PlTerm(const char *text);
PlTerm(const wchar_t *text);
PlTerm(long val);
PlTerm(double val);
PlTerm(const PlAtom &a);
PlTerm(void *ptr);
/* PlTerm --> C */
operator term_t(void) const
{ return ref;
}
operator char *(void) const;
operator wchar_t *(void) const;
operator long(void) const;
operator int(void) const;
operator uint32_t(void) const;
operator bool(void) const;
operator double(void) const;
operator PlAtom(void) const;
operator void *(void) const;
int type() const
{ return PL_term_type(ref);
}
/* Compounds */
PlTerm operator [](ARITY_T index) const;
ARITY_T arity() const;
const char *name() const;
/* UNIFY */
int operator =(const PlTerm &t2); /* term */
int operator =(const PlAtom &a); /* atom */
int operator =(const char *v); /* atom (from char *) */
int operator =(const wchar_t *v); /* atom (from wchar_t *) */
int operator =(long v); /* integer */
int operator =(int v); /* integer */
int operator =(double v); /* float */
int operator =(const PlFunctor &f); /* functor */
/* Comparison standard order terms */
int operator ==(const PlTerm &t2) const
{ return PL_compare(ref, t2.ref) == 0;
}
int operator !=(const PlTerm &t2) const
{ return PL_compare(ref, t2.ref) != 0;
}
int operator <(const PlTerm &t2) const
{ return PL_compare(ref, t2.ref) < 0;
}
int operator >(const PlTerm &t2) const
{ return PL_compare(ref, t2.ref) > 0;
}
int operator <=(const PlTerm &t2) const
{ return PL_compare(ref, t2.ref) <= 0;
}
int operator >=(const PlTerm &t2) const
{ return PL_compare(ref, t2.ref) >= 0;
}
/* comparison (long) */
int operator ==(long v) const;
int operator !=(long v) const;
int operator <(long v) const;
int operator >(long v) const;
int operator <=(long v) const;
int operator >=(long v) const;
/* comparison (string) */
int operator ==(const char *s) const;
int operator ==(const wchar_t *s) const;
int operator ==(const PlAtom &a) const;
};
/*******************************
* TERM VECTOR *
*******************************/
class PlTermv
{
public:
term_t a0;
size_t size;
PlTermv(int n)
{ a0 = PL_new_term_refs(n);
size = static_cast<size_t>(n);
}
PlTermv(int n, term_t t0)
{ a0 = t0;
size = static_cast<size_t>(n);
}
PlTermv(size_t n)
{ a0 = PL_new_term_refs(static_cast<int>(n));
size = n;
}
PlTermv(size_t n, term_t t0)
{ a0 = t0;
size = n;
}
/* create from args */
PlTermv(PlTerm m0);
PlTermv(PlTerm m0, PlTerm m1);
PlTermv(PlTerm m0, PlTerm m1, PlTerm m2);
PlTermv(PlTerm m0, PlTerm m1, PlTerm m2, PlTerm m3);
PlTermv(PlTerm m0, PlTerm m1, PlTerm m2, PlTerm m3, PlTerm m4);
PlTerm operator [](size_t n) const;
};
/*******************************
* SPECIALISED TERMS *
*******************************/
class PlCompound : public PlTerm
{
public:
PlCompound(const char *text);
PlCompound(const wchar_t *text);
PlCompound(const char *functor, const PlTermv &args);
PlCompound(const wchar_t *functor, const PlTermv &args);
};
class PlString : public PlTerm
{
public:
PlString(const char *text);
PlString(const char *text, size_t len);
PlString(const wchar_t *text);
PlString(const wchar_t *text, size_t len);
};
class PlCodeList : public PlTerm
{
public:
PlCodeList(const char *text);
PlCodeList(const wchar_t *text);
};
class PlCharList : public PlTerm
{
public:
PlCharList(const char *text);
PlCharList(const wchar_t *text);
};
/*******************************
* EXCEPTIONS *
*******************************/
class PlException : public PlTerm
{
public:
PlException()
{ term_t ex = PL_exception(0);
if ( ex )
ref = ex;
else
PL_fatal_error("No exception");
}
PlException(const PlTerm &t)
{ ref = t.ref;
}
operator const char *(void);
operator const wchar_t *(void);
foreign_t plThrow()
{ return static_cast<foreign_t>(PL_raise_exception(ref));
}
void cppThrow();
};
class PlTypeError : public PlException
{
public:
PlTypeError(const PlTerm &t) : PlException(t) {}
PlTypeError(const char *expected, PlTerm actual) :
PlException(PlCompound("error",
PlTermv(PL_is_variable(actual.ref) ?
PlTerm("instantiation_error") :
PlCompound("type_error",
PlTermv(expected, actual)),
PlTerm())))
{
}
};
class PlDomainError : public PlException
{
public:
PlDomainError(const PlTerm &t) : PlException(t) {}
PlDomainError(const char *expected, PlTerm actual) :
PlException(PlCompound("error",
PlTermv(PlCompound("domain_error",
PlTermv(expected, actual)),
PlTerm())))
{
}
};
class PlInstantiationError : public PlException
{
public:
PlInstantiationError(const PlTerm &t) :
PlException(PL_is_variable(t) ?
PlCompound("error",
PlTermv("instantiation_error",
t)) : t) {}
PlInstantiationError() :
PlException(PlCompound("error",
PlTermv("instantiation_error",
PlTerm())))
{
}
};
class PlExistenceError : public PlException
{
public:
PlExistenceError(const PlTerm &t) : PlException(t) {}
PlExistenceError(const char *type, PlTerm actual) :
PlException(PlCompound("error",
PlTermv(PlCompound("existence_error",
PlTermv(type, actual)),
PlTerm())))
{
}
};
class PlPermissionError : public PlException
{
public:
PlPermissionError(const PlTerm &t) : PlException(t) {}
PlPermissionError(const char *op, const char *type, PlTerm obj) :
PlException(PlCompound("error",
PlTermv(PlCompound("permission_error",
PlTermv(op, type, obj)),
PlTerm())))
{
}
};
class PlResourceError : public PlException
{
public:
PlResourceError() : PlException() {}
PlResourceError(const PlTerm &t) : PlException(t) {}
PlResourceError(const char *resource) :
PlException(PlCompound("error",
PlTermv(PlCompound("resource_error",
PlTermv(PlTerm(resource))),
PlTerm())))
{
}
};
class PlTermvDomainError : public PlException
{
public:
PlTermvDomainError(size_t size, size_t n) :
PlException(PlCompound("error",
PlTermv(PlCompound("domain_error",
PlTermv(PlCompound("argv",
size),
PlTerm(static_cast<long>(n)))),
PlTerm())))
{
}
};
/*******************************
* PLTERM IMPLEMENTATION *
*******************************/
__inline
PlTerm::PlTerm()
{ if ( !(ref = PL_new_term_ref()) )
throw PlResourceError();
}
__inline
PlTerm::PlTerm(const char *text)
{ if ( !(ref = PL_new_term_ref()) ||
!PL_put_atom_chars(ref, text) )
throw PlResourceError();
}
__inline
PlTerm::PlTerm(const wchar_t *text)
{ if ( !(ref = PL_new_term_ref()) ||
!PL_unify_wchars(ref, PL_ATOM, static_cast<size_t>(-1), text) )
throw PlResourceError();
}
__inline
PlTerm::PlTerm(long val)
{ if ( !(ref = PL_new_term_ref()) ||
!PL_put_integer(ref, val) )
throw PlResourceError();
}
__inline
PlTerm::PlTerm(double val)
{ if ( !(ref = PL_new_term_ref()) ||
!PL_put_float(ref, val) )
throw PlResourceError();
}
__inline
PlTerm::PlTerm(const PlAtom &a)
{ if ( !(ref = PL_new_term_ref()) )
throw PlResourceError();
PL_put_atom(ref, a.handle);
}
__inline
PlTerm::PlTerm(void *ptr)
{ if ( !(ref = PL_new_term_ref()) ||
!PL_put_pointer(ref, ptr) )
throw PlResourceError();
}
/*******************************
* SPECIALISED IMPLEMENTATIONS *
*******************************/
__inline
PlString::PlString(const char *text) : PlTerm()
{ if ( !PL_put_string_chars(ref, text) )
throw PlResourceError();
}
__inline
PlString::PlString(const char *text, size_t len) : PlTerm()
{ if ( !PL_put_string_nchars(ref, len, text) )
throw PlResourceError();
}
__inline
PlString::PlString(const wchar_t *text) : PlTerm()
{ if ( !PL_unify_wchars(ref, PL_STRING, static_cast<size_t>(-1), text) )
throw PlResourceError();
}
__inline
PlString::PlString(const wchar_t *text, size_t len) : PlTerm()
{ if ( !PL_unify_wchars(ref, PL_STRING, len, text) )
throw PlResourceError();
}
__inline
PlCodeList::PlCodeList(const char *text) : PlTerm()
{ if ( !PL_put_list_codes(ref, text) )
throw PlResourceError();
}
__inline
PlCharList::PlCharList(const char *text) : PlTerm()
{ if ( !PL_put_list_chars(ref, text) )
throw PlResourceError();
}
__inline
PlCodeList::PlCodeList(const wchar_t *text) : PlTerm()
{ if ( !PL_unify_wchars(ref, PL_CODE_LIST, static_cast<size_t>(-1), text) )
throw PlResourceError();
}
__inline
PlCharList::PlCharList(const wchar_t *text) : PlTerm()
{ if ( !PL_unify_wchars(ref, PL_CHAR_LIST, static_cast<size_t>(-1), text) )
throw PlResourceError();
}
/*******************************
* LISTS *
*******************************/
class PlTail : public PlTerm
{
public:
PlTail(const PlTerm &l)
{ if ( PL_is_variable(l.ref) || PL_is_list(l.ref) )
{ if ( !(ref = PL_copy_term_ref(l.ref)) )
throw PlResourceError();
} else
throw PlTypeError("list", l.ref);
}
/* building */
int append(const PlTerm &e)
{ term_t tmp, ex;
if ( (tmp = PL_new_term_ref()) &&
PL_unify_list(ref, tmp, ref) &&
PL_unify(tmp, e.ref) )
{ PL_reset_term_refs(tmp);
return TRUE;
}
if ( (ex = PL_exception(0)) )
throw PlResourceError(ex);
return FALSE;
}
int close()
{ return PL_unify_nil(ref);
}
/* enumerating */
int next(PlTerm &t)
{ if ( PL_get_list(ref, t, ref) )
return TRUE;
if ( PL_get_nil(ref) )
return FALSE;
throw PlTypeError("list", ref);
}
};
/*******************************
* REGISTER *
*******************************/
class PlRegister
{
public:
PlRegister(const char *module, const char *name, int arity,
foreign_t (f)(term_t t0, int a, control_t ctx))
{ PL_register_foreign_in_module(module, name, arity, reinterpret_cast<pl_function_t>(f), PL_FA_VARARGS);
}
PlRegister(const char *module, const char *name, foreign_t (*f)(PlTerm a0))
{ PL_register_foreign_in_module(module, name, 1, reinterpret_cast<pl_function_t>(f), 0);
}
PlRegister(const char *module, const char *name, foreign_t (*f)(PlTerm a0, PlTerm a1))
{ PL_register_foreign_in_module(module, name, 2, reinterpret_cast<pl_function_t>(f), 0);
}
PlRegister(const char *module, const char *name, foreign_t (*f)(PlTerm a0, PlTerm a1, PlTerm a2))
{ PL_register_foreign_in_module(module, name, 3, reinterpret_cast<pl_function_t>(f), 0);
}
// for non-deterministic calls
PlRegister(const char *module, const char *name, int arity,
foreign_t (f)(term_t t0, int a, control_t ctx), short flags)
{ PL_register_foreign_in_module(module, name, arity, reinterpret_cast<pl_function_t>(f), flags);
}
};
/*******************************
* CALLING PROLOG *
*******************************/
class PlFrame
{
public:
fid_t fid;
PlFrame()
{ fid = PL_open_foreign_frame();
}
~PlFrame()
{ PL_close_foreign_frame(fid);
}
void rewind()
{ PL_rewind_foreign_frame(fid);
}
};
class PlQuery
{
public:
qid_t qid;
PlQuery(predicate_t pred, const PlTermv &av)
{ qid = PL_open_query(static_cast<module_t>(0), PL_Q_PASS_EXCEPTION, pred, av.a0);
if ( !qid )
throw PlResourceError();
}
PlQuery(const char *name, const PlTermv &av)
{ predicate_t p = PL_predicate(name, static_cast<int>(av.size), "user");
qid = PL_open_query(static_cast<module_t>(0), PL_Q_PASS_EXCEPTION, p, av.a0);
if ( !qid )
throw PlResourceError();
}
PlQuery(const char *module, const char *name, const PlTermv &av)
{ atom_t ma = PL_new_atom(module);
atom_t na = PL_new_atom(name);
module_t m = PL_new_module(ma);
predicate_t p = PL_pred(PL_new_functor(na, av.size), m);
PL_unregister_atom(ma);
PL_unregister_atom(na);
qid = PL_open_query(m, PL_Q_PASS_EXCEPTION, p, av.a0);
if ( !qid )
throw PlResourceError();
}
~PlQuery()
{ if ( qid )
PL_cut_query(qid);
}
int next_solution();
};
__inline int
PlCall(const char *predicate, const PlTermv &args)
{ PlQuery q(predicate, args);
return q.next_solution();
}
__inline int
PlCall(const char *module, const char *predicate, const PlTermv &args)
{ PlQuery q(module, predicate, args);
return q.next_solution();
}
__inline int
PlCall(const char *goal)
{ PlQuery q("call", PlTermv(PlCompound(goal)));
return q.next_solution();
}
__inline int
PlCall(const wchar_t *goal)
{ PlQuery q("call", PlTermv(PlCompound(goal)));
return q.next_solution();
}
/*******************************
* ATOM (BODY) *
*******************************/
__inline
PlAtom::PlAtom(const PlTerm &t)
{ atom_t a;
if ( PL_get_atom(t.ref, &a) )
handle = a;
else
throw PlTypeError("atom", t);
}
/*******************************
* TERM (BODY) *
*******************************/
/* PlTerm --> C */
__inline PlTerm::operator char *(void) const
{ char *s;
if ( PL_get_chars(ref, &s, CVT_ALL|CVT_WRITEQ|BUF_RING) )
return s;
throw PlTypeError("text", ref);
}
__inline PlTerm::operator wchar_t *(void) const
{ wchar_t *s;
if ( PL_get_wchars(ref, NULL, &s, CVT_ALL|CVT_WRITEQ|BUF_RING) )
return s;
throw PlTypeError("text", ref);
}
__inline PlTerm::operator long(void) const
{ long v;
if ( PL_get_long(ref, &v) )
return v;
throw PlTypeError("integer", ref);
}
__inline PlTerm::operator int(void) const
{ int v;
if ( PL_get_integer(ref, &v) )
return v;
throw PlTypeError("integer", ref);
}
__inline PlTerm::operator uint32_t(void) const
{ int64_t v;
if ( PL_get_int64(ref, &v) && v >= 0 && v <= UINT32_MAX )
return v;
throw PlTypeError("uint32_t", ref);
}
__inline PlTerm::operator bool(void) const
{ int v;
if ( PL_get_bool(ref, &v) )
return v;
throw PlTypeError("bool", ref);
}
__inline PlTerm::operator double(void) const
{ double v;
if ( PL_get_float(ref, &v) )
return v;
throw PlTypeError("float", ref);
}
__inline PlTerm::operator PlAtom(void) const
{ atom_t v;
if ( PL_get_atom(ref, &v) )
return PlAtom(v);
throw PlTypeError("atom", ref);
}
__inline PlTerm::operator void *(void) const
{ void *ptr;
if ( PL_get_pointer(ref, &ptr) )
return ptr;
throw PlTypeError("pointer", ref);
}
/* compounds */
__inline PlTerm
PlTerm::operator [](ARITY_T index) const
{ PlTerm t;
if ( PL_get_arg(index, ref, t.ref) )
return t;
if ( !PL_is_compound(ref) )
{ throw PlTypeError("compound", ref);
} else
{ if ( !PL_put_uint64(t.ref, index) )
throw PlResourceError();
if ( index < 1 )
throw PlDomainError("not_less_than_zero", t.ref);
else
throw PlDomainError("arity", t.ref); /* TBD: proper exception */
}
}
__inline ARITY_T
PlTerm::arity() const
{ atom_t name;
ARITY_T arity;
if ( PL_get_name_arity(ref, &name, &arity) )
return arity;
throw PlTypeError("compound", ref);
}
__inline const char *
PlTerm::name() const
{ atom_t name;
ARITY_T arity;
if ( PL_get_name_arity(ref, &name, &arity) )
return PL_atom_chars(name);
throw PlTypeError("compound", ref);
}
/* Unification */
__inline int PlTerm::operator =(const PlTerm &t2) /* term = term */
{ int rc = PL_unify(ref, t2.ref);
term_t ex;
if ( !rc && (ex=PL_exception(0)) )
throw PlResourceError(ex);
return rc;
}
__inline int PlTerm::operator =(const PlAtom &a) /* term = atom */
{ int rc = PL_unify_atom(ref, a.handle);
term_t ex;
if ( !rc && (ex=PL_exception(0)) )
throw PlResourceError(ex);
return rc;
}
__inline int PlTerm::operator =(const char *v) /* term = atom */
{ int rc = PL_unify_atom_chars(ref, v);
term_t ex;
if ( !rc && (ex=PL_exception(0)) )
throw PlResourceError(ex);
return rc;
}
__inline int PlTerm::operator =(const wchar_t *v) /* term = atom */
{ int rc = PL_unify_wchars(ref, PL_ATOM, static_cast<size_t>(-1), v);
term_t ex;
if ( !rc && (ex=PL_exception(0)) )
throw PlResourceError(ex);
return rc;
}
__inline int PlTerm::operator =(long v)
{ int rc = PL_unify_integer(ref, v);
term_t ex;
if ( !rc && (ex=PL_exception(0)) )
throw PlResourceError(ex);
return rc;
}
__inline int PlTerm::operator =(int v)
{ int rc = PL_unify_integer(ref, v);
term_t ex;
if ( !rc && (ex=PL_exception(0)) )
throw PlResourceError(ex);
return rc;
}
__inline int PlTerm::operator =(double v)
{ int rc = PL_unify_float(ref, v);
term_t ex;
if ( !rc && (ex=PL_exception(0)) )
throw PlResourceError(ex);
return rc;
}
__inline int PlTerm::operator =(const PlFunctor &f)
{ int rc = PL_unify_functor(ref, f.functor);
term_t ex;
if ( !rc && (ex=PL_exception(0)) )
throw PlResourceError(ex);
return rc;
}
/* comparison */
__inline int PlTerm::operator ==(long v) const
{ long v0;
if ( PL_get_long(ref, &v0) )
return v0 == v;
throw PlTypeError("integer", ref);
}
__inline int PlTerm::operator !=(long v) const
{ long v0;
if ( PL_get_long(ref, &v0) )
return v0 != v;
throw PlTypeError("integer", ref);
}
__inline int PlTerm::operator <(long v) const
{ long v0;
if ( PL_get_long(ref, &v0) )
return v0 < v;
throw PlTypeError("integer", ref);
}
__inline int PlTerm::operator >(long v) const
{ long v0;
if ( PL_get_long(ref, &v0) )
return v0 > v;
throw PlTypeError("integer", ref);
}
__inline int PlTerm::operator <=(long v) const
{ long v0;
if ( PL_get_long(ref, &v0) )
return v0 <= v;
throw PlTypeError("integer", ref);
}
__inline int PlTerm::operator >=(long v) const