Did you know ... | Search Documentation: |
Atom map utilities |
The include file SWI-cpp2-atommap.h
contains a templated
class
AtomMap
for mapping atoms to atoms or terms. The typical
use case is for when it is desired to open a database or stream and,
instead of passing around the blob, an atom can be used to identify the
blob.
The keys in the map must be standard Prolog atoms and not blobs - the code depends on the fact that an atom has a unique ID.
The AtomMap
is thread-safe (it contains a mutex). It
also takes care of reference counts for both the key and the value. Here
is a typical use case:
static AtomMap<PlAtom, PlAtom> map_atom_my_blob("alias", "my_blob"); // look up an entry: auto value = map_atom_my_blob(A1.as_atom()); PlCheckFail(value.not_null()); // insert an entry: map_atom_my_blob.insert(A1.as_atom(), A2.as_atom()); // remove an entry: map_atom_my_blob.erase(A1.as_atom());
The constructor and methods are as follows:
AtomMap
.
The ValueType and StoredValueType specify what
type you wish for the value. Currently, two value types are supported:
PlAtom
- the StoredValueType should be PlAtom
.PlTerm
- the StoredValueType shoud be PlRecord
(because the term needs to be put on the global stack).permission_error
if the value
is already in the map, unless the value is identical to the value in the
map. The insert() method converts the value to the StoredValueType
.
The insertion code takes care of atom reference counts.StoredValueType
to ValueType
.