This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/CXX/yapa.hh

172 lines
5.4 KiB
C++
Raw Normal View History

2017-04-07 23:10:59 +01:00
/// @file yapa.hh
///
/// @brief C++ Interface to atoms and their properties.
2017-03-20 15:52:48 +00:00
2015-02-09 01:52:10 +00:00
#ifndef YAPA_HH
#define YAPA_HH 1
/**
2017-03-20 15:52:48 +00:00
*
2017-04-07 23:10:59 +01:00
* @defgroup yap-cplus-interface-atoms Prolog Atoms
2017-03-20 15:52:48 +00:00
*
2017-04-07 23:10:59 +01:00
* @brief Symbols and associated propeeties.
*
* @ingroup yap-cplus-interface
2017-03-20 15:52:48 +00:00
* @tableofcontents
2017-04-07 23:10:59 +01:00
* @{
2017-03-20 15:52:48 +00:00
*
*
2017-04-07 23:10:59 +01:00
* Prolog interns symbols, called *atoms* in a Hash table, usually
called the atom table_. Each entry in this table stores the atom's
name, but also may have a linked list of _properties_ We use
properties to represent all kinds of data including. data-base
tecords, predicates. operators, and more.
2017-03-20 15:52:48 +00:00
*
2017-04-07 23:10:59 +01:00
* In a nutshell:
* - YAPAtom serves as the gateway to the data-base;
2017-09-06 01:16:36 +01:00
*
2017-04-07 23:10:59 +01:00
* - YAProp abstracts most data-base objects.
2017-03-20 15:52:48 +00:00
*
2017-04-07 23:10:59 +01:00
* - PropTag allows distinguishing the different classes of YAPProp.
2017-03-20 15:52:48 +00:00
*/
2015-02-09 01:52:10 +00:00
enum PropTag {
/// predicate
PRED_TAG = PEProp, // 0x0000,
/// db key, may be associated with a functor
2017-03-20 15:52:48 +00:00
DB_TAG = DBProperty, // 0x8000,
2015-02-09 01:52:10 +00:00
/// generic functor, may include sub-properties
2017-03-20 15:52:48 +00:00
FUNCTOR_TAG = FunctorProperty, // 0xBB00,
2015-02-09 01:52:10 +00:00
// SPARSE_FUNCTOR_TAG = 0xFFDF,
/// arithmetic function
2017-03-20 15:52:48 +00:00
ARITHMETIC_PROPERTY_TAG = ExpProperty, // 0xFFE0,
2015-02-09 01:52:10 +00:00
/// map the atom to an integer
2017-03-20 15:52:48 +00:00
TRANSLATION_TAG = TranslationProperty, // 0xFFF4,
2017-09-06 01:16:36 +01:00
/// ensure the atom may not be garbafe colected
HOLD_TAG = HoldProperty, // 0xFFF6
2017-03-20 15:52:48 +00:00
/// named mutEX
MUTEX_TAG = MutexProperty, // 0xFFF6,
2015-02-09 01:52:10 +00:00
/// A typed array, may be in-db or in-stack deped
2017-09-06 01:16:36 +01:00
ARRAY_TAG = ArrayProperty, // 0xFFF7,
2015-02-09 01:52:10 +00:00
/// module
2017-03-20 15:52:48 +00:00
MODULE_TAG = ModProperty, // 0xFFFA,
2015-02-09 01:52:10 +00:00
/// the original SICStus blackboard
2017-03-20 15:52:48 +00:00
BLACKBOARD_TAG = BBProperty, // 0xFFFB,
2015-02-10 09:14:55 +00:00
/// associate an atomic value with the atom
2017-03-20 15:52:48 +00:00
VALUE_TAG = ValProperty, // 0xFFFC,
2015-02-09 01:52:10 +00:00
/// Demoen's proposal for gkobal variables
2017-03-20 15:52:48 +00:00
GLOBAL_VAR_TAG = GlobalProperty, // 0xFFFD
2015-02-10 09:14:55 +00:00
/// SWI-STYLE ATOM Extension
2017-03-20 15:52:48 +00:00
BLOB_TAG = BlobProperty, // 0xFFFE,
2017-09-06 01:16:36 +01:00
/// Prolog operator,
OPERATOR_TAG = OpProperty, // 0xFFFF,
2017-03-20 15:52:48 +00:00
};
2015-02-09 01:52:10 +00:00
/**
* @brief Atom
* A YAP data-base is a collection of atoms, where each atom has a name
* and a set of Properties. Examples of properties include functors,
* predicates, operators, modules, almost everything.
*
*/
2017-06-12 18:00:47 +01:00
class X_API YAPAtom {
2016-07-31 16:22:24 +01:00
friend class YAPEngine;
2015-02-09 01:52:10 +00:00
friend class YAPModuleProp;
friend class YAPPredicate;
friend class YAPFunctor;
friend class YAPAtomTerm;
friend class YAProp;
friend class YAPModule;
2016-07-31 16:22:24 +01:00
friend class YAPQuery;
2015-02-09 01:52:10 +00:00
Atom a;
/// construct new YAPAtom from Atom
YAPAtom( Atom at ) { a = at; }
public:
2017-03-20 15:52:48 +00:00
/// construct new YAPAtom from UTF-8 string
YAPAtom( const char * s) { a = Yap_LookupAtom( s ); }
/// construct new YAPAtom from UTF-8 string
YAPAtom( const wchar_t * s) { CACHE_REGS a = UTF32ToAtom( s PASS_REGS ); }
2015-02-09 01:52:10 +00:00
/// construct new YAPAtom from wide string
//YAPAtom( const wchar_t * s) { a = Yap_LookupMaybeWideAtom( s ); }
2015-02-09 01:52:10 +00:00
/// construct new YAPAtom from max-length string
2017-03-20 15:52:48 +00:00
YAPAtom( const char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
2015-02-09 01:52:10 +00:00
/// get name of atom
2016-07-31 16:22:24 +01:00
const char *getName(void);
/// get name of (other way)
2017-03-20 15:52:48 +00:00
inline const char *text(void) { return getName(); } ;
/// get prop of type
2017-09-06 01:16:36 +01:00
Prop getProp( PropTag tag ) { return Yap_GetAProp( a , (PropFlags)tag ); }
2015-02-09 01:52:10 +00:00
};
/**
* @brief Prop
* A YAP Propery is ameta-class for everything one can in a atom.
* Examples of properties include functors,
* predicates, operators, modules, almost everything.
*
*/
2017-06-12 18:00:47 +01:00
class X_API YAPProp {
2015-02-09 01:52:10 +00:00
friend class YAPModuleProp;
friend class YAPFunctor;
/// does nothing, p is defined by the subclass
YAPProp() { }
/// get type of prop
PropTag tag( Prop p ) { return (PropTag)(p->KindOfPE); }
public:
/// get name of property
2017-02-20 14:38:00 +00:00
// virtual YAPAtom name();
2017-03-20 15:52:48 +00:00
virtual ~YAPProp() {};
2017-09-06 01:16:36 +01:00
2015-02-09 01:52:10 +00:00
};
2019-03-18 14:47:29 +00:00
/**
* @brief YAPFunctor represents Prolog functors Name/Arity
*/
class X_API YAPFunctor : public YAPProp {
friend class YAPApplTerm;
friend class YAPTerm;
friend class YAPPredicate;
friend class YAPQuery;
Functor f;
/// Constructor: receives Prolog functor and casts it to YAPFunctor
///
/// Notice that this is designed for internal use only.
inline YAPFunctor(Functor ff) { f = ff; }
public:
/// Constructor: receives name as an atom, plus arity
///
/// This is the default method, and the most popular
YAPFunctor(YAPAtom at, uintptr_t arity) { f = Yap_MkFunctor(at.a, arity); }
/// Constructor: receives name as a string plus arity
///
/// Notice that this is designed for ISO-LATIN-1 right now
/// Note: Python confuses the 3 constructors,
/// use YAPFunctorFromString
inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) {
f = Yap_MkFunctor(Yap_LookupAtom(s), arity);
}
/// Constructor: receives name as a wide string plus arity
///
/// Notice that this is designed for UNICODE right now
///
/// Note: Python confuses the 3 constructors,
/// use YAPFunctorFromWideString
inline YAPFunctor(const wchar_t *s, uintptr_t arity) {
CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity);
}
/// Getter: extract name of functor as an atom
///
/// this is for external usage.
YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); }
/// Getter: extract arity of functor as an unsigned integer
///
/// this is for external usage.
uintptr_t arity(void) { return ArityOfFunctor(f); }
};
2015-02-09 01:52:10 +00:00
2017-09-06 01:16:36 +01:00
#endif /* YAPA_HH */
2017-03-20 15:52:48 +00:00
/// @}