2 A C++ interface to SWI-Prolog (Version 2)
AllApplicationManualNameSummaryHelp

  • 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)
          • Classes for the recorded database: PlRecord and PlRecordExternalCopy
          • 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)

2.12 Classes for the recorded database: PlRecord and PlRecordExternalCopy

The recorded database is has two wrappers, for supporting the internal records and external records.

Currently, the interface to internal records requires that the programmer explicitly call the dupicate() and erase() methods - in future, it is intended that this will be done automatically by a new PlRecord class, so that the internal records behave like "smart pointers"; in the meantime, the PlRecord provides a trivial wrapper around the various recorded database functions.

The class PlRecord supports the following methods:

PlRecord(PlTerm)
Constructor.
PlRecord(PlRecord)
Copy and move constructors. Currently these do not do any reference counting. The assignment operator is currently not supported.
 PlRecord()
Destructor. Currently this does not call PL_erase().
PlTerm term()
- creates a term from the record, using PL_recorded().
void erase()
- decrements the reference count of the record and deletes it if the count goes to zero, using PL_erase(). It is safe to do this multiple times on the same PlRecord object.
PlRecord duplicate()
- increments the reference count of the record, using PL_duplicate_record().

The class PlRecord provides direct access to the reference counting aspects of the recorded term (through the duplicate() and erase() methods), but does not connect these with C++'s copy constructor, assignment operator, or destructor. If the recorded term is encapsulated within an object, then the containing object can use the duplicate() and erase() methods in its copy and move constructors and assignment operators (and the erase() method in the destructor).17The copy constructor and assignment use the duplicate() method; the move constructor and assignment use the duplicate() method to assign to the destination and the erase() method on the source; and the destructor uses erase().

Alternatively, the std::shared_ptr or std::unique_ptr can be used with the supplied PlrecordDeleter, which calls the erase() method when the shared_ptr reference count goes to zero or when the unique_ptr goes out of scope.

For example:

std::shared_ptr<PlRecord> r(new PlRecord(t.record()), PlRecordDeleter());
assert(t.unify_term(r->term()));

The class PlRecordExternalCopy keeps the external record as an uninterpreted string. It supports the following methods.

PlRecordExternalCopy()
Constructor. Creates a string using Pl_record_external(), copies it into the object, then deletes the reference using PL_erase_external().
PlTerm term()
- creates a term from the record, using PL_recorded_external()).