-
Notifications
You must be signed in to change notification settings - Fork 14
/
SWI-cpp2.h
2087 lines (1739 loc) · 73.7 KB
/
SWI-cpp2.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 and Peter Ludemann
E-mail: J.Wielemaker@vu.nl
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.
*/
/*********************************************************************
SWI-cpp2.h is a significant rewrite of SWI-cpp.h, taking into account
experiences with the original code. A discussion of these changes is
in https://swi-prolog.discourse.group/t/changes-to-swi-cpp-h/5601
and in the "Rational" section of the documentation
https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/pl2cpp.html%27)
For porting from SWI-cpp.h to SWI-cpp2.h, please see the documentation
https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/pl2cpp.html%27)
Wherever possible, SWI-cpp2.h tries to maintain backwards compatiblity
with SWI-cpp.h, but often that has not been possible due to a
combination of design choices in SWI-Prolog.h and the ways that
various compilers have implemented details of the C++ standard,
particularly integer conversions.
*********************************************************************/
#ifndef _SWI_CPP2_H
#define _SWI_CPP2_H
#include <SWI-Prolog.h>
#include <SWI-Stream.h>
#include <climits>
#include <cstdint>
#include <cstring>
#include <cwchar>
#include <functional>
#include <string>
#include <cassert>
#include <memory>
#include <typeinfo>
#if INT_MAX != 0x7fffffff
#error "Unexpected value for INT_MAX"
#endif
#if LONG_MAX == 0x7fffffffffffffff
#if SIZE_MAX != 0xffffffffffffffff
#error "Unexpected value for SIZE_MAX"
#endif
#elif LONG_MAX == 0x7fffffff
#if SIZE_MAX == 0xffffffffffffffff || SIZE_MAX == 0xffffffff
#else
#error "Unexpected value for SIZE_MAX"
#endif
#else
#error "Unexpected value for LONG_MAX"
#endif
#if !(defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__))
#include <malloc.h>
#endif
class PlAtom;
class PlTerm;
class PlTermv;
class PlRecord;
class PlRecordExternalCopy;
class PlBlob;
// PlExceptionBase is used for try-catch that handles the exceptions
// defined in this header file but excludes the standard C++
// exceptions.
class PlExceptionBase : public std::exception
{
};
// PlExceptionFail is for PlFail and PlExceptionFail in
// PREDICATE_CATCH but excludes PlException
class PlExceptionFailBase : PlExceptionBase
{
};
// PlFail is a pseudo-exception for quick exit on failure, for use by
// the PlTerm::unify methods and PlQuery::next_solution(). This is
// special-cased in the PREDICATE et al macros. Note that it is *not*
// a subclass of PlException. See the documentation for more details
// on how this works with returning Prolog failure and returning
// exceptions.
class PlFail : public PlExceptionFailBase
{
public:
explicit PlFail() {}
virtual const char* what() const throw() override
{ return "PlFail";
}
};
// PlExceptionFail is a variant of PlFail, for when a resource error
// happens and we can't use PlException (because we're out of
// resources and therefore can't create any more terms).
class PlExceptionFail : public PlExceptionFailBase
{
public:
explicit PlExceptionFail() {}
virtual const char* what() const throw() override
{ return "PlExceptionFail";
}
};
// Check the return code; if there's a Prolog exception, throw
// PlException else return the rc. If the rc is FALSE (e.g., from
// PL_unify_*() or PL_next_solution(), that rc is returned; you might
// wish to wrap the call in PlCheckFail().
template<typename C_t> [[nodiscard]] C_t PlWrap(C_t rc, qid_t qid = 0);
// As PlWrap, but always throw an exception for zero rc.
// This is for functions that report errors but don't have an
// indication of "fail" - that is, almost everything except for
// functions like PL_unify_*() or PL_next_solution().
template<typename C_t> void PlEx(C_t rc, qid_t qid = 0);
// Check the return code: if "false", throw PlFail.
inline void PlCheckFail(bool rc);
#include "SWI-cpp2-plx.h"
/*******************************
* COMMON OPERATIONS (TEMPLATE) *
*******************************/
template <typename C_t> class WrappedC
{
private:
C_t C_ = null; // The wrapped value - access by unwrap(), unwrap_as_ptr(), or PlUnwrapAsPtr()
public:
static constexpr C_t null = C_t();
bool is_null() const { return unwrap() == null; }
bool not_null() const { return unwrap() != null; }
void set_null() { C_ = null; }
C_t unwrap() const { return C_; }
C_t *unwrap_as_ptr() { return &C_; }
friend C_t *PlUnwrapAsPtr(WrappedC<C_t>* obj) { return obj ? obj->unwrap_as_ptr() : nullptr; }
explicit WrappedC(C_t v)
: C_(v) { }
WrappedC( const WrappedC<C_t>&) = default;
WrappedC& operator =(const WrappedC<C_t>&) = default;
// This simple wrapper class doesn't need a move constructor or
// move operator =.
~WrappedC() { }
operator bool() const = delete; // Use not_null(), is_null() instead
bool operator ==(WrappedC<C_t> o) const { return C_ == o.C_; }
bool operator !=(WrappedC<C_t> o) const { return C_ != o.C_; }
// reset() is common with "smart pointers"; wrapped atom_t, term_t,
// etc. aren't "smart" in the same sense, but the objects they refer
// to are garbage collected by Prolog and some care is needed to
// ensure they have appropriate reference counts (e.g.,
// PlAtom::register_ref() and PlTerm::record()).
void reset() { C_ = null; } // same as set_null()
void reset(WrappedC<C_t> v) { C_ = v.unwrap(); }
void reset_wrapped(C_t v) { C_ = v; }
};
// TODO: use PlEncoding wherever a method takes a char* or std::string.
// TODO: #define SWI_DEFAULT_TEXT_ENCODING EncUTF8
// (set outside SWI-cpp2.h, with an appropriate default)
// For the various "get/put/unify string" methods:
typedef enum class PlEncoding
{ Latin1 = REP_ISO_LATIN_1,
UTF8 = REP_UTF8,
Locale = REP_MB
} PlEncoding;
static constexpr PlEncoding ENC_INPUT = PlEncoding::Latin1; // TODO: EncUTF8?
static constexpr PlEncoding ENC_OUTPUT = PlEncoding::Locale;
/*******************************
* PL_STRINGS_{MARK,RELEASE} *
*******************************/
class PlStringBuffers
{ // This class depends on the details of PL_STRINGS_MARK() and PL_STRINGS_RELEASE().
private:
buf_mark_t __PL_mark;
public:
explicit PlStringBuffers()
{ Plx_mark_string_buffers(&__PL_mark);
}
~PlStringBuffers()
{ Plx_release_string_buffers_from_mark(__PL_mark);
}
};
/*******************************
* PL_{acquire,release}_stream *
*******************************/
// TODO: document this.
// In brief, this is RAII for PL_{acquire,release_stream}.
// To use:
// PlAcquireStream strm(other_stream);
// Sfprintf(strm, ...);
class PlAcquireStream
{
public:
explicit PlAcquireStream(IOSTREAM *s)
: s_(Plx_acquire_stream(s))
{ PlEx<bool>(s_ != nullptr);
}
operator IOSTREAM *()
{ return s_;
}
// The following has an implicit throw of PlFail if
// PL_release_stream() detects an IO error had happened
~PlAcquireStream()
{ if ( s_ )
{ Plx_release_stream(s_);
}
}
private:
IOSTREAM *s_ = nullptr;
};
/*******************************
* PROLOG CONSTANTS *
*******************************/
class PlAtom : public WrappedC<atom_t>
{
public:
explicit PlAtom(atom_t v)
: WrappedC<atom_t>(v) { }
explicit PlAtom(const std::string& text)
: WrappedC<atom_t>(Plx_new_atom_nchars(text.size(), text.data()))
{ }
explicit PlAtom(const std::wstring& text)
: WrappedC<atom_t>(Plx_new_atom_wchars(text.size(), text.data()))
{ }
explicit PlAtom(const pl_wchar_t *text)
: WrappedC<atom_t>(Plx_new_atom_wchars(static_cast<size_t>(-1), text))
{ }
explicit PlAtom(const char *text)
: WrappedC<atom_t>(Plx_new_atom_nchars(static_cast<size_t>(-1), text))
{ }
explicit PlAtom(PlEncoding rep, size_t len, const char *s)
: WrappedC<atom_t>(Plx_new_atom_mbchars(static_cast<int>(rep), len, s))
{ }
explicit PlAtom(PlEncoding rep, std::string& text) // TODO: rep as optional with default ENC_INPUT
: WrappedC<atom_t>(Plx_new_atom_mbchars(static_cast<int>(rep), text.size(), text.data()))
{ }
const std::string mbchars(unsigned int flags) const;
const std::wstring wchars() const;
const std::string as_string(PlEncoding enc=ENC_OUTPUT) const
{ return mbchars(static_cast<unsigned int>(enc));
}
const std::wstring as_wstring() const
{ return wchars();
}
[[nodiscard]] int write(IOSTREAM *s, int flags) const;
// TODO: operator ==(PlAtom) is defined by WrappedC<C_t> but
// the following is needed to avoid amiguous overload:
bool operator ==(PlAtom to) const { return unwrap() == to.unwrap(); }
bool operator !=(PlAtom to) const { return unwrap() != to.unwrap(); }
[[deprecated("use as_string() and std::string::operator==() or ==PlAtom")]] bool operator ==(const char *s) const { return eq(s); }
[[deprecated("use as_string() and std::string::operator==() or ==PlAtom")]] bool operator ==(const wchar_t *s) const { return eq(s); }
[[deprecated("use as_string() and std::string::operator==() or ==PlAtom")]] bool operator ==(const std::string& s) const { return eq(s); }
[[deprecated("use as_swtring() and std::wstring::operator==() or ==PlAtom")]] bool operator ==(const std::wstring& s) const { return eq(s); }
[[deprecated("use PlAtom instead of atom_t")]] bool operator ==(atom_t to) const { return unwrap() == to; }
[[deprecated("use as_string() and std::string::operator!=() or !=PlAtom")]] bool operator !=(const char *s) const { return !eq(s); }
[[deprecated("use as_string() and std::string::operator!=() or !=PlAtom")]] bool operator !=(const wchar_t *s) const { return !eq(s); }
[[deprecated("use PlAtom instead of atom_t")]] bool operator !=(atom_t to) const { return unwrap() != to; }
// TODO: when C++17 becomes standard, rename register_ref() to register().
void register_ref() const
{ Plx_register_atom(unwrap());
}
void unregister_ref() const
{ Plx_unregister_atom(unwrap());
}
// TODO: replace blob_data() with C++ interface to blobs
void* blob_data(size_t *len, struct PL_blob_t **type) const
{ return Plx_blob_data(unwrap(), len, type);
}
private:
bool eq(const char *s) const // used by deprecated operator ==
{ PlStringBuffers _string_buffers;
return strcmp(s, Plx_atom_nchars(unwrap(), nullptr)) == 0;
}
bool eq(const wchar_t *s) const // used by deprecated operator ==
{ PlStringBuffers _string_buffers;
return wcscmp(s, Plx_atom_wchars(unwrap(), nullptr)) == 0;
}
bool eq(const std::string& s) const // used by deprecated operator ==
{ PlStringBuffers _string_buffers;
size_t len;
const char* s0 = Plx_atom_nchars(unwrap(), &len);
return std::string(s0, len) == s;
}
bool eq(const std::wstring& s) const // used by deprecated operator ==
{ PlStringBuffers _string_buffers;
size_t len;
const wchar_t* s0 = Plx_atom_wchars(unwrap(), &len);
return std::wstring(s0, len) == s;
}
};
class PlFunctor : public WrappedC<functor_t>
{
public:
explicit PlFunctor(functor_t v)
: WrappedC<functor_t>(v) { }
// PlFunctor(const char*) is handled by std::string constructor
// TODO: add encoding to string
explicit PlFunctor(const std::string& name, size_t arity)
: WrappedC<functor_t>(null)
{ PlAtom a(name);
reset_wrapped(Plx_new_functor(a.unwrap(), arity));
Plx_unregister_atom(a.unwrap());
}
explicit PlFunctor(const std::wstring& name, size_t arity)
: WrappedC<functor_t>(null)
{ PlAtom a(name);
reset_wrapped(Plx_new_functor(a.unwrap(), arity));
Plx_unregister_atom(a.unwrap());
}
explicit PlFunctor(PlAtom name, size_t arity)
: WrappedC<functor_t>(Plx_new_functor(name.unwrap(), arity)) { }
[[deprecated("use PlPredicate")]] predicate_t pred(module_t m) const {
predicate_t p = Plx_pred(unwrap(), m);
return p;
}
PlAtom name() const { return PlAtom(Plx_functor_name(unwrap())); }
size_t arity() const { return Plx_functor_arity(unwrap()); }
};
class PlModule : public WrappedC<module_t>
{
public:
explicit PlModule(module_t m = 0)
: WrappedC<module_t>(m) { }
explicit PlModule(const std::string& name)
: WrappedC<module_t>(Plx_new_module(PlAtom(name).unwrap()))
{ }
explicit PlModule(PlAtom name)
: WrappedC<module_t>(Plx_new_module(name.unwrap()))
{ }
PlAtom module_name() const
{ return PlAtom(Plx_module_name(unwrap()));
}
// TODO: strip_module
};
/*******************************
* GENERIC PROLOG TERM *
*******************************/
class PlTerm : public WrappedC<term_t>
{
protected:
explicit PlTerm()
: WrappedC<term_t>(Plx_new_term_ref())
{ }
public:
explicit PlTerm(PlAtom a)
: WrappedC<term_t>(Plx_new_term_ref())
{ Plx_put_atom(unwrap(), a.unwrap());
}
// The following constructor is the same as to PlTerm_term_t(); the
// latter is for consistency with other constructors
// (PlTerm_integer(), etc.) and the former is to make some template
// programming eaiser.
explicit PlTerm(term_t t)
: WrappedC<term_t>(t)
{ }
explicit PlTerm(const PlRecord& r);
PlTerm(const PlTerm&) = default;
// TODO: PlTerm& operator =(const PlTerm&) = delete; // TODO: when the deprecated items below are removed
[[nodiscard]] bool get_atom(PlAtom *a) const { return Plx_get_atom(unwrap(), PlUnwrapAsPtr(a)); }
[[nodiscard]] bool get_bool(int *value) const { return Plx_get_bool(unwrap(), value); }
[[deprecated("use get_chars(flags) returning std::string")]]
[[nodiscard]] bool get_chars(char **s, unsigned int flags) const { return Plx_get_chars(unwrap(), s, flags); }
[[nodiscard]] bool get_list_chars(char **s, unsigned int flags) const { return Plx_get_list_chars(unwrap(), s, flags); }
[[nodiscard]] bool get_atom_nchars(size_t *len, char **a) const { return Plx_get_atom_nchars(unwrap(), len, a); }
[[nodiscard]] bool get_list_nchars(size_t *len, char **s, unsigned int flags) const { return Plx_get_list_nchars(unwrap(), len, s, flags); }
[[deprecated("use get_nchars(flags) returning std::string")]]
[[nodiscard]] bool get_nchars(size_t *len, char **s, unsigned int flags) const { return _get_nchars(len, s, flags); }
const std::string get_nchars(unsigned int flags) const;
[[deprecated("use get_wchars(flags) returning std::wstring")]]
[[nodiscard]] bool get_wchars(size_t *length, pl_wchar_t **s, unsigned flags) const { return _get_wchars(length, s, flags); }
const std::wstring get_wchars(unsigned int flags) const;
[[nodiscard]] bool get_integer(int *i) const { return Plx_get_integer(unwrap(), i); }
[[nodiscard]] bool get_long(long *i) const { return Plx_get_long(unwrap(), i); }
[[nodiscard]] bool get_intptr(intptr_t *i) const { return Plx_get_intptr(unwrap(), i); }
[[nodiscard]] bool get_pointer(void **ptr) const { return Plx_get_pointer(unwrap(), ptr); }
[[nodiscard]] bool get_float(double *f) const { return Plx_get_float(unwrap(), f); }
[[nodiscard]] bool get_functor(PlFunctor *f) const { return Plx_get_functor(unwrap(), PlUnwrapAsPtr(f)); }
[[nodiscard]] bool get_name_arity(PlAtom *name, size_t *arity) const { return Plx_get_name_arity(unwrap(), PlUnwrapAsPtr(name), arity); }
[[nodiscard]] bool get_compound_name_arity(PlAtom *name, size_t *arity) const { return Plx_get_compound_name_arity(unwrap(), PlUnwrapAsPtr(name), arity); }
[[nodiscard]] bool get_module(PlModule *module) const { return Plx_get_module(unwrap(), PlUnwrapAsPtr(module)); }
[[nodiscard]] bool get_arg(size_t index, PlTerm a) const { return Plx_get_arg(index, unwrap(), a.unwrap()); }
[[nodiscard]] bool get_dict_key(PlAtom key, PlTerm dict, PlTerm value) const { return Plx_get_dict_key(key.unwrap(), dict.unwrap(), value.unwrap()); }
[[nodiscard]] bool get_list(PlTerm h, PlTerm t) const { return Plx_get_list(unwrap(), h.unwrap(), t.unwrap()); }
[[nodiscard]] bool get_head(PlTerm h) const { return Plx_get_head(unwrap(), h.unwrap()); }
[[nodiscard]] bool get_tail(PlTerm t) const { return Plx_get_tail(unwrap(), t.unwrap()); }
// TODO: get_mpz
// TODO: get_mpq
[[nodiscard]] bool get_nil() const { return Plx_get_nil(unwrap()); }
[[nodiscard]] bool get_blob(void **blob, size_t *len, PL_blob_t **type) const { return Plx_get_blob(unwrap(), blob, len, type); }
[[nodiscard]] bool get_file_name(char **name, int flags) const { return Plx_get_file_name(unwrap(), name, flags); }
[[nodiscard]] bool get_file_nameW(wchar_t **name, int flags) const { return Plx_get_file_nameW(unwrap(), name, flags); }
[[nodiscard]] const std::string get_file_name(int flags) const;
[[nodiscard]] const std::wstring get_file_nameW(int flags) const;
[[nodiscard]] bool get_attr(term_t a) const { return Plx_get_attr(unwrap(), a); }
void get_atom_ex(PlAtom *a) const { Plx_get_atom_ex(unwrap(), PlUnwrapAsPtr(a)); }
void get_integer_ex(int *i) const { Plx_get_integer_ex(unwrap(),i); }
void get_long_ex(long *i) const { Plx_get_long_ex(unwrap(), i); }
void get_int64_ex(int64_t *i) const { Plx_get_int64_ex(unwrap(), i); }
void get_uint64_ex(uint64_t *i) const { Plx_get_uint64_ex(unwrap(), i); }
void get_intptr_ex(intptr_t *i) const { Plx_get_intptr_ex(unwrap(), i); }
void get_size_ex(size_t *i) const { Plx_get_size_ex(unwrap(), i); }
void get_bool_ex(int *i) const { Plx_get_bool_ex(unwrap(), i); }
void get_float_ex(double *f) const { Plx_get_float_ex(unwrap(), f); }
void get_char_ex(int *p, int eof) const { Plx_get_char_ex(unwrap(), p, eof); }
void unify_bool_ex(int val) const { Plx_unify_bool_ex(unwrap(), val); }
void get_pointer_ex(void **addrp) const { Plx_get_pointer_ex(unwrap(), addrp); }
bool unify_list_ex(PlTerm h, PlTerm t) const { return Plx_unify_list_ex(unwrap(), h.unwrap(), t.unwrap()); }
void unify_nil_ex() const { Plx_unify_nil_ex(unwrap()); }
bool get_list_ex(PlTerm h, PlTerm t) const { return Plx_get_list_ex(unwrap(), h.unwrap(), t.unwrap()); }
void get_nil_ex() const { Plx_get_nil_ex(unwrap()); }
int type() const { return Plx_term_type(unwrap()); } // PL_VARIABLE, PL_ATOM, etc.
bool is_attvar() const { return Plx_is_attvar(unwrap()); }
bool is_variable() const { return Plx_is_variable(unwrap()); }
bool is_ground() const { return Plx_is_ground(unwrap()); }
bool is_atom() const { return Plx_is_atom(unwrap()); }
bool is_integer() const { return Plx_is_integer(unwrap()); }
bool is_string() const { return Plx_is_string(unwrap()); }
bool is_atom_or_string() const
{ int t = type();
return t == PL_ATOM || t == PL_STRING;
}
bool is_float() const { return Plx_is_float(unwrap()); }
bool is_rational() const { return Plx_is_rational(unwrap()); }
bool is_compound() const { return Plx_is_compound(unwrap()); }
bool is_callable() const { return Plx_is_callable(unwrap()); }
bool is_list() const { return Plx_is_list(unwrap()); }
bool is_dict() const { return Plx_is_dict(unwrap()); }
bool is_pair() const { return Plx_is_pair(unwrap()); }
bool is_atomic() const { return Plx_is_atomic(unwrap()); }
bool is_number() const { return Plx_is_number(unwrap()); }
bool is_acyclic() const { return Plx_is_acyclic(unwrap()); }
bool is_functor(PlFunctor f) const { return Plx_is_functor(unwrap(), f.unwrap()); }
bool is_blob(PL_blob_t **type) const { return Plx_is_blob(unwrap(), type); }
void must_be_attvar() const;
void must_be_variable() const;
void must_be_ground() const;
void must_be_atom() const;
void must_be_integer() const;
void must_be_string() const;
void must_be_atom_or_string() const;
void must_be_float() const;
void must_be_rational() const;
void must_be_compound() const;
void must_be_callable() const;
void must_be_list() const;
void must_be_dict() const;
void must_be_pair() const;
void must_be_atomic() const;
void must_be_number() const;
void must_be_acyclic() const;
// TODO: if needed
// void must_be_functor(PlFunctor f) const;
// void must_be_blob(PL_blob_t **type) const;
void put_variable() { Plx_put_variable(unwrap()); }
void put_atom(PlAtom a) { Plx_put_atom(unwrap(), a.unwrap()); }
void put_bool(int val) { Plx_put_bool(unwrap(), val); }
void put_atom_chars(const char *chars) { Plx_put_atom_chars(unwrap(), chars); }
void put_string_chars(const char *chars) { Plx_put_string_chars(unwrap(), chars); }
void put_chars(int flags, size_t len, const char *chars) { Plx_put_chars(unwrap(), flags, len, chars); }
void put_list_chars(const char *chars) { Plx_put_list_chars(unwrap(), chars); }
void put_list_codes(const char *chars) { Plx_put_list_codes(unwrap(), chars); }
// TODO: add std::string versions of the following
void put_atom_nchars(size_t l, const char *chars) { Plx_put_atom_nchars(unwrap(), l, chars); }
void put_string_nchars(size_t len, const char *chars) { Plx_put_string_nchars(unwrap(), len, chars); }
void put_list_nchars(size_t l, const char *chars) { Plx_put_list_nchars(unwrap(), l, chars); }
void put_list_ncodes(size_t l, const char *chars) { Plx_put_list_ncodes(unwrap(), l, chars); }
void put_integer(long i) { Plx_put_integer(unwrap(), i); }
void put_int64(int64_t i) { Plx_put_int64(unwrap(), i); }
void put_uint64(uint64_t i) { Plx_put_uint64(unwrap(), i); }
void put_pointer(void *ptr) { Plx_put_pointer(unwrap(), ptr); }
void put_float(double f) { Plx_put_float(unwrap(), f); }
void put_functor(PlFunctor functor) { Plx_put_functor(unwrap(), functor.unwrap()); }
void put_list() { Plx_put_list(unwrap()); }
void put_nil() { Plx_put_nil(unwrap()); }
void put_term(PlTerm t2) { Plx_put_term(unwrap(), t2.unwrap()); }
void put_blob( void *blob, size_t len, PL_blob_t *type) { Plx_put_blob(unwrap(), blob, len, type); }
PlRecord record() const;
// TODO: When the following are implemented, add them
// to the deleted methods in PlTermScoped:
// TODO: PL_put_dict(term_t t, atom_t tag, size_t len, const atom_t *keys, term_t values)
// TODO: PL_cons_functor(term_t h, functor_t f, ...)
// TODO: PL_cons_functor_v(term_t h, functor_t fd, term_t a0)
// TODO: PL_cons_list(term_t l, term_t h, term_t t)
// TODO: PL_unify_*()?
// TODO: PL_skip_list()
/* PlTerm --> C */
[[deprecated("use as_long()")]] explicit operator long() const { return as_long(); }
[[deprecated("use as_int()")]] explicit operator int() const { return as_int(); }
[[deprecated("use as_uint32_t()")]] explicit operator uint32_t() const { return as_uint32_t(); }
[[deprecated("use as_uint64_t()")]] explicit operator uint64_t() const { return as_uint64_t(); }
[[deprecated("use as_float()")]] explicit operator double() const { return as_float(); }
[[deprecated("use as_pointer()")]] explicit operator void *() const { return as_pointer(); }
[[deprecated("use as_atom()")]] explicit operator PlAtom() const { return as_atom(); }
// No need for overloading int64_t, size_t, etc.; these are defined
// by the compiler in terms of one of the types below.
// TODO: add wchar_t, char16_t, char32_t
void integer(bool *v) const { int v0; Plx_cvt_i_bool(unwrap(), &v0); *v = v0; }
void integer(char *v) const { Plx_cvt_i_char( unwrap(), v); }
void integer(int *v) const { Plx_cvt_i_int( unwrap(), v); }
void integer(long *v) const { Plx_cvt_i_long( unwrap(), v); }
void integer(long long *v) const { Plx_cvt_i_llong( unwrap(), v); }
void integer(short *v) const { Plx_cvt_i_short( unwrap(), v); }
void integer(signed char *v) const { Plx_cvt_i_schar( unwrap(), v); }
void integer(unsigned char *v) const { Plx_cvt_i_uchar( unwrap(), v); }
void integer(unsigned int *v) const { Plx_cvt_i_uint( unwrap(), v); }
void integer(unsigned long *v) const { Plx_cvt_i_ulong( unwrap(), v); }
void integer(unsigned long long *v) const { Plx_cvt_i_ullong(unwrap(), v); }
void integer(unsigned short *v) const { Plx_cvt_i_ushort(unwrap(), v); }
// All the conversion functions throw a PlTypeError exception if
// they fail (because of the wrong Prolog type). If you want to be
// safe, use is_XXX() first to verify the type.
const std::string as_string(PlEncoding enc=ENC_OUTPUT) const
{ return get_nchars(CVT_ALL|CVT_WRITEQ|static_cast<unsigned int>(enc));
}
const std::wstring as_wstring() const
{ return get_wchars(CVT_ALL|CVT_WRITEQ);
}
long as_long() const { long v; integer(&v); return v; }
int32_t as_int32_t() const { int32_t v; integer(&v); return v; }
uint32_t as_uint32_t() const { uint32_t v; integer(&v); return v; }
uint64_t as_uint64_t() const { uint64_t v; integer(&v); return v; }
int64_t as_int64_t() const { int64_t v; integer(&v); return v; }
size_t as_size_t() const { size_t v; integer(&v); return v; }
int as_int() const { int v; integer(&v); return v; }
unsigned as_uint() const { unsigned v; integer(&v); return v; }
unsigned long as_ulong() const { unsigned long v; integer(&v); return v; }
bool as_bool() const { bool v; integer(&v); return v; }
void as_nil() const;
double as_float() const;
double as_double() const { return as_float(); }
void * as_pointer() const; // TODO: replace with C++ interface to blobs
// TODO: PL_get_mpz(), PL_getr_mpq()
PlAtom as_atom() const;
[[nodiscard]] bool eq_if_atom(PlAtom a) const;
/* Compounds */
PlTerm operator [](size_t index) const;
size_t arity() const; // throws PlTypeError if not a "compound" or atom
PlAtom name() const; // throws PlTypeError if not a "compound" or atom
[[nodiscard]] bool name_arity(PlAtom *name, size_t *arity) const; // name and/or arity can be nullptr
[[nodiscard]] PlTerm copy_term_ref() const;
void free_term_ref();
void free_term_ref_reset();
// The assignment operators from version 1 have been removed because
// of possible confusion with the standard assignment and copy
// operators. Also, they have unusual semantics; normally an
// assignment operator would have the form
// PlTerm& PlTerm::operator =(const PlTerm&)
// with implicit or explicit cast from, e.g. PlAtom to PlTerm
/* UNIFY */
[[deprecated("use unify_*()")]] [[nodiscard]] bool operator =(PlTerm t2) const { return unify_term(t2); }
[[deprecated("use unify_*()")]] [[nodiscard]] bool operator =(PlAtom a) const { return unify_atom(a); }
[[deprecated("use unify_*()")]] [[nodiscard]] bool operator =(const char *v) const { return unify_atom(v); }
[[deprecated("use unify_*()")]] [[nodiscard]] bool operator =(const wchar_t *v) const { return unify_atom(v); }
[[deprecated("use unify_*()")]] [[nodiscard]] bool operator =(intptr_t v) const { return unify_integer(v); }
[[deprecated("use unify_*()")]] [[nodiscard]] bool operator =(double v) const { return unify_float(v); }
[[deprecated("use unify_*()")]] [[nodiscard]] bool operator =(PlFunctor f) const { return unify_functor(f); }
// All the unify_*() methods check for an exception (and throw), so
// the return code is whether the unification succeeded or not.
// TODO: replace PL_unify_*() with PL_unify_string() and flags, where appropriate
// TODO: encodings for char*, std::string
[[nodiscard]] bool unify_term(PlTerm t2) const { return Plx_unify(unwrap(), t2.unwrap()); }
[[nodiscard]] bool unify_atom(PlAtom a) const { return Plx_unify_atom(unwrap(), a.unwrap()); }
[[nodiscard]] bool unify_chars(int flags, size_t len, const char *s) const { return Plx_unify_chars(unwrap(), flags, len, s); }
[[nodiscard]] bool unify_chars(int flags, const std::string& s) const { return Plx_unify_chars(unwrap(), flags, s.size(), s.data()); }
[[nodiscard]] bool unify_atom(const char* v) const { return Plx_unify_atom_chars(unwrap(), v); }
[[nodiscard]] bool unify_atom(const wchar_t* v) const { return Plx_unify_wchars(unwrap(), PL_ATOM, static_cast<size_t>(-1), v); }
[[nodiscard]] bool unify_atom(const std::string& v) const { return Plx_unify_atom_nchars(unwrap(), v.size(), v.data()); }
[[nodiscard]] bool unify_atom(const std::wstring& v) const { return Plx_unify_wchars(unwrap(), PL_ATOM, v.size(), v.data()); }
[[nodiscard]] bool unify_list_codes(const char* v) const { return Plx_unify_list_codes(unwrap(), v); } // TODO: [[deprecated]]
[[nodiscard]] bool unify_list_chars(const char* v) const { return Plx_unify_list_chars(unwrap(), v); } // TODO: [[deprecated]]
// See comment with PlTerm::integer() about the overloading.
[[nodiscard]] bool unify_integer(bool v) const { return Plx_unify_int64(unwrap(), v); }
[[nodiscard]] bool unify_integer(char v) const { return Plx_unify_int64(unwrap(), v); }
[[nodiscard]] bool unify_integer(int v) const { return Plx_unify_int64(unwrap(), v); }
[[nodiscard]] bool unify_integer(long v) const { return Plx_unify_int64(unwrap(), v); }
[[nodiscard]] bool unify_integer(long long v) const { return Plx_unify_int64(unwrap(), v); }
[[nodiscard]] bool unify_integer(short v) const { return Plx_unify_int64(unwrap(), v); }
[[nodiscard]] bool unify_integer(signed char v) const { return Plx_unify_int64(unwrap(), v); }
[[nodiscard]] bool unify_integer(unsigned char v) const { return Plx_unify_uint64(unwrap(), v); }
[[nodiscard]] bool unify_integer(unsigned int v) const { return Plx_unify_uint64(unwrap(), v); }
[[nodiscard]] bool unify_integer(unsigned long v) const { return Plx_unify_uint64(unwrap(), v); }
[[nodiscard]] bool unify_integer(unsigned long long v) const { return Plx_unify_uint64(unwrap(), v); }
[[nodiscard]] bool unify_integer(unsigned short v) const { return Plx_unify_uint64(unwrap(), v); }
[[nodiscard]] bool unify_float(double v) const { return Plx_unify_float(unwrap(), v); }
[[nodiscard]] bool unify_string(const std::string& v) const { return Plx_unify_string_nchars(unwrap(), v.size(), v.data()); }
[[nodiscard]] bool unify_wstring(const std::wstring& v) const { return Plx_unify_wchars(unwrap(), PL_STRING, v.size(), v.data()); }
[[nodiscard]] bool unify_functor(PlFunctor f) const { return Plx_unify_functor(unwrap(), f.unwrap()); }
[[nodiscard]] bool unify_pointer(void *ptr) const { return Plx_unify_pointer(unwrap(), ptr); } // TODO: replace with C++ interface to blobs
[[nodiscard]] bool unify_nil() const { return Plx_unify_nil(unwrap()); }
[[nodiscard]] bool unify_list(PlTerm h, PlTerm t) const { return Plx_unify_list(unwrap(), h.unwrap(), t.unwrap()); }
[[nodiscard]] bool unify_bool(bool val) const { return Plx_unify_bool(unwrap(), val); }
[[nodiscard]] bool unify_blob(const PlBlob* blob) const;
[[nodiscard]] bool unify_blob(std::unique_ptr<PlBlob>* blob) const;
[[nodiscard]] bool unify_blob(const void *blob, size_t len, const PL_blob_t *type) const
{ return Plx_unify_blob(unwrap(), const_cast<void*>(blob), len, const_cast<PL_blob_t*>(type)); }
// TODO: PL_unify_mpz(), PL_unify_mpq()
/* Comparison standard order terms */
[[nodiscard]] int compare(PlTerm t2) const { return Plx_compare(unwrap(), t2.unwrap()); }
bool operator ==(PlTerm t2) const { return compare(t2) == 0; }
bool operator !=(PlTerm t2) const { return compare(t2) != 0; }
bool operator < (PlTerm t2) const { return compare(t2) < 0; }
bool operator > (PlTerm t2) const { return compare(t2) > 0; }
bool operator <=(PlTerm t2) const { return compare(t2) <= 0; }
bool operator >=(PlTerm t2) const { return compare(t2) >= 0; }
/* comparison (long) */
[[deprecated("use as_int64_t() and int64_t::operator==()")]] bool operator ==(int64_t v) const;
[[deprecated("use as_int64_t() and int64_t::operator!=()")]] bool operator !=(int64_t v) const;
[[deprecated("use as_int64_t() and int64_t::operator<()")]] bool operator < (int64_t v) const;
[[deprecated("use as_int64_t() and int64_t::operator>()")]] bool operator > (int64_t v) const;
[[deprecated("use as_int64_t() and int64_t::operator<=()")]] bool operator <=(int64_t v) const;
[[deprecated("use as_int64_t() and int64_t::operator>=()")]] bool operator >=(int64_t v) const;
/* comparison (atom, string) */
[[deprecated("use as_string() and std::string::operator==()")]] bool operator ==(const char *s) const { return eq(s); }
[[deprecated("use as_string() and std::string::operator==()")]] bool operator ==(const wchar_t *s) const { return eq(s); }
[[deprecated("use as_wstring() and std::string::operator==()")]] bool operator ==(const std::string& s) const { return eq(s); }
[[deprecated("use as_wstring() and std::wstring::operator==()")]] bool operator ==(const std::wstring& s) const { return eq(s); }
[[deprecated("use as_atom()")]] bool operator ==(PlAtom a) const { return eq(a); }
[[deprecated("use as_string() and std::string::operator==()")]] bool operator !=(const char *s) const { return !eq(s); }
[[deprecated("use as_wstring() and std::wstring::operator==()")]] bool operator !=(const wchar_t *s) const { return !eq(s); }
[[deprecated("use as_string() and std::string::operator==()")]] bool operator !=(const std::string& s) const { return !eq(s); }
[[deprecated("use as_wstring() and std::wstring::operator==()")]] bool operator !=(const std::wstring& s) const { return !eq(s); }
[[deprecated("use as_atom()")]] bool operator !=(PlAtom a) const { return !eq(a); }
// E.g.: t.write(Serror, 1200, PL_WRT_QUOTED);
[[nodiscard]] int write(IOSTREAM *s, int precedence, int flags) const
{ // TODO: move "&~PL_WRITE_NEWLINE" to PL_write_term() and update foreign.doc
return Plx_write_term(s, unwrap(), precedence, flags & ~PL_WRT_NEWLINE);
}
void reset_term_refs() { Plx_reset_term_refs(unwrap()); }
bool call(PlModule module = PlModule()) const { return Plx_call(unwrap(), module.unwrap()); }
private:
bool eq(const char *s) const;
bool eq(const wchar_t *s) const;
bool eq(const std::string& s) const;
bool eq(const std::wstring& s) const;
bool eq(PlAtom a) const;
// deprecated get_chars get_nchars(), get_wchars() are used internally (and safely):
[[nodiscard]] bool _get_chars(char **s, unsigned int flags) const { return Plx_get_chars(unwrap(), s, flags); }
[[nodiscard]] bool _get_nchars(size_t *len, char **s, unsigned int flags) const { return Plx_get_nchars(unwrap(), len, s, flags); }
[[nodiscard]] bool _get_wchars(size_t *length, pl_wchar_t **s, unsigned flags) const { return Plx_get_wchars(unwrap(), length, s, flags); }
};
// PlTermScoped is an *experimental* inteface, which may change
// in the future. It implements a PlTerm that is automatically
// freed when it goes out of scope. The API is similar to
// std::unique_ptr.
class PlTermScoped : public PlTerm
{
public:
explicit PlTermScoped()
: PlTerm()
{ }
explicit PlTermScoped(PlTerm t)
: PlTerm(t.copy_term_ref())
{ }
explicit PlTermScoped(term_t t)
: PlTerm(Plx_copy_term_ref(t))
{ }
explicit PlTermScoped(PlTermScoped&& moving) noexcept
: PlTerm(moving)
{ moving.reset();
}
PlTermScoped& operator=(PlTermScoped&& moving) noexcept
{ if ( this != &moving )
{ reset(moving);
moving.reset();
}
return *this;
}
PlTermScoped(const PlTermScoped&) = delete;
PlTermScoped& operator=(PlTermScoped const&) = delete;
~PlTermScoped()
{ free_term_ref_reset(); // TODO: reset() isn't needed?
}
void reset()
{ free_term_ref();
PlTerm::reset();
}
void reset(PlTerm src)
{ free_term_ref();
PlTerm::reset(src);
}
PlTerm get() const noexcept { return PlTerm(unwrap()); }
PlTermScoped release() noexcept
{ term_t t = unwrap();
reset();
return PlTermScoped(t);
}
void swap(PlTermScoped& src) noexcept
{ // std::swap(*this, src);
term_t this_unwrap = unwrap();
// Must use PlTerm::reset() to avoid call to PL_free_term_ref():
static_cast<PlTerm*>(this)->reset(PlTerm(src.unwrap()));
static_cast<PlTerm>(src).reset(PlTerm(this_unwrap));
}
// The put_*() and cons_*() methods are incompatible with the
// call to PL_free_term_ref() in the destructor.
void put_variable() = delete;
void put_atom(PlAtom a) = delete;
void put_bool(int val) = delete;
void put_atom_chars(const char *chars) = delete;
void put_string_chars(const char *chars) = delete;
void put_chars(int flags, size_t len, const char *chars) = delete;
void put_list_chars(const char *chars) = delete;
void put_list_codes(const char *chars) = delete;
void put_atom_nchars(size_t l, const char *chars) = delete;
void put_string_nchars(size_t len, const char *chars) = delete;
void put_list_nchars(size_t l, const char *chars) = delete;
void put_list_ncodes(size_t l, const char *chars) = delete;
void put_integer(long i) = delete;
void put_pointer(void *ptr) = delete;
void put_float(double f) = delete;
void put_functor(PlFunctor functor) = delete;
void put_list() = delete;
void put_nil() = delete;
void put_term(PlTerm t2) = delete;
void put_blob( void *blob, size_t len, PL_blob_t *type) = delete;
};
// TODO: verify that this is the right way to specialize
// std::swap()'s use with std containers:
namespace std
{
inline
void swap(PlTermScoped& lhs, PlTermScoped& rhs) noexcept
{ lhs.swap(rhs);
}
}
class PlTerm_atom : public PlTerm
{
public:
// TODO: Add encoding for char*, std::string.
// For now, these are safe only with ASCII (PlEncoding::Latin1):
explicit PlTerm_atom(atom_t a) { Plx_put_atom(unwrap(), a); }
explicit PlTerm_atom(PlAtom a) { Plx_put_atom(unwrap(), a.unwrap()); }
explicit PlTerm_atom(const char *text) { Plx_put_atom_chars(unwrap(), text); } // TODO: add encoding
explicit PlTerm_atom(const wchar_t *text) { PlEx<bool>(Plx_unify_wchars(unwrap(), PL_ATOM, static_cast<size_t>(-1), text)); }
explicit PlTerm_atom(const std::string& text) { Plx_put_atom_nchars(unwrap(), text.size(), text.data()); } // TODO: add encoding
explicit PlTerm_atom(const std::wstring& text) { PlEx<int>(Plx_unify_wchars(unwrap(), PL_ATOM, text.size(), text.data())); }
};
[[nodiscard]]
inline int
PlAtom::write(IOSTREAM *s, int flags) const {
// TODO: move "&~PL_WRITE_NEWLINE" to PL_write_term() and update foreign.doc
return PlTerm_atom(*this).write(s, 1200, flags & ~PL_WRT_NEWLINE);
}
class PlTerm_var : public PlTerm
{
public:
explicit PlTerm_var() { } // PlTerm() calls Pl_new_term_ref()
};
class PlTerm_term_t : public PlTerm
{
public:
// TODO: [[deprecated("use PlTerm(Plterm::null)")]]
explicit PlTerm_term_t(term_t t)
: PlTerm(t) {}
};
class PlTerm_integer : public PlTerm
{
public:
// See comment with PlTerm::integer() about the overloading.
explicit PlTerm_integer(char v) { Plx_put_int64(unwrap(), v); }
explicit PlTerm_integer(int v) { Plx_put_int64(unwrap(), v); }
explicit PlTerm_integer(long v) { Plx_put_int64(unwrap(), v); }
explicit PlTerm_integer(long long v) { Plx_put_int64(unwrap(), v); }
explicit PlTerm_integer(short v) { Plx_put_int64(unwrap(), v); }
explicit PlTerm_integer(signed char v) { Plx_put_int64(unwrap(), v); }
explicit PlTerm_integer(unsigned char v) { Plx_put_uint64(unwrap(), v); }
explicit PlTerm_integer(unsigned int v) { Plx_put_uint64(unwrap(), v); }
explicit PlTerm_integer(unsigned long v) { Plx_put_uint64(unwrap(), v); }
explicit PlTerm_integer(unsigned long long v) { Plx_put_uint64(unwrap(), v); }
explicit PlTerm_integer(unsigned short v) { Plx_put_uint64(unwrap(), v); }
};
class PlTerm_float : public PlTerm
{
public:
explicit PlTerm_float(double v) { Plx_put_float(unwrap(), v); }
};
// TODO: deprecate PlTerm_pointer and replace by C++ interface to blobs
// (see also PlAtom::blob_data(), PlTerm::as_pointer())
class PlTerm_pointer : public PlTerm
{
public:
explicit PlTerm_pointer(void * ptr) { Plx_put_pointer(unwrap(), ptr); }
};
inline PlModule PlContext();
class PlPredicate : public WrappedC<predicate_t>
{
public:
explicit PlPredicate(predicate_t p)
: WrappedC<predicate_t>(p) { }
explicit PlPredicate(PlFunctor f)
: WrappedC<predicate_t>(Plx_pred(f.unwrap(), static_cast<module_t>(PlModule::null)))
{ }
explicit PlPredicate(PlFunctor f, PlModule m)
: WrappedC<predicate_t>(Plx_pred(f.unwrap(), m.unwrap()))
{ }
explicit PlPredicate(const char *name, int arity, const char *module)
: WrappedC<predicate_t>(Plx_predicate(name, arity, module))
{ }
explicit PlPredicate(const std::string& name, int arity, const std::string& module)
: WrappedC<predicate_t>(Plx_predicate(name.c_str(), arity, module.c_str()))
{ }
void predicate_info(PlAtom *name, size_t *arity, PlModule *module)
{ atom_t n;
module_t m;
Plx_predicate_info(unwrap(), &n, arity, &m);
*name = PlAtom(n);
*module = PlModule(m);
}
};
/*******************************
* TERM VECTOR *
*******************************/
class PlTermv
{
private:
size_t size_;
term_t a0_; // A vector of term_t
public:
explicit PlTermv(size_t n = 0)
: size_(n),
a0_(n ? Plx_new_term_refs(n) : PlTerm::null)
{ if ( size_ )
PlEx<bool>(a0_ != (term_t)0);
}
explicit PlTermv(size_t n, PlTerm t0)
: size_(n),
a0_(t0.unwrap())
{ // Assume that t0 is valid - it can be if 0 if PREDICATE_NONDET is
// called for PL_PRUNED
}
PlTermv(const PlTermv&) = default;
PlTermv& operator =(const PlTermv&) = default;
~PlTermv() = default;
term_t termv() const
{ // Note that a0_ can be PlTerm::null if size_ == 0
return a0_;
}
size_t size() const
{ return size_;
}