- Documentation
- Reference manual
- Packages
- A C++ interface to SWI-Prolog
- A C++ interface to SWI-Prolog
- Summary of changes between Versions 1 and 2
- A simple example
- Sample code
- Introduction
- The life of a PREDICATE
- Overview
- Examples
- Rationale for changes from version 1
- Porting from version 1 to version 2
- The class PlFail
- Overview of accessing and changing values
- The class PlRegister
- The class PlQuery
- The PREDICATE and PREDICATE_NONDET macros
- Exceptions
- Embedded applications
- Considerations
- Conclusions
 
 
- A C++ interface to SWI-Prolog
 
- A C++ interface to SWI-Prolog
 
1.9 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.
For convenience when calling PL_*() functions, the Plx_*() wrapper 
functions add error checking. Also, most of the PL_*() functions that 
work with term_t, atom_t, etc. have 
corresponding methods in PlTerm, PlAtom, etc.
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_tand replace them bystd::stringorstd::wstringif appropriate. For example,cout << "Hello " << (char*)A1 << endlcan be replaced bycout << "Hello " << A1.as_string() << endl. In general,std::stringis safer thanchar*because the latter can potentially point to freed memory.
- Instead of returning falsefrom a predicate for failure, you can dothrow PlFail(). This mechanism is also used by PlCheckFail(rc). Note that throwing an exception is slower than returningfalse, so performance-critical code should avoid PlCheckFail(rc) if failure is expected to happen often.
- You can use the PlEx(rc) to check 
the return code from a function in SWI-Prologand throw aPlFailexception to short-circuit execution and return failure (false) to Prolog (or throw aPlExceptionif there was a Prolog error.
- PlAtom::handlehas been replaced by- PlAtom::C_, which should be accessed by PlAtom::unwrap().
- PlTerm::refhas been replaced by- PlTerm::C_, which should be accessed by PlTerm::unwrap().
- PlFunctor::functorhas been replaced by- PlFunctor::C_, which should be accessed by PlFunctor::unwrap().
- The operator =
- 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()(and you should prefert.as_striong();static_cast<int32_t>(t)is replaced byt.as_int32_t(), etc.
- It is recommended that you do not use intorlongbecause of problems porting between Unix and Windows platforms; instead, useint32_t,int64_t,uint32_t,uint64_t, etc.