PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. If a method does not have a throws specification, but does throw
  5. an exception, then the %exceptionclass feature can be used to tell
  6. SWIG about the exception class so it can be properly added to Ruby.
  7. This is done by making the exception class inherit from rb_eRuntimeError.*/
  8. %module example
  9. %{
  10. #include "example.h"
  11. %}
  12. /* The EmpytError doesn't appear in a throw declaration, and hence
  13. we need to tell SWIG that the dequeue method throws it. This can
  14. now be done via the %catchs feature. */
  15. %catches(EmptyError) *::dequeue();
  16. /* What the catches clause is doing under the covers is this:
  17. %exceptionclass EmptyError;
  18. %exception *::dequeue {
  19. try {
  20. $action
  21. } catch(EmptyError& e) {
  22. // Create a new instance of the EmptyError, wrap it as a Ruby object that Ruby owns,
  23. // and return it as the exception. For this to work EmtpyError must inherit from
  24. // a standard Ruby exception class such as rb_eRuntimeError. SWIG automatically does
  25. // this when the class is marked as %exceptionclass or is a throws specification.
  26. %raise(SWIG_NewPointerObj(new EmptyError(e),SWIGTYPE_p_EmptyError, SWIG_POINTER_OWN),
  27. "EmptyError", SWIGTYPE_p_EmptyError);
  28. }
  29. }
  30. */
  31. /* Grab the original header file */
  32. %include "example.h"
  33. /* Instantiate a few templates */
  34. %template(IntQueue) Queue<int>;
  35. %template(DoubleQueue) Queue<double>;