1
0
Fork 0
cl-sites/guile.html_node/Dia-Primitives.html

127 lines
6 KiB
HTML
Raw Normal View History

2024-12-17 12:49:28 +01:00
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>Dia Primitives (Guile Reference Manual)</title>
<meta name="description" content="Dia Primitives (Guile Reference Manual)">
<meta name="keywords" content="Dia Primitives (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Extending-Dia.html" rel="up" title="Extending Dia">
<link href="Dia-Hook.html" rel="next" title="Dia Hook">
<link href="Dia-Objects.html" rel="prev" title="Dia Objects">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsubsection-level-extent" id="Dia-Primitives">
<div class="nav-panel">
<p>
Next: <a href="Dia-Hook.html" accesskey="n" rel="next">Providing a Hook for the Evaluation of Scheme Code</a>, Previous: <a href="Dia-Objects.html" accesskey="p" rel="prev">How to Represent Dia Data in Scheme</a>, Up: <a href="Extending-Dia.html" accesskey="u" rel="up">How One Might Extend Dia Using Guile</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsubsection" id="Writing-Guile-Primitives-for-Dia"><span>5.7.1.4 Writing Guile Primitives for Dia<a class="copiable-link" href="#Writing-Guile-Primitives-for-Dia"> &para;</a></span></h4>
<p>Once the details of object representation are decided, writing the
primitive function code that you need is usually straightforward.
</p>
<p>A primitive is simply a C function whose arguments and return value are
all of type <code class="code">SCM</code>, and whose body does whatever you want it to do.
As an example, here is a possible implementation of the <code class="code">square?</code>
primitive:
</p>
<div class="example lisp">
<pre class="lisp-preformatted">static SCM square_p (SCM shape)
{
struct dia_guile_shape * guile_shape;
/* Check that arg is really a shape object. */
scm_assert_foreign_object_type (shape_type, shape);
/* Access Scheme-specific shape structure. */
guile_shape = scm_foreign_object_ref (shape, 0);
/* Find out if underlying shape exists and is a
square; return answer as a Scheme boolean. */
return scm_from_bool (guile_shape-&gt;c_shape &amp;&amp;
(guile_shape-&gt;c_shape-&gt;type == DIA_SQUARE));
}
</pre></div>
<p>Notice how easy it is to chain through from the <code class="code">SCM shape</code>
parameter that <code class="code">square_p</code> receives &mdash; which is a foreign object
&mdash; to the Scheme-specific structure inside the foreign object, and
thence to the underlying C structure for the shape.
</p>
<p>In this code, <code class="code">scm_assert_foreign_object_type</code>,
<code class="code">scm_foreign_object_ref</code>, and <code class="code">scm_from_bool</code> are from the
standard Guile API. We assume that <code class="code">shape_type</code> was given to us
when we made the shape foreign object type, using
<code class="code">scm_make_foreign_object_type</code>. The call to
<code class="code">scm_assert_foreign_object_type</code> ensures that <var class="var">shape</var> is indeed
a shape. This is needed to guard against Scheme code using the
<code class="code">square?</code> procedure incorrectly, as in <code class="code">(square? &quot;hello&quot;)</code>;
Scheme&rsquo;s latent typing means that usage errors like this must be caught
at run time.
</p>
<p>Having written the C code for your primitives, you need to make them
available as Scheme procedures by calling the <code class="code">scm_c_define_gsubr</code>
function. <code class="code">scm_c_define_gsubr</code> (see <a class="pxref" href="Primitive-Procedures.html">Primitive Procedures</a>)
takes arguments that specify the Scheme-level name for the primitive and
how many required, optional and rest arguments it can accept. The
<code class="code">square?</code> primitive always requires exactly one argument, so the
call to make it available in Scheme reads like this:
</p>
<div class="example lisp">
<pre class="lisp-preformatted">scm_c_define_gsubr (&quot;square?&quot;, 1, 0, 0, square_p);
</pre></div>
<p>For where to put this call, see the subsection after next on the
structure of Guile-enabled code (see <a class="pxref" href="Dia-Structure.html">Top-level Structure of Guile-enabled Dia</a>).
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Dia-Hook.html">Providing a Hook for the Evaluation of Scheme Code</a>, Previous: <a href="Dia-Objects.html">How to Represent Dia Data in Scheme</a>, Up: <a href="Extending-Dia.html">How One Might Extend Dia Using Guile</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>