Kkaempf KnowledgeSharing Swig
Kkaempf KnowledgeSharing Swig
Building more powerful C/C++ programs Make C libraries 'object oriented' Rapid prototyping and debugging Systems integration Construction of scripting language extension modules
About SWIG
Homepage: http://www.swig.org
# zypper in swig
History
Initially started in July, 1995 at Los Alamos National Laboratory. First alpha release: February, 1996. Latest release: April 7, 2008. SWIG-1.3.35
Active development
3-4 releases per year
Supported languages
Allegro Common Lisp CFFI (Common Lisp)
CLisp
Octave
Library lib.so
demo.py Python
Using SWIG
Running SWIG
gcc -shared
Running
irb irb(main):001:0> require "example" => true irb(main):002:0> s = Example::Solver.new => #<Example::Solver:0x7ffd300d4de8>
9
10
11
Example (Python)
(taken from libyui-bindings)
YaST2/yui/YUI.h
class YUI { ... static YWidgetFactory * widgetFactory(); ... }
demo.py
import yui factory = yui.YUI.widgetFactory() dialog = factory.createPopupDialog() vbox = factory.createVBox( dialog ) factory.createLabel( vbox, "Hello, World!" ) factory.createPushButton( vbox,
yui.i
%module yui %{ #include "YaST2/yui/YUI.h" %} %include YUI.h
12
Perl
use yui; my $factory = yui::YUI::widgetFactory; my $dialog = $factory->createPopupDialog; my $vbox = $factory->createVBox( $dialog ); $factory->createLabel( $vbox, "Hello, World!" );
13
Comparing objects
SWIG wraps pointers to structs/classes, resulting in target languages objects (Python: PyObject*, Ruby: VALUE) 'a == b' compares PyObject* (resp. VALUE), not the wrapped C++ object pointer
Object ownership
No explicit 'free' in e.g. Ruby and Python
14
Exposure
Hiding elements
%ignore solver::noupdate; %include satsolver/solver.h
Hiding everything
typedef struct solver {} Solver; %extend Solver { ...
16
Memory management
Complex types (struct/class) as pointers SWIG runs constructor ('malloc (sizeof struct)') Might not be useful
%nodefault solver;
Explicit constructor/destructor
%extend Solver { Solver( Pool *pool, Repo *installed = NULL ) { return solver_create( pool, installed ); } ~Solver() { solver_free( $self ); }
17
Making C object-oriented
(Ruby)
solver = Solver.new solver_solve solver, job solver.solve job
# Bad # Good
18
19
Useful commands
Renaming
%rename("to_s") asString(); %rename( "name=" ) set_name( const char *name ); %rename("empty?") empty();
Aliasing
%alias get "[]";
Constants
%constant int Script = C_CONSTANT;
Defines
%define YUILogComponent "bindings" %enddef %define %macro(PARAMETER) ...
20
Type conversions
SWIG has default conversions for most types Look at the SWIG 'library'
/usr/share/swig/<version> %include carray.i
Typemaps
#if defined(SWIGRUBY) %typemap(in) (int bflag) { $1 = RTEST( $input ); } %typemap(out) int problems_found "$result = ($1 != 0) ? Qtrue : Qfalse;"; %rename("problems?") problems_found(); #endif
21
Target specifics
Bypassing SWIG type conversion Use target-specific types Ruby: VALUE Python: PyObject *
Example
%rename( "attr?" ) attr_exists( VALUE attrname ); VALUE attr_exists( VALUE attrname ) { ...
22
Generating Documentation
SWIG can generate target-specific documentation e.g. rdoc for Ruby, pydoc for Python Enable with %feature("autodoc","1"); Converts C-style comments in .i files Needs fixing ...
23
Inversion of control
Inversion of control
Binary Daemon
'call'
PyObject_CallObject() PyInitialize() PyImport_ImportModule()
dlopen()
Python
demo.py
25
SWIG is a tool, use it wisely Take the (script language) programmers view
How should it look in Python/Ruby/Perl/... ?
Tweak the bindings, not the target language Look at other SWIG code SWIG is very well documented
But not without bugs ...
26
27
General Disclaimer
This document is not to be construed as a promise by any participating company to develop, deliver, or market a product. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. Novell, Inc. makes no representations or warranties with respect to the contents of this document, and specifically disclaims any express or implied warranties of merchantability or fitness for any particular purpose. The development, release, and timing of features or functionality described for Novell products remains at the sole discretion of Novell. Further, Novell, Inc. reserves the right to revise this document and to make changes to its content, at any time, without obligation to notify any person or entity of such revisions or changes. All Novell marks referenced in this presentation are trademarks or registered trademarks of Novell, Inc. in the United States and other countries. All third-party trademarks are the property of their respective owners.