Give same name to common example
This commit is contained in:
parent
760c008311
commit
35ef98f4d0
4 changed files with 0 additions and 0 deletions
23
Examples/ruby/exceptproxy/Makefile
Normal file
23
Examples/ruby/exceptproxy/Makefile
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
TOP = ../..
|
||||
SWIGEXE = $(TOP)/../swig
|
||||
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
|
||||
CXXSRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
|
||||
check: build
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' ruby_run
|
||||
|
||||
build:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ruby_cpp
|
||||
|
||||
static:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
|
||||
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
|
||||
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' ruby_clean
|
||||
65
Examples/ruby/exceptproxy/example.h
Normal file
65
Examples/ruby/exceptproxy/example.h
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/* File : example.h */
|
||||
|
||||
// A simple exception
|
||||
class EmptyError { };
|
||||
class FullError {
|
||||
public:
|
||||
int maxsize;
|
||||
FullError(int m) : maxsize(m) { }
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
#if __GNUC__ >= 7
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated" // dynamic exception specifications are deprecated in C++11
|
||||
#endif
|
||||
|
||||
template<typename T> class Queue {
|
||||
int maxsize;
|
||||
T *items;
|
||||
int nitems;
|
||||
int last;
|
||||
public:
|
||||
Queue(int size) {
|
||||
maxsize = size;
|
||||
items = new T[size];
|
||||
nitems = 0;
|
||||
last = 0;
|
||||
}
|
||||
~Queue() {
|
||||
delete [] items;
|
||||
}
|
||||
void enqueue(T x) throw(FullError) {
|
||||
if (nitems == maxsize) {
|
||||
throw FullError(maxsize);
|
||||
}
|
||||
items[last] = x;
|
||||
last = (last + 1) % maxsize;
|
||||
nitems++;
|
||||
}
|
||||
T dequeue() {
|
||||
T x;
|
||||
if (nitems == 0) throw EmptyError();
|
||||
x = items[(last + maxsize - nitems) % maxsize];
|
||||
nitems--;
|
||||
return x;
|
||||
}
|
||||
int length() {
|
||||
return nitems;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
#if __GNUC__ >= 7
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
46
Examples/ruby/exceptproxy/example.i
Normal file
46
Examples/ruby/exceptproxy/example.i
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* This example illustrates the use of the %exceptionclass feature. By
|
||||
default, if a method has a throws specification then SWIG will generate
|
||||
code to catch the exception and pass it on the scripting language.
|
||||
|
||||
If a method does not have a throws specification, but does throw
|
||||
an exception, then the %exceptionclass feature can be used to tell
|
||||
SWIG about the exception class so it can be properly added to Ruby.
|
||||
This is done by making the exception class inherit from rb_eRuntimeError.*/
|
||||
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
|
||||
/* The EmpytError doesn't appear in a throw declaration, and hence
|
||||
we need to tell SWIG that the dequeue method throws it. This can
|
||||
now be done via the %catchs feature. */
|
||||
%catches(EmptyError) *::dequeue();
|
||||
|
||||
|
||||
/* What the catches clause is doing under the covers is this:
|
||||
|
||||
%exceptionclass EmptyError;
|
||||
|
||||
%exception *::dequeue {
|
||||
try {
|
||||
$action
|
||||
} catch(EmptyError& e) {
|
||||
// Create a new instance of the EmptyError, wrap it as a Ruby object that Ruby owns,
|
||||
// and return it as the exception. For this to work EmtpyError must inherit from
|
||||
// a standard Ruby exception class such as rb_eRuntimeError. SWIG automatically does
|
||||
// this when the class is marked as %exceptionclass or is a throws specification.
|
||||
%raise(SWIG_NewPointerObj(new EmptyError(e),SWIGTYPE_p_EmptyError, SWIG_POINTER_OWN),
|
||||
"EmptyError", SWIGTYPE_p_EmptyError);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/* Grab the original header file */
|
||||
%include "example.h"
|
||||
|
||||
/* Instantiate a few templates */
|
||||
%template(IntQueue) Queue<int>;
|
||||
%template(DoubleQueue) Queue<double>;
|
||||
45
Examples/ruby/exceptproxy/runme.rb
Normal file
45
Examples/ruby/exceptproxy/runme.rb
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
require 'example'
|
||||
|
||||
q = Example::IntQueue.new(10)
|
||||
|
||||
puts "Inserting items into intQueue"
|
||||
|
||||
begin
|
||||
0.upto(100) do |i|
|
||||
q.enqueue(i)
|
||||
end
|
||||
rescue Example::FullError => e
|
||||
puts "Maxsize is: #{e.maxsize}"
|
||||
end
|
||||
|
||||
puts "Removing items"
|
||||
|
||||
begin
|
||||
loop do
|
||||
q.dequeue()
|
||||
end
|
||||
rescue Example::EmptyError => e
|
||||
## do nothing
|
||||
end
|
||||
|
||||
q = Example::DoubleQueue.new(1000)
|
||||
|
||||
puts "Inserting items into doubleQueue"
|
||||
|
||||
begin
|
||||
0.upto(100) do |i|
|
||||
q.enqueue(i*1.5)
|
||||
end
|
||||
rescue Example::FullError => e
|
||||
puts "Maxsize is: #{e.maxsize}"
|
||||
end
|
||||
|
||||
puts "Removing items"
|
||||
|
||||
begin
|
||||
loop do
|
||||
q.dequeue()
|
||||
end
|
||||
rescue Example::EmptyError => e
|
||||
# do nothing
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue