PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 1ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/ruby/exception_class/example.i

#
Swig | 46 lines | 8 code | 8 blank | 30 comment | 0 complexity | ddde88c24ebf46114a2d40753fa4791d MD5 | raw file
 1/* This example illustrates the use of the %exceptionclass feature.  By
 2   default, if a method has a throws specification then SWIG will generate
 3   code to catch the exception and pass it on the scripting language.
 4   
 5   If a method does not have a throws specification, but does throw 
 6   an exception, then the %exceptionclass feature can be used to tell
 7   SWIG about the exception class so it can be properly added to Ruby.
 8   This is done by making the exception class inherit from rb_eRuntimeError.*/
 9
10%module example
11
12%{
13#include "example.h"
14%}
15
16
17/* The EmpytError doesn't appear in a throw declaration, and hence
18  we need to tell SWIG that the dequeue method throws it.  This can
19  now be done via the %catchs feature. */
20%catches(EmptyError) *::dequeue();
21
22
23/* What the catches clause is doing under the covers is this:
24
25%exceptionclass EmptyError;
26
27%exception *::dequeue {
28   try {
29      $action
30   } catch(EmptyError& e) {
31     // Create a new instance of the EmptyError, wrap it as a Ruby object that Ruby owns,
32     // and return it as the exception.  For this to work EmtpyError must inherit from
33     // a standard Ruby exception class such as rb_eRuntimeError.  SWIG automatically does
34     // this when the class is marked as %exceptionclass or is a throws specification.
35     %raise(SWIG_NewPointerObj(new EmptyError(e),SWIGTYPE_p_EmptyError, SWIG_POINTER_OWN), 
36            "EmptyError", SWIGTYPE_p_EmptyError);
37   }
38}
39*/
40
41/* Grab the original header file */
42%include "example.h"
43
44/* Instantiate a few templates */
45%template(IntQueue) Queue<int>;
46%template(DoubleQueue) Queue<double>;