- Documentation
- Reference manual
- Packages
- A C++ interface to SWI-Prolog
- A C++ interface to SWI-Prolog (Version 2)
- Summary of changes between Versions 1 and 2
- Introduction (version 2)
- The life of a PREDICATE (version 2)
- Overview (version 2)
- Examples (version 2)
- Rational for changes from version 1 (version 2)
- Porting from version 1 to version 2
- The class PlFail (version 2)
- The class PlTerm (version 2)
- The class PlTermv (version 2)
- The class PlAtom - Supporting Prolog constants (version 2)
- Unification and foreign frames (version 2)
- The class PlRegister (version 2)
- The class PlQuery (version 2)
- The PREDICATE and PREDICATE_NONDET macros (version 2)
- Exceptions (version 2)
- Embedded applications (version 2)
- Considerations (version 2)
- Conclusions (version 2)
- A C++ interface to SWI-Prolog (Version 2)
- A C++ interface to SWI-Prolog
2.7 Porting from version 1 to version 2
The easiest way of porting from SWI-cpp.h
to SWI-cpp2.h
is to change the #include "SWI-cpp.h"
to #include
"SWI-cpp2.h"
and look at the warning and error messages. Where
possible, version 2 keeps old interfaces with a "deprecated" flag if
there is a better way of doing things with version 2.
Here is a list of typical changes:
- Replace PlTerm() constructor with
PlTerm_var() for uninstantiated variables,
PlTerm_atom(a) for atoms, PlTerm_term_t(t) for the raw
term_t
, PlTerm_integer(i), PlTerm_float(v), or PlTerm_pointer(p). - Examine uses of
char*
orwchar_t
and replace them bystd::string
orstd::wstring
if appropriate. For example,cout << "Hello " << A1.as_string().c_str()() << endl
can be replaced bycout << "Hello " << A1.as_string() << endl
. In general,std::string
is safer thanchar*
because the latter can potentially point to freed memory. - Instead of returning
false
from a predicate for failure, you can dothrow PlFail()
. This mechanism is also used by PlCheck(rc). Note that throwing an exception is significantly slower than returningfalse
, so performance-critical code should avoid PlCheck(rc). - You can use the PlCheck(rc) to check the return code from a
function in
SWI-Prolog
and throw a PlFail() exception to short-circuit execution and return failure (false
) to Prolog. PlAtom::handle
has been replaced byPlAtom::C_
.PlTerm::ref
has been replaced byPlAtom::C_
.PlFunctor::functor
has been replaced byPlAtom::C_
.- The operator
for unification has been deprecated, replaced by various=
unify_XXX
‘methods (PlTerm::unify_term(t2), PlTerm::unify_atom(a), etc.). - The various "cast" operators have been deprecated or deleted; you
should use the various "getter" methods. For example,
static_cast<char*>(t)
is replaced byt.as_string().c_str()
;static_cast<int32_t>(t)
is replaced byt.as_int32_t()
. - It is recommended that you do not use
int
orlong
because of problems porting between Unix and Windows platforms; instead, useint32_t
,int64_t
,uint32_t
,uint64_t
, etc.