"Fossies" - the Fresh Open Source Software Archive

Member "swig-4.0.0/Doc/Manual/Pike.html" (26 Apr 2019, 6305 Bytes) of package /linux/privat/swig-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    2 <html>
    3 <head>
    4 <title>SWIG and Pike</title>
    5 <link rel="stylesheet" type="text/css" href="style.css">
    6 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    7 </head>
    8 
    9 <body bgcolor="#ffffff">
   10 <H1><a name="Pike">37 SWIG and Pike</a></H1>
   11 <!-- INDEX -->
   12 <div class="sectiontoc">
   13 <ul>
   14 <li><a href="#Pike_nn2">Preliminaries</a>
   15 <ul>
   16 <li><a href="#Pike_nn3">Running SWIG</a>
   17 <li><a href="#Pike_nn4">Getting the right header files</a>
   18 <li><a href="#Pike_nn5">Using your module</a>
   19 </ul>
   20 <li><a href="#Pike_nn6">Basic C/C++ Mapping</a>
   21 <ul>
   22 <li><a href="#Pike_nn7">Modules</a>
   23 <li><a href="#Pike_nn8">Functions</a>
   24 <li><a href="#Pike_nn9">Global variables</a>
   25 <li><a href="#Pike_nn10">Constants and enumerated types</a>
   26 <li><a href="#Pike_nn11">Constructors and Destructors</a>
   27 <li><a href="#Pike_nn12">Static Members</a>
   28 </ul>
   29 </ul>
   30 </div>
   31 <!-- INDEX -->
   32 
   33 
   34 
   35 <p>
   36 This chapter describes SWIG support for Pike. As of this writing, the
   37 SWIG  Pike module is still under development and is not considered
   38 ready for prime  time. The Pike module is being developed against the
   39 Pike 7.4.10 release  and may not be compatible with previous versions
   40 of Pike.
   41 </p>
   42 
   43 <p>
   44 This chapter covers most SWIG features, but certain low-level details
   45 are  covered in less depth than in earlier chapters.  At the very
   46 least, make sure you read the "<a href="SWIG.html#SWIG">SWIG Basics</a>"
   47 chapter.<br>
   48 </p>
   49 
   50 <H2><a name="Pike_nn2">37.1 Preliminaries</a></H2>
   51 
   52 
   53 <H3><a name="Pike_nn3">37.1.1 Running SWIG</a></H3>
   54 
   55 
   56 <p>
   57 Suppose that you defined a SWIG module such as the following:
   58 </p>
   59 
   60 <div class="code">
   61   <pre>%module example<br><br>%{<br>#include "example.h"<br>%}<br><br>int fact(int n);<br></pre>
   62 </div>
   63 
   64 <p>
   65 To build a C extension module for Pike, run SWIG using the <tt>-pike</tt> option :
   66 </p>
   67 
   68 <div class="code">
   69   <pre>$ <b>swig -pike example.i</b><br></pre>
   70 </div>
   71 
   72 <p>
   73 If you're building a C++ extension, be sure to add the <tt>-c++</tt> option:
   74 </p>
   75 
   76 <div class="code">
   77   <pre>$ <b>swig -c++ -pike example.i</b><br></pre>
   78 </div>
   79 
   80 <p>
   81 This creates a single source file named <tt>example_wrap.c</tt> (or <tt>example_wrap.cxx</tt>, if you
   82 ran SWIG with the <tt>-c++</tt> option).
   83 The SWIG-generated source file contains the low-level wrappers that need
   84 to be compiled and linked with the rest of your C/C++ application to
   85 create an extension module.
   86 </p>
   87 
   88 <p>
   89 The name of the wrapper file is derived from the name of the input
   90 file.  For example, if the input file is <tt>example.i</tt>, the name
   91 of the wrapper file is <tt>example_wrap.c</tt>. To change this, you
   92 can use the <tt>-o</tt> option:
   93 </p>
   94 
   95 <div class="code">
   96   <pre>$ <b>swig -pike -o pseudonym.c example.i</b><br></pre>
   97 </div>
   98 <H3><a name="Pike_nn4">37.1.2 Getting the right header files</a></H3>
   99 
  100 
  101 <p>
  102 In order to compile the C/C++ wrappers, the compiler needs to know the
  103 path to the Pike header files. These files are usually contained in a
  104 directory such as
  105 </p>
  106 
  107 <div class="code">
  108   <pre>/usr/local/pike/7.4.10/include/pike<br></pre>
  109 </div>
  110 
  111 <p>
  112 There doesn't seem to be any way to get Pike itself to reveal the
  113 location of these files, so you may need to hunt around for them.
  114 You're looking for files with the names <tt>global.h</tt>, <tt>program.h</tt>
  115 and so on.
  116 </p>
  117 
  118 <H3><a name="Pike_nn5">37.1.3 Using your module</a></H3>
  119 
  120 
  121 <p>
  122 To use your module, simply use Pike's <tt>import</tt> statement:
  123 </p>
  124 
  125 <div class="code"><pre>
  126 $ <b>pike</b>
  127 Pike v7.4 release 10 running Hilfe v3.5 (Incremental Pike Frontend)
  128 &gt; <b>import example;</b>
  129 &gt; <b>fact(4);</b>
  130 (1) Result: 24
  131 </pre></div>
  132 
  133 <H2><a name="Pike_nn6">37.2 Basic C/C++ Mapping</a></H2>
  134 
  135 
  136 <H3><a name="Pike_nn7">37.2.1 Modules</a></H3>
  137 
  138 
  139 <p>
  140 All of the code for a given SWIG module is wrapped into a single Pike
  141 module. Since the name of the shared library that implements your
  142 module ultimately determines the module's name (as far as Pike is
  143 concerned), SWIG's <tt>%module</tt> directive doesn't really have any
  144 significance.
  145 </p>
  146 
  147 <H3><a name="Pike_nn8">37.2.2 Functions</a></H3>
  148 
  149 
  150 <p>
  151 Global functions are wrapped as new Pike built-in functions. For
  152 example,
  153 </p>
  154 
  155 <div class="code"><pre>
  156 %module example
  157 
  158 int fact(int n);
  159 </pre></div>
  160 
  161 <p>
  162 creates a new built-in function <tt>example.fact(n)</tt> that works
  163 exactly as you'd expect it to:
  164 </p>
  165 
  166 <div class="code"><pre>
  167 &gt; <b>import example;</b>
  168 &gt; <b>fact(4);</b>
  169 (1) Result: 24
  170 </pre></div>
  171 
  172 <H3><a name="Pike_nn9">37.2.3 Global variables</a></H3>
  173 
  174 
  175 <p>
  176 Global variables are currently wrapped as a pair of functions, one to get
  177 the current value of the variable and another to set it. For example, the
  178 declaration
  179 </p>
  180 
  181 <div class="code"><pre>
  182 %module example
  183 
  184 double Foo;
  185 </pre></div>
  186 
  187 <p>
  188 will result in two functions, <tt>Foo_get()</tt> and <tt>Foo_set()</tt>:
  189 </p>
  190 
  191 <div class="code"><pre>
  192 &gt; <b>import example;</b>
  193 &gt; <b>Foo_get();</b>
  194 (1) Result: 3.000000
  195 &gt; <b>Foo_set(3.14159);</b>
  196 (2) Result: 0
  197 &gt; <b>Foo_get();</b>
  198 (3) Result: 3.141590
  199 </pre></div>
  200 
  201 <H3><a name="Pike_nn10">37.2.4 Constants and enumerated types</a></H3>
  202 
  203 
  204 <p>
  205 Enumerated types in C/C++ declarations are wrapped as Pike constants,
  206 not as Pike enums.
  207 </p>
  208 
  209 <H3><a name="Pike_nn11">37.2.5 Constructors and Destructors</a></H3>
  210 
  211 
  212 <p>
  213 Constructors are wrapped as <tt>create()</tt> methods, and destructors are
  214 wrapped as <tt>destroy()</tt> methods, for Pike classes.
  215 </p>
  216 
  217 <H3><a name="Pike_nn12">37.2.6 Static Members</a></H3>
  218 
  219 
  220 <p>
  221 Since Pike doesn't support static methods or data for Pike classes, static
  222 member functions in your C++ classes are wrapped as regular functions and
  223 static member variables are wrapped as pairs of functions (one to get the
  224 value of the static member variable, and another to set it). The names of
  225 these functions are prepended with the name of the class.
  226 For example, given this C++ class declaration:
  227 </p>
  228 
  229 <div class="code"><pre>
  230 class Shape
  231 {
  232 public:
  233   static void print();
  234   static int nshapes;
  235 };
  236 </pre></div>
  237 
  238 <p>
  239 SWIG will generate a <tt>Shape_print()</tt> method that invokes the static
  240 <tt>Shape::print()</tt> member function, as well as a pair of methods,
  241 <tt>Shape_nshapes_get()</tt> and <tt>Shape_nshapes_set()</tt>, to get and set
  242 the value of <tt>Shape::nshapes</tt>.
  243 </p>
  244 
  245 </body>
  246 </html>