2.9 The class PlTerm (version 2)
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • A C++ interface to SWI-Prolog
        • A C++ interface to SWI-Prolog (Version 2)
          • The class PlTerm (version 2)
            • Constructors (version 2)
            • Overview of accessing and changing values (version 2)
            • Converting PlTerm to native C and C++ types (version 2)
            • Unification (version 2)
            • Comparison (version 2)
            • Analysing compound terms (version 2)
            • Miscellaneous (version 2)
            • The class PlTermString (version 2)
            • The class PlCodeList (version 2)
            • The class PlCharList (version 2)
            • The class PlCompound (version 2)
            • The class PlTail (version 2)

2.9.1 Constructors (version 2)

The constructors are defined as subclasses of PlTerm, with a name that reflects the Prolog type of what is being created (e.g., PlTerm_atom creates an atom; PlTerm_string creates a Prolog string). All of the constructors are "explicit" because implicit creation of PlTerm objects can lead to subtle and difficult to debug errors.

PlTerm :: PlTerm()
Creates a new initialised "null" term (holding a Prolog variable).
PlTerm_term_t :: PlTerm_term_t(term_t t)
Converts between the C-interface and the C++ interface by turning the term-reference into an instance of PlTerm. Note that, being a lightweight class, this is a no-op at the machine-level!
PlTerm_atom :: PlTerm_atom(const char *text)
Creates a term-references holding a Prolog atom representing text.
PlTerm_atom :: PlTerm_atom(const wchar_t *text)
Creates a term-references holding a Prolog atom representing text.
PlTerm_atom :: PlTerm_atom(const PlAtom &atom)
Creates a term-references holding a Prolog atom from an atom-handle.
PlTerm_int :: PlTerm_int(long n)
Creates a term-references holding a Prolog integer representing n.
PlTerm_int :: PlTerm_int(int64_t n)
Creates a term-references holding a Prolog integer representing n (up to 64 bits signed).
PlTerm_int :: PlTerm_int(uint64_t n)
Creates a term-references holding a Prolog integer representing n (up to 64 bits unsigned).
PlTerm_float :: PlTerm_float(double f)
Creates a term-references holding a Prolog float representing f.
PlTerm_pointer :: PlTerm_pointer(void *ptr)
Creates a term-references holding a Prolog pointer. A pointer is represented in Prolog as a mangled integer. The mangling is designed to make most pointers fit into a tagged-integer. Any valid pointer can be represented. This mechanism can be used to represent pointers to C++ objects in Prolog. Please note that‘MyClass' should define conversion to and from void *. Also note that in general blobs are a better way of doing this (see the section on blobs in the Foreign Language Interface part of the SWI-Prolog manual).
PREDICATE(make_my_object, 1)
{ auto myobj = new MyClass();

  return A1.unify_pointer(myobj);
}

PREDICATE(my_object_contents, 2)
{ auto myobj = static_cast<MyClass*>(A1.pointer());
  return A2.unify_string(myobj->contents);
}

PREDICATE(free_my_object, 1)
{ auto myobj = static_cast<MyClass*>(A1.pointer());

  delete myobj;
  return true;
}