1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8% 
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License. 
13% 
14% The Original Code is  The ECLiPSe Constraint Logic Programming System. 
15% The Initial Developer of the Original Code is  Cisco Systems, Inc. 
16% Portions created by the Initial Developer are
17% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18% 
19% Contributor(s): 
20% 
21% END LICENSE BLOCK
22%
23% $Id: embsumcxx.tex,v 1.2 2015/01/14 01:31:09 jschimpf Exp $
24%
25
26\chapter{Summary of C++ Interface Functions}
27%HEVEA\cutdef[1]{section}
28\label{chapsumcxx}
29Note that apart from the methods and functions described here,
30all functions from the C interface which operate on simple types
31(int, long, char*) can also be used from C++ code.
32
33\section{Constructing {\eclipse} terms in C++}
34
35\subsection{Class EC_atom and EC_functor}
36The {\eclipse} dictionary provides unique identifiers for
37name/arity pairs. EC_atoms are dictionary identifiers with zero arity,
38EC_functors are dictionary identifiers with non-zero arity.
39\begin{description}
40\item[EC_atom(char*)]\ \\
41	looks up or enters the given string into the {\eclipse}
42	dictionary and returns a unique atom identifier for it.
43
44\item[char* EC_atom::name()]\ \\
45	returns the name string of the given atom identifier.
46
47\item[EC_functor(char*,int)]\ \\
48	looks up or enters the given string with arity into the {\eclipse}
49	dictionary and returns a unique functor identifier for it.
50
51\item[char* EC_functor::name()]\ \\
52	returns the name string of the given functor identifier.
53
54\item[int EC_functor::arity()]\ \\
55	returns the arity of the given functor identifier.
56\end{description}
57
58
59\subsection{Class EC_word}
60The EC_word is the basic type that all {\eclipse} data structures are
61built from (because within {\eclipse} typing is dynamic).
62The following are the functions for constructing {\eclipse} terms from
63the fundamental C++ types:
64\begin{description}
65\item[EC_word(const char *)]\ \\
66	converts a C++ string to an {\eclipse} string. The string is copied.
67
68\item[EC_word(const int, const char *)]\ \\
69	converts a C++ string of given length to an {\eclipse} string.
70	The string is copied and can contain NUL bytes.
71
72\item[EC_word(const EC_atom)]\ \\
73	creates an {\eclipse} atom from an atom identifier.
74
75\item[EC_word(const long)]\ \\
76	converts a C++ long to an {\eclipse} integer.
77
78\item[EC_word(const long long)]\ \\
79	converts a C++ long long to an {\eclipse} integer.
80
81\item[EC_word(const double)]\ \\
82	converts a C++ double to an {\eclipse} double float.
83
84\item[EC_word(const EC_ref\&)]\ \\
85	retrieves the {\eclipse} term referenced by the EC_ref (see below).
86
87\item[EC_word term(const EC_functor,const EC_word args\nil)]\ \\
88\item[EC_word term(const EC_functor,const EC_word arg1, ...)]\ \\
89	creates an {\eclipse} compound term from the given components.
90
91\item[EC_word list(const EC_word hd, const EC_word tl)]\ \\
92	Construct a single {\eclipse} list cell.
93
94\item[EC_word list(int n, const long*)]\ \\
95	Construct an {\eclipse} list of length n from an array of long integers.
96
97\item[EC_word list(int n, const char*)]\ \\
98	Construct an {\eclipse} list of length n from an array of chars.
99
100\item[EC_word list(int n, const double*)]\ \\
101	Construct an {\eclipse} list of length n from an array of doubles.
102
103\item[EC_word array(int, const double*)]\ \\
104	creates an {\eclipse} array (a structure with functor \nil of
105	appropriate arity) of doubles from the given C++ array.
106	The data is copied.
107
108\item[EC_word matrix(int rows, int cols, const double*)]\ \\
109	creates an {\eclipse} array (size rows) of arrays (size cols) of doubles
110	from the given C++ array.  The data is copied.
111
112\item[EC_word handle(const t_ext_type *cl, const t_ext_ptr data)]\ \\
113	Construct an {\eclipse} handle for external data, attaching the
114	given method table.
115
116\item[EC_word newvar()]\ \\
117	Construct a fresh {\eclipse} variable.
118
119\item[EC_word nil()]\ \\
120	Construct the empty list \nil.
121\end{description}
122
123\section{Decomposing {\eclipse} terms in C++}
124The following methods type-check an {\eclipse} term and
125retrieve its contents if it is of the correct type.
126The return code is EC_succeed for successful conversion,
127an error code otherwise.
128\begin{description}
129\item[int EC_word::is_atom(EC_atom *)]\ \\
130	checks whether the {\eclipse} pword is an atom, and if so,
131	return its atom identifier.
132
133\item[int EC_word::is_string(char **)]\ \\
134	checks whether the EC_word is a string (or atom) and converts
135	it to a C++ string. This string is volatile, ie.\ it should be copied
136	when it is required to survive resuming of {\eclipse}.
137
138\item[int EC_word::is_string(char **, long *)]\ \\
139	checks whether the EC_word is a string (or atom) and converts
140	it to a C++ string. This string is volatile, ie.\ it should be copied
141	when it is required to survive resuming of {\eclipse}.
142	The string's length is returned in the second argument.
143
144\item[int EC_word::is_long(long *)]\ \\
145	checks whether the EC_word is an integer that fits into
146	a C++ long, and if so, stores it via the pointer provided.
147
148\item[int EC_word::is_long_long(long long *)]\ \\
149	checks whether the EC_word is an integer that fits into
150	a C++ long long, and if so, stores it via the pointer provided.
151
152\item[int EC_word::is_double(double *)]\ \\
153	checks whether the EC_word is a floating point number,
154	and if so, returns it as an C++ double.
155
156\item[int EC_word::is_list(EC_word\&,EC_word\&)]\ \\
157	checks whether the EC_word is a list and if so, returns its head and tail.
158
159\item[int EC_word::is_nil()]\ \\
160	checks whether the EC_word is nil, the empty list.
161
162\item[int EC_word::functor(EC_functor *)]\ \\
163	checks whether the EC_word is a compound term and if so,
164	returns its functor.
165
166\item[int EC_word::arg(const int,EC_word\&)]\ \\
167	checks whether the EC_word is a compound term and if so,
168	returns its nth argument.
169
170\item[int EC_word::arity()]\ \\
171	returns the arity of an EC_word if it is a compound term,
172	zero otherwise.
173
174\item[int EC_word::is_handle(const t_ext_type *, t_ext_ptr *)]\ \\
175	checks whether the EC_word is a handle whose method table
176	matches the given one, and if so, the data pointer is returned.
177
178\item[int EC_word::is_var()]\ \\
179	checks whether the EC_word is a variable.
180	Returns EC_succeed if so, EC_fail otherwise.
181\end{description}
182
183
184\section{Referring to {\eclipse} terms from C++}
185The data types EC_refs and EC_ref provide a means to have non-volatile
186references to {\eclipse} data from within C++ data structures. However,
187it must be kept in mind that {\eclipse} data structures are nevertheless
188subject to backtracking, which means they may be reset to an earlier
189status when the search engine requires it. Creating a reference to
190a data structure does not change that in any way. In particular, 
191when the search engine fails beyond the state where the reference was
192set up, the reference disappears and is also reset to its earlier value.
193\begin{description}
194\item[EC_refs(int n)]\ \\
195	create a data structure capable of holding n non-volatile
196	references to {\eclipse} data items. They are each initialised
197	with a freshly created {\eclipse} variable.
198
199\item[EC_refs(int n,EC_word pw)]\ \\
200	create a data structure capable of holding n non-volatile
201	references to {\eclipse} data items. They are all initialised
202	with the value pw, which must be of a simple type.
203
204\item[\tld EC_refs()]\ \\
205	destroy the {\eclipse} references. It is important that this
206	is done, otherwise the {\eclipse} garbage collector will not
207	be able to free the references data structures, which may
208	eventually lead to memory overflow.
209
210\item[EC_word EC_refs::operator\nil(int i)]\ \\
211	return the {\eclipse} term referred to by the i'th reference.
212
213\item[void EC_refs::set(int i, EC_word new)]\ \\
214	assign the term new to the i'th reference.
215	This is a backtrackable operation very similar to
216	\bipref{setarg/3}{../bips/kernel/termmanip/setarg-3.html}.
217
218\item[EC_word list(EC_refs\&)]\ \\
219	creates an {\eclipse} list containing all the elements of the EC_refs.
220
221\item[EC_ref()]\ \\
222\item[EC_ref(EC_word pw)]\ \\
223\item[\tld EC_ref()]\ \\
224	analogous to the corresponding EC_refs constructors/destructor.
225
226\item[EC_ref\& operator=(const EC_word)]\ \\
227	assign a value to the EC_ref.
228
229\item[EC_word(const EC_ref\&)]\ \\
230	retrieves the {\eclipse} term referenced by the EC_ref.
231\end{description}
232
233\section{Passing Data to and from External Predicates in C++}
234These two functions are only meaningful inside C++ functions that have been
235called from {\eclipse} as external predicates.
236\begin{description}
237\item[EC_word		EC_arg(int i)]\ \\
238	If inside a C++ function called from {\eclipse}, this returns the
239	i'th argument of the call.
240
241\item[int		unify(EC_word,EC_word)]\ \\
242	Unify the two given pwords. Note that, if attributed variables
243	are involved in the unification, the associated unification handlers
244	as well as subsequent waking will only happen once control is
245	returned to {\eclipse}.
246
247\item[int		EC_word::unify(EC_word)]\ \\
248	Similar, but a method of EC_word.
249\end{description}
250
251\section{Operations on {\eclipse} Data}
252Interfaces to some basic operations on {\eclipse} Data.
253\begin{description}
254\item[int		compare(const EC_word\& pw1, const EC_word\& pw2)]\ \\
255	Similar to the compare/3 built-in predicate:
256	returns 0 if the arguments are identical,
257	a negative number if pw1 is smaller than pw2,
258	and a positive number if pw1 is greater than pw2
259	in the standard term ordering.
260
261\item[int		EC_word::schedule_suspensions(int)]\ \\
262	Similar to the
263	\bipref{schedule_suspensions/2}{../bips/kernel/suspensions/schedule_suspensions-2.html}
264	built-in predicate.
265	Waking will only happen once control is returned to {\eclipse}
266	and the wake/0 predicate is invoked.
267	Return code is EC_succeed or an error code.
268
269\item[pword		EC_word::free_handle(const t_ext_type*)]\ \\
270	checks whether the EC_word is an {\eclipse} external data handle of the
271	expected type, and calls its free-method. After doing that, the
272	handle is stale and cannot be used any longer. Calling this method
273	on an already stale handle silently succeeds.
274	Return code is EC_succeed or an error code.
275
276\end{description}
277
278
279
280\section{Initialising and Shutting Down the {\eclipse} Subsystem}
281These are the functions needed to embed {\eclipse} into a C++ main program.
282\begin{description}
283\item[int		ec_init()]\ \\
284	Initialise the {\eclipse} engine. This is required before any other
285	functions of this interface can be used.
286
287\item[int		ec_cleanup()]\ \\
288	Shutdown the {\eclipse} engine.
289\end{description}
290
291
292\section{Passing Control and Data to {\eclipse} from C++}
293These are the functions needed to embed {\eclipse} into C++ code.
294\begin{description}
295\item[void post_goal(const EC_word)]\ \\
296\item[void post_goal(const char *)]\ \\
297	post a goal (constraint) that will be executed when \eclipse
298	is resumed.
299
300\item[int EC_resume(EC_word FromC, EC_ref\& ToC)]\ \\
301\item[int EC_resume(EC_word FromC)]\ \\
302\item[int EC_resume()]\ \\
303	resume execution of the {\eclipse} engine: All posted goals will
304	be executed. The return value will be EC_succeed if the goals succeed
305	(in this case the ToC argument returns a cut value that can be
306	used to discard alternative solutions). EC_fail is returned if the
307	goals fail, and EC_yield if control was yielded because of a
308	yield/2 predicate call in the {\eclipse} code (in this case, ToC
309	contains the data passed by the first argument of yield/2).
310	If a writable queue stream with yield-option
311	(see \bipref{open/4}{../bips/kernel/iostream/open-4.html})
312	was flushed, the return value is PFLUSHIO and ToC contains the
313	associated stream number.
314	If there was an attempt to read from an empty queue stream with
315	yield-option, the return value is PWAITIO and ToC contains the
316	associated stream number.
317	Moreover, if the previous EC_resume yielded
318	due to a yield/2 predicate call, The term FromC is passed
319	as input into the second argument of yield/2 on resuming.
320
321\item[void EC_ref::cut_to()]\ \\
322	Should be applied to the ToC cut return value of an EC_resume().
323	Cut all choicepoints created by the batch of goals whose execution
324	succeeded.
325
326\item[int post_event(EC_word Name)]\ \\
327	Post an event to the {\eclipse} engine. This will lead to the
328	execution of the corresponding event handler once the \eclipse
329	execution is resumed. See also \bipref{event/1}{../bips/kernel/event/event-1.html} and the User Manual
330	chapter on event handling for more information. Name should be 
331	an {\eclipse} atom.
332\end{description}
333
334%HEVEA\cutend
335