6 Supporting Prolog constants
Both for quick comparison as for quick building of lists of atoms, it is desirable to provide access to Prolog's atom-table, mapping handles to unique string-constants. If the handles of two atoms are different it is guaranteed they represent different text strings.
Suppose we want to test whether a term represents a certain atom, this interface presents a large number of alternatives:
Direct comparision to char *
Example:
PREDICATE(test, 1)
{ if ( A1 == "read" )
...;
This writes easily and is the preferred method is performance is not critical and only a few comparisons have to be made. It validates A1 to be a term-reference representing text (atom, string, integer or float) extracts the represented text and uses strcmp() to match the strings.
Direct comparision to PlAtom
Example:
static PlAtom ATOM_read("read");
PREDICATE(test, 1)
{ if ( A1 == ATOM_read )
...;
This case raises a type_error if A1 is not an
atom. Otherwise it extacts the atom-handle and compares it to the
atom-handle of the global PlAtom
object. This approach is faster and provides more strict type-checking.
Extraction of the atom and comparison to PlAtom
Example:
static PlAtom ATOM_read("read");
PREDICATE(test, 1)
{ PlAtom a1(A1);
if ( a1 == ATOM_read )
...;
This approach is basically the same as section 6, but in nested if-then-else the extraction of the atom from the term is done only once.
Extraction of the atom and comparison to char *
Example:
PREDICATE(test, 1)
{ PlAtom a1(A1);
if ( a1 == "read" )
...;
This approach extracts the atom once and for each test extracts the represented string from the atom and compares it. It avoids the need for global atom constructors.
- PlAtom :: PlAtom(atom_t handle)
- Create from C-interface atom handle. Used internally and for integration with the C-interface.
- PlAtom :: PlAtom(const wchar_t *text)
- PlAtom :: PlAtom(const char *text)
- Create an atom from a string. The text is copied if a new atom is created.
- PlAtom :: PlAtom(const PlTerm &t)
- If t represents an atom, the new instance represents this
atom. Otherwise a
type_erroris thrown. - int PlAtom::operator ==(const wchar_t *text)
- int PlAtom::operator ==(const char *text)
- Yields
TRUEif the atom represents text,FALSEotherwise. Performs a strcmp() for this. - int PlAtom::operator ==(const PlAtom &a)
- Compares the two atom-handles, returning
TRUEorFALSE.