-
Notifications
You must be signed in to change notification settings - Fork 14
/
SWI-cpp2.cpp
1095 lines (928 loc) · 29.2 KB
/
SWI-cpp2.cpp
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: [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.
*/
/* If you wish, you can append SWI-cpp2.cpp file to SWI-pp2.h ...
to do this, you need this definition:
#define _SWI_CPP2_CPP_inline inline
*/
#ifndef _SWI_CPP2_CPP
#define _SWI_CPP2_CPP
#ifndef _SWI_CPP2_CPP_inline
#define _SWI_CPP2_CPP_inline
#endif
#include "SWI-cpp2.h"
_SWI_CPP2_CPP_inline
static
bool ex_is_resource_error(PlTerm ex)
{ // TODO: move the static PlFunctor to outside this function: https://github.com/SWI-Prolog/swipl-devel/issues/1155
static PlFunctor FUNCTOR_error_2("error", 2);
static PlFunctor FUNCTOR_resource_error_1("resource_error", 1);
// The following doesn't check details of the resource error; if desired
// these can be added by ex[1][1].unify_atom(ATOM_stack), ATOM_memory, etc
return ( ex.is_functor(FUNCTOR_error_2) &&
ex[1].is_functor(FUNCTOR_resource_error_1) );
}
_SWI_CPP2_CPP_inline
void
PlWrap_fail(qid_t qid)
{ PlTerm ex(PL_exception(qid));
if ( ex.not_null() )
{ // The error(resource_error(stack), _) exception is special because
// nothing can be put on the stack, so all we can do is report failure
// to the Prolog engine, which will take care of things.
// This means, of course, that a user catch(PlException&) won't catch
// this particular exception.
if ( ex_is_resource_error(ex) )
throw PlExceptionFail();
const PlException ex2(ex);
Plx_clear_exception(); // See https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/66
throw ex2;
}
}
_SWI_CPP2_CPP_inline
void
PlEx_fail(qid_t qid)
{ PlTerm ex(PL_exception(qid));
if ( ex.not_null() )
{ // The error(resource_error(stack), _) exception is special because
// nothing can be put on the stack, so all we can do is report failure
// to the Prolog engine, which will take care of things.
// This means, of course, that a user catch(PlException&) won't catch
// this particular exception.
if ( ex_is_resource_error(ex) )
throw PlExceptionFail();
const PlException ex2(ex);
Plx_clear_exception(); // See https://swi-prolog.discourse.group/t/cpp2-exceptions/6040/66
throw ex2;
} else
{ // TODO: get the name of the PL_...() function that caused the problem:
// assert(0);
throw PlUnknownError("False return code without exception");
}
}
// TODO: add unit test for unrepresentable string - should throw error
_SWI_CPP2_CPP_inline
const std::string
PlTerm::get_nchars(unsigned int flags) const
{ flags &= ~static_cast<unsigned int>(BUF_STACK|BUF_MALLOC|BUF_ALLOW_STACK);
flags |= static_cast<unsigned int>(BUF_DISCARDABLE|CVT_EXCEPTION);
char *s = nullptr;
size_t len = 0;
PlStringBuffers _string_buffers;
if ( _get_nchars(&len, &s, flags) )
return std::string(s, len);
PlEx_fail();
return "<---get_nchars error --->"; // Should never get here
}
_SWI_CPP2_CPP_inline
const std::wstring
PlTerm::get_wchars(unsigned int flags) const
{ flags &= ~static_cast<unsigned int>(BUF_STACK|BUF_MALLOC|BUF_ALLOW_STACK);
flags |= static_cast<unsigned int>(BUF_DISCARDABLE|CVT_EXCEPTION);
pl_wchar_t *s;
size_t len;
PlStringBuffers _string_buffers;
if ( _get_wchars(&len, &s, flags) )
return std::wstring(s, len);
PlEx_fail();
return L"<---get_wchars error --->"; // Should never get here
}
_SWI_CPP2_CPP_inline
const std::string
PlTerm::get_file_name(int flags) const
{ char *name = const_cast<char*>("");
PlCheckFail(get_file_name(&name, flags));
return name;
}
_SWI_CPP2_CPP_inline
const std::wstring
PlTerm::get_file_nameW(int flags) const
{ wchar_t *name = const_cast<wchar_t*>(L"");
PlCheckFail(get_file_nameW(&name, flags));
return name;
}
#define _MUST_BE_TYPE(must_be_name, is_test, type_name) \
_SWI_CPP2_CPP_inline \
void \
PlTerm::must_be_name() const \
{ if ( !is_test() ) \
throw PlTypeError(type_name, *this); \
}
_MUST_BE_TYPE(must_be_attvar, is_attvar, "attvar")
_MUST_BE_TYPE(must_be_variable, is_variable, "variable")
_MUST_BE_TYPE(must_be_ground, is_ground, "ground")
_MUST_BE_TYPE(must_be_atom, is_atom, "atom")
_MUST_BE_TYPE(must_be_integer, is_integer, "integer")
_MUST_BE_TYPE(must_be_string, is_string, "string")
_MUST_BE_TYPE(must_be_atom_or_string, is_atom_or_string, "atom or string")
_MUST_BE_TYPE(must_be_float, is_float, "float")
_MUST_BE_TYPE(must_be_rational, is_rational, "rational")
_MUST_BE_TYPE(must_be_compound, is_compound, "compound")
_MUST_BE_TYPE(must_be_callable, is_callable, "callable")
_MUST_BE_TYPE(must_be_list, is_list, "list")
_MUST_BE_TYPE(must_be_dict, is_dict, "dict")
_MUST_BE_TYPE(must_be_pair, is_pair, "pair")
_MUST_BE_TYPE(must_be_atomic, is_atomic, "atomic")
_MUST_BE_TYPE(must_be_number, is_number, "number")
_MUST_BE_TYPE(must_be_acyclic, is_acyclic, "acyclic")
#undef _MUST_BE_TYPE
_SWI_CPP2_CPP_inline
PlModule
PlContext()
{ return PlModule(Plx_context());
}
_SWI_CPP2_CPP_inline
PlException
PlGeneralError(PlTerm inside)
{ return PlException(PlCompound("error", PlTermv(inside, PlTerm_var())));
}
_SWI_CPP2_CPP_inline
PlException
PlTypeError(const std::string& expected, PlTerm actual)
{ // See PL_type_error()
return PlGeneralError(PlCompound("type_error",
PlTermv(PlTerm_atom(expected), actual)));
}
_SWI_CPP2_CPP_inline
PlException
PlDomainError(const std::string& expected, PlTerm actual)
{ // See PL_domain_error()
return PlGeneralError(PlCompound("domain_error",
PlTermv(PlTerm_atom(expected), actual)));
}
_SWI_CPP2_CPP_inline
PlException
PlDomainError(PlTerm expected, PlTerm actual)
{ // See PL_domain_error()
// This is used by
// PlDomainError(PlCompound("argv", PlTermv(PlTerm_integer(size_))), ...)
// for an out-of-bounds indexing error
return PlGeneralError(PlCompound("domain_error",
PlTermv(expected, actual)));
}
_SWI_CPP2_CPP_inline
PlException
PlInstantiationError(PlTerm t)
{ // See PL_instantiation_error()
return PlGeneralError(PlCompound("instantiation_error", PlTermv(t)));
}
_SWI_CPP2_CPP_inline
PlException
PlUninstantiationError(PlTerm t)
{ // See PL_uninstantiation_error()
return PlGeneralError(PlCompound("uninstantiation_error", PlTermv(t)));
}
_SWI_CPP2_CPP_inline
PlException
PlRepresentationError(const std::string& resource)
{ // See PL_representation_error()
return PlGeneralError(PlCompound("representation_error", PlTermv(PlAtom(resource))));
}
_SWI_CPP2_CPP_inline
PlException
PlExistenceError(const std::string& type, PlTerm actual)
{ // See PL_existence_error()
return PlGeneralError(PlCompound("existence_error",
PlTermv(PlTerm_atom(type), actual)));
}
_SWI_CPP2_CPP_inline
PlException
PlPermissionError(const std::string& op, const std::string& type, PlTerm obj)
{ // See: Use PL_permission_error()
return PlGeneralError(PlCompound("permission_error",
PlTermv(PlTerm_atom(op), PlTerm_atom(type), obj)));
}
_SWI_CPP2_CPP_inline
PlException
PlResourceError(const std::string& resource)
{ // See PL_resource_error()
return PlGeneralError(PlCompound("resource_error",
PlTermv(PlTerm_atom(resource))));
}
_SWI_CPP2_CPP_inline
PlException
PlUnknownError(const std::string& description)
{ // For PlWrap()
return PlGeneralError(PlCompound("unknown_error",
PlTermv(PlTerm_atom(description))));
}
/*******************************
* ATOM IMPLEMENTATION *
*******************************/
_SWI_CPP2_CPP_inline
const std::string
PlAtom::mbchars(unsigned int flags) const
{ PlStringBuffers _string_buffers;
size_t len;
char *s;
Plx_atom_mbchars(unwrap(), &len, &s, CVT_EXCEPTION|flags);
return std::string(s, len);
}
_SWI_CPP2_CPP_inline
const std::wstring
PlAtom::wchars() const
{ PlStringBuffers _string_buffers;
size_t len;
const wchar_t *s = Plx_atom_wchars(unwrap(), &len);
return std::wstring(s, len);
}
_SWI_CPP2_CPP_inline
bool PlBlob::write(IOSTREAM *s, int flags) const
{ if ( Sfprintf(s, "<%s>(%p", blob_t_->name, this) < 0 )
return false;
{ bool rc = true;
try
{ if ( !write_fields(s, flags) )
return false;
} PREDICATE_CATCH(rc = false)
if ( !rc )
return false;
}
if ( Sfprintf(s, ")") < 0 )
return false;
return true;
}
_SWI_CPP2_CPP_inline
void PlBlob::save(IOSTREAM *fd) const
{ (void)PL_warning("Cannot save reference to <%s>(%p)", blob_t_->name, this);
throw PlFail();
}
_SWI_CPP2_CPP_inline
PlAtom PlBlob::load(IOSTREAM *fd)
{ (void)PL_warning("Cannot load reference to <%s>", blob_t_->name);
PL_system_error("Cannot load reference to <%s>", blob_t_->name);
return PlAtom(PlAtom::null);
}
_SWI_CPP2_CPP_inline
PlTerm PlBlob::symbol_term() const
{ if ( symbol_.not_null() )
return PlTerm_atom(symbol_);
return PlTerm_var();
}
_SWI_CPP2_CPP_inline
bool PlTerm::unify_blob(const PlBlob* blob) const
{ return PlTerm::unify_blob(static_cast<const void*>(blob),
blob->blob_size_(), blob->blob_t_);
}
_SWI_CPP2_CPP_inline
bool PlTerm::unify_blob(std::unique_ptr<PlBlob>* b) const
{ std::unique_ptr<PlBlob> blob(std::move(*b));
// if std::move is not supported, the above can be replaced by:
// std:unique_ptr<PlBlob> blob;
// blob.swap(*b);
if ( !unify_blob(blob.get()) )
return false;
(void)blob.release(); // Pass ownership to the Prolog blob (`this`)
return true;
}
/*******************************
* TERM (BODY) *
*******************************/
/* PlTerm --> C */
_SWI_CPP2_CPP_inline
PlTerm
PlTerm::copy_term_ref() const
{ return PlTerm(Plx_copy_term_ref(unwrap()));
}
_SWI_CPP2_CPP_inline
void
PlTerm::free_term_ref()
{ if ( not_null() )
Plx_free_term_ref(unwrap());
}
_SWI_CPP2_CPP_inline
void
PlTerm::free_term_ref_reset()
{ free_term_ref();
reset();
}
_SWI_CPP2_CPP_inline
void
PlTerm::as_nil() const
{ get_nil_ex();
}
_SWI_CPP2_CPP_inline
double
PlTerm::as_float() const
{ double v;
get_float_ex(&v);
return v;
}
_SWI_CPP2_CPP_inline
PlAtom
PlTerm::as_atom() const
{ PlAtom v(PlAtom::null);
get_atom_ex(&v);
return v;
}
_SWI_CPP2_CPP_inline
bool
PlTerm::eq_if_atom(PlAtom a) const
{ PlAtom v(PlAtom::null);
return get_atom(&v) && v == a;
}
_SWI_CPP2_CPP_inline
void *
PlTerm::as_pointer() const
{ void *ptr;
get_pointer_ex(&ptr);
return ptr;
}
_SWI_CPP2_CPP_inline
PlRecord
PlTerm::record() const
{ return PlRecord(*this);
}
_SWI_CPP2_CPP_inline
PlTerm::PlTerm(const PlRecord& r)
: WrappedC<term_t>(r.term().unwrap())
{ }
/*******************************
* LISTS *
*******************************/
_SWI_CPP2_CPP_inline
PlTerm_tail::PlTerm_tail(PlTerm l)
{ if ( l.is_variable() || l.is_list() )
reset(l.copy_term_ref());
else
throw PlTypeError("list", l);
}
_SWI_CPP2_CPP_inline
bool
PlTerm_tail::append(PlTerm e)
{ PlTerm_var tmp;
if ( unify_list(tmp, *this) &&
tmp.unify_term(e) )
{ tmp.reset_term_refs();
return true;
}
return false;
}
_SWI_CPP2_CPP_inline
bool PlTerm_tail::next(PlTerm& t)
{ if ( Plx_get_list(unwrap(), t.unwrap(), unwrap()) )
return true;
if ( get_nil() )
return false;
throw PlTypeError("list", *this);
}
_SWI_CPP2_CPP_inline
bool
PlRewindOnFail(std::function<bool()> f)
{ PlFrame frame;
bool rc = f();
if ( !rc )
frame.discard(); // Same as frame.rewind(); destructor's frame.close()
return rc;
}
_SWI_CPP2_CPP_inline
PlQuery
PlCurrentQuery()
{ return PlQuery(Plx_current_query());
}
_SWI_CPP2_CPP_inline
int
PlCall(const std::string& predicate, const PlTermv& args, int flags /* = PL_Q_PASS_EXCEPTION */ )
{ PlQuery q(predicate, args, flags);
return q.next_solution();
}
_SWI_CPP2_CPP_inline
int
PlCall(const std::string& module, const std::string& predicate, const PlTermv& args, int flags /* = PL_Q_PASS_EXCEPTION */ )
{ PlQuery q(module, predicate, args, flags);
return q.next_solution();
}
_SWI_CPP2_CPP_inline
int
PlCall(const std::string& goal, int flags /* = PL_Q_PASS_EXCEPTION */ )
{ PlQuery q("call", PlTermv(PlCompound(goal)), flags);
return q.next_solution();
}
_SWI_CPP2_CPP_inline
int
PlCall(const std::wstring& goal, int flags /* = PL_Q_PASS_EXCEPTION */)
{ PlQuery q("call", PlTermv(PlCompound(goal)), flags);
return q.next_solution();
}
_SWI_CPP2_CPP_inline
int
PlCall(PlTerm goal, int flags /* = PL_Q_PASS_EXCEPTION */ )
{ PlQuery q("call", PlTermv(goal), flags);
return q.next_solution();
}
/* compounds */
_SWI_CPP2_CPP_inline
PlTerm
PlTerm::operator [](size_t index) const
{ PlTerm t;
if ( Plx_get_arg(index, unwrap(), t.unwrap()) )
return t;
if ( !is_compound() )
throw PlTypeError("compound", *this);
/* Construct error term and throw it */
Plx_put_uint64(t.unwrap(), index);
if ( index < 1 )
throw PlDomainError("not_less_than_zero", t);
else
throw PlDomainError("arity", t); /* TODO: arity(t.unwrap()) - see PlTermv::operator[] */
}
_SWI_CPP2_CPP_inline
size_t
PlTerm::arity() const
{ size_t arity;
if ( get_name_arity(nullptr, &arity) )
return arity;
throw PlTypeError("compound", *this);
}
_SWI_CPP2_CPP_inline
PlAtom
PlTerm::name() const
{ atom_t name;
size_t arity;
if ( Plx_get_name_arity(unwrap(), &name, &arity) )
return PlAtom(name);
throw PlTypeError("compound", *this);
}
_SWI_CPP2_CPP_inline
bool
PlTerm::name_arity(PlAtom *name, size_t *arity) const
{ atom_t name_a;
if ( Plx_get_name_arity(unwrap(), &name_a, arity) )
{ if ( name )
*name = PlAtom(name_a);
return true;
}
return false;
}
/* comparison */
_SWI_CPP2_CPP_inline
bool
PlTerm::operator ==(int64_t v) const
{ int64_t v0;
get_int64_ex(&v0);
return v0 == v;
}
_SWI_CPP2_CPP_inline
bool
PlTerm::operator !=(int64_t v) const
{ int64_t v0;
get_int64_ex(&v0);
return v0 != v;
}
_SWI_CPP2_CPP_inline
bool
PlTerm::operator <(int64_t v) const
{ int64_t v0;
get_int64_ex(&v0);
return v0 < v;
}
_SWI_CPP2_CPP_inline
bool
PlTerm::operator >(int64_t v) const
{ int64_t v0;
get_int64_ex(&v0);
return v0 > v;
}
_SWI_CPP2_CPP_inline
bool
PlTerm::operator <=(int64_t v) const
{ int64_t v0;
get_int64_ex(&v0);
return v0 <= v;
}
_SWI_CPP2_CPP_inline
bool
PlTerm::operator >=(int64_t v) const
{ int64_t v0;
get_int64_ex(&v0);
return v0 >= v;
}
/* comparison (string) */
_SWI_CPP2_CPP_inline
bool
PlTerm::eq(const char *s) const
{ char *s0;
PlStringBuffers _string_buffers;
if ( _get_chars(&s0, CVT_ALL) )
return strcmp(s0, s) == 0;
throw PlTypeError("text", *this);
}
_SWI_CPP2_CPP_inline
bool
PlTerm::eq(const wchar_t *s) const
{ wchar_t *s0;
PlStringBuffers _string_buffers;
if ( _get_wchars(nullptr, &s0, CVT_ALL) )
return wcscmp(s0, s) == 0;
throw PlTypeError("text", *this);
}
_SWI_CPP2_CPP_inline
bool
PlTerm::eq(const std::string& s) const
{ char *s0;
PlStringBuffers _string_buffers;
// Doesn't handle non-NUL terminated - but it's only used by deprecated operator ==
if ( _get_chars(&s0, CVT_ALL) )
return s.compare(s0) == 0;
throw PlTypeError("text", *this);
}
_SWI_CPP2_CPP_inline
bool
PlTerm::eq(const std::wstring& s) const
{ // Doesn't handle non-NUL terminated - but it's only used by deprecated operator ==
return s.compare(get_wchars(CVT_ALL)) == 0;
throw PlTypeError("text", *this);
}
_SWI_CPP2_CPP_inline
bool
PlTerm::eq(PlAtom a) const
{ atom_t v;
if ( Plx_get_atom(unwrap(), &v) )
return v == a.unwrap();
throw PlTypeError("atom", *this);
}
/*******************************
* COMPOUND (BODY) *
*******************************/
_SWI_CPP2_CPP_inline
PlCompound::PlCompound(const wchar_t *text)
{ term_t t = Plx_new_term_ref();
if ( !Plx_wchars_to_term(text, t) )
throw PlException(PlTerm(t));
Plx_put_term(unwrap(), t);
}
_SWI_CPP2_CPP_inline
PlCompound::PlCompound(const std::string& text, PlEncoding enc)
{ term_t t = Plx_new_term_ref();
PlEx<bool>(t != (term_t)0);
// TODO: PL_put_term_from_chars() should take an unsigned int flags
PlEx<int>(Plx_put_term_from_chars(t, static_cast<int>(enc)|CVT_EXCEPTION, text.size(), text.data()));
Plx_put_term(unwrap(), t);
}
_SWI_CPP2_CPP_inline
PlCompound::PlCompound(const std::wstring& text)
{ term_t t = Plx_new_term_ref();
PlEx<bool>(t != (term_t)0);
// TODO: what is wchar_t equivalent of PL_put_term_from_chars()?
if ( !Plx_wchars_to_term(text.c_str(), t) ) // TODO: use text.size()
throw PlException(PlTerm(t));
Plx_put_term(unwrap(), t);
}
_SWI_CPP2_CPP_inline
PlCompound::PlCompound(const char *functor, const PlTermv& args)
{ functor_t f = Plx_new_functor(Plx_new_atom(functor), args.size());
PlEx<bool>(f != (functor_t)0);
Plx_cons_functor_v(unwrap(), f, args.termv());
}
_SWI_CPP2_CPP_inline
PlCompound::PlCompound(const wchar_t *functor, const PlTermv& args)
{ functor_t f = Plx_new_functor(Plx_new_atom_wchars(wcslen(functor), functor), args.size());
PlEx<bool>(f != (functor_t)0);
Plx_cons_functor_v(unwrap(), f, args.termv());
}
_SWI_CPP2_CPP_inline
PlCompound::PlCompound(const std::string& functor, const PlTermv& args)
{ functor_t f = Plx_new_functor(Plx_new_atom_nchars(functor.size(), functor.data()), args.size());
Plx_cons_functor_v(unwrap(), f, args.termv());
}
_SWI_CPP2_CPP_inline
PlCompound::PlCompound(const std::wstring& functor, const PlTermv& args)
{ functor_t f = Plx_new_functor(Plx_new_atom_wchars(functor.size(), functor.data()), args.size());
Plx_cons_functor_v(unwrap(), f, args.termv());
}
/*******************************
* TERMV (BODY) *
*******************************/
_SWI_CPP2_CPP_inline
PlTermv::PlTermv(PlAtom a)
: size_(1),
a0_(PlTerm_atom(a).unwrap())
{ PlEx<bool>(a0_ != (term_t)0);
}
_SWI_CPP2_CPP_inline
PlTermv::PlTermv(PlTerm m0)
: size_(1),
a0_(m0.unwrap())
{ // Assume that m0 is valid
}
_SWI_CPP2_CPP_inline
PlTermv::PlTermv(PlTerm m0, PlTerm m1)
: size_(2),
a0_(Plx_new_term_refs(2))
{ PlEx<bool>(a0_ != (term_t)0);
// Commented out code is possibly less efficient:
// PlTerm(a0_+0).put_term(m0); // (*this)[0].put_term(m0) does an unnecessary range check
// PlTerm(a0_+1).put_term(m1);
Plx_put_term(a0_+0, m0.unwrap());
Plx_put_term(a0_+1, m1.unwrap());
}
_SWI_CPP2_CPP_inline
PlTermv::PlTermv(PlTerm m0, PlTerm m1, PlTerm m2)
: size_(3),
a0_(Plx_new_term_refs(3))
{ PlEx<bool>(a0_ != (term_t)0);
Plx_put_term(a0_+0, m0.unwrap());
Plx_put_term(a0_+1, m1.unwrap());
Plx_put_term(a0_+2, m2.unwrap());
}
_SWI_CPP2_CPP_inline
PlTermv::PlTermv(PlTerm m0, PlTerm m1, PlTerm m2, PlTerm m3)
: size_(4),
a0_(Plx_new_term_refs(4))
{ PlEx<bool>(a0_ != (term_t)0);
Plx_put_term(a0_+0, m0.unwrap());
Plx_put_term(a0_+1, m1.unwrap());
Plx_put_term(a0_+2, m2.unwrap());
Plx_put_term(a0_+3, m3.unwrap());
}
_SWI_CPP2_CPP_inline
PlTermv::PlTermv(PlTerm m0, PlTerm m1, PlTerm m2, PlTerm m3, PlTerm m4)
: size_(5),
a0_(Plx_new_term_refs(5))
{ PlEx<bool>(a0_ != (term_t)0);
Plx_put_term(a0_+0, m0.unwrap());
Plx_put_term(a0_+1, m1.unwrap());
Plx_put_term(a0_+2, m2.unwrap());
Plx_put_term(a0_+3, m3.unwrap());
Plx_put_term(a0_+4, m4.unwrap());
}
_SWI_CPP2_CPP_inline
PlTerm
PlTermv::operator [](size_t n) const
{ if ( n >= size_ )
throw PlDomainError(PlCompound("argv",
PlTermv(PlTerm_integer(size_))),
PlTerm_integer(n));
return PlTerm(a0_+n);
}
/*******************************
* EXCEPTIONS (BODY) *
*******************************/
_SWI_CPP2_CPP_inline
PlTerm
PlException::string_term() const
{ PlFrame fr;
// Note that the result is a *term*, so it's unencoded (wchar_t
// or equivalent) and will be encoded when it's output.
// TODO: remove USE_PRINT_MESSAGE code (obsolete)
// - or use with_output_to(string(String), print_message(error, ...))
#ifdef USE_PRINT_MESSAGE
PlTermv av(2);
PlCheckFail(av[0].unify_term(PlCompound("print_message",
PlTermv("error", term()))));
PlQuery q("$write_on_string", av);
if ( q.next_solution() )
return av[1];
#else
// '$messages':message_to_string(error(existence_error(procedure,unknown_predicate/1),context(system:call/1,_)), Str).
// Str = "call/1: Unknown procedure: unknown_predicate/1"
PlTermv av(2);
PlCheckFail(av[0].unify_term(term()));
PlQuery q("$messages", "message_to_string", av);
if ( q.next_solution() )
return av[1];
#endif
// TODO: return term_.as_string()
return PlTerm_string("[ERROR: Failed to generate message. Internal error]");
}
_SWI_CPP2_CPP_inline
const std::string
PlException::as_string(PlEncoding enc) const
{ // Use what_str_ to hold the string so that c_str() doesn't return
// a pointer into the stack. Note that as_string() can cause an
// exception (out of memory, either in generating the string or in
// allocating the std::string) even though we specify "throw()" -
// telling the truth "noexcept(false)" results in a compilation
// error.
(void)enc; // TODO: use this
const_cast<PlException*>(this)->set_what_str();
return what_str_;
}
_SWI_CPP2_CPP_inline
const char*
PlException::what() const throw()
{ const_cast<PlException*>(this)->set_what_str();
return what_str_.c_str();
}
_SWI_CPP2_CPP_inline
void
PlException::set_what_str()
{ if ( what_str_.empty() )
{ // Doing a query inside a query is ... problematic:
// TODO: const_cast<PlException*>(this)->what_str_ = string_term().as_string();
what_str_ = term().as_string();
}
}
_SWI_CPP2_CPP_inline
void
PlException::erase()
{ if ( term_rec_.not_null() )
term_rec_.erase();
term_rec_.set_null();
}
/*******************************
* QUERY (BODY) *
*******************************/
_SWI_CPP2_CPP_inline
int
PlQuery::next_solution()
{ int rval = PL_next_solution(unwrap());
if ( flags_ & PL_Q_EXT_STATUS )
{ // values are:
// PL_S_EXCEPTION, PL_S_FALSE PL_S_TRUE PL_S_LAST. PL_S_YIELD:
return rval;
} else
{ if ( rval )
return rval;
}
// If we get here, rval is "false". The user must specifically
// request PL_Q_CATCH_EXCEPTION; otherwise exception_qid() won't
// give an appropriate value.
if ( flags_ & PL_Q_CATCH_EXCEPTION )
PlEx_fail(exception_qid());
close_destroy();
return rval;
}
/*******************************
* DEBUG *
*******************************/
// This is used in SWI-cpp2-plx.h - currently its action is commented out.
// TODO: remove this when PlWrapDebug() is removed from SWI-cpp2-plx.h
#ifdef O_DEBUG
#include <SWI-Stream.h>
_SWI_CPP2_CPP_inline
void PlWrapDebug(const char*msg) {
// Sdprintf("***PlWrapDebug %s\n", msg);
// PL_check_stacks();
}
#endif
/*******************************
* PlString *
*******************************/
_SWI_CPP2_CPP_inline
PlStream::PlStream(PlTerm stream, int flags)
{ Plx_get_stream(stream.unwrap(), &s_, flags);
check_stream(); // Shouldn't happen
}
_SWI_CPP2_CPP_inline
PlStream::PlStream(IOSTREAM *s)
: s_(Plx_acquire_stream(s))
{ check_stream(); // Shouldn't happen
}
_SWI_CPP2_CPP_inline
PlStream::~PlStream() noexcept
{ release();
}
_SWI_CPP2_CPP_inline
void PlStream::release()
{ if ( s_ )
Plx_release_stream(s_);
s_ = nullptr;
}
_SWI_CPP2_CPP_inline
void
PlStream::check_stream() const
{ if ( ! s_ )
throw PlUnknownError("Stream not set");
}
/* JW: was using check_rc(), but somehown Apple Clang thinks ssize_t
maps both to int32_t and int64_t.
*/
#define _SWI_CPP2_CPP_check_rc(rc_t, defn, call) \
_SWI_CPP2_CPP_inline \
rc_t \
PlStream::defn \
{ check_stream(); \
rc_t rc = call; \
if ( rc < 0 ) { release(); throw PlUnknownError("Stream error"); } \
return rc; \
}
#define _SWI_CPP2_CPP_check_brc(rc_t, defn, call) \
_SWI_CPP2_CPP_inline \
rc_t \
PlStream::defn \
{ check_stream(); \
rc_t rc = call; \
if ( !rc ) { release(); throw PlUnknownError("Stream error"); } \
return rc; \
}
#define _SWI_CPP2_CPP_nocheck(rc_t, defn, call) \
_SWI_CPP2_CPP_inline \
rc_t \
PlStream::defn \
{ check_stream(); \
return call; \
}