The great merge
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
6fcc22a1f8
commit
516036631c
1508 changed files with 125983 additions and 44037 deletions
1
SWIG/Examples/ruby/hashargs/.cvsignore
Normal file
1
SWIG/Examples/ruby/hashargs/.cvsignore
Normal file
|
|
@ -0,0 +1 @@
|
|||
example_wrap.c
|
||||
20
SWIG/Examples/ruby/hashargs/Makefile
Executable file
20
SWIG/Examples/ruby/hashargs/Makefile
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ruby
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile ruby_clean
|
||||
|
||||
check: all
|
||||
36
SWIG/Examples/ruby/hashargs/example.i
Executable file
36
SWIG/Examples/ruby/hashargs/example.i
Executable file
|
|
@ -0,0 +1,36 @@
|
|||
%module example
|
||||
|
||||
%typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_ary, int i, VALUE key, VALUE val) {
|
||||
Check_Type($input, T_HASH);
|
||||
$1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
|
||||
$2 = NULL;
|
||||
$3 = NULL;
|
||||
if ($1 > 0) {
|
||||
$2 = (char **) malloc($1*sizeof(char *));
|
||||
$3 = (int *) malloc($1*sizeof(int));
|
||||
keys_ary = rb_funcall($input, rb_intern("keys"), 0, NULL);
|
||||
for (i = 0; i < $1; i++) {
|
||||
key = rb_ary_entry(keys_ary, i);
|
||||
val = rb_hash_aref($input, key);
|
||||
Check_Type(key, T_STRING);
|
||||
Check_Type(val, T_FIXNUM);
|
||||
$2[i] = STR2CSTR(key);
|
||||
$3[i] = NUM2INT(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
%typemap(freearg) (int nattributes, const char **names, const int *values) {
|
||||
free((void *) $2);
|
||||
free((void *) $3);
|
||||
}
|
||||
|
||||
%inline %{
|
||||
void setVitalStats(const char *person, int nattributes, const char **names, const int *values) {
|
||||
int i;
|
||||
printf("Name: %s\n", person);
|
||||
for (i = 0; i < nattributes; i++) {
|
||||
printf(" %s => %d\n", names[i], values[i]);
|
||||
}
|
||||
}
|
||||
%}
|
||||
26
SWIG/Examples/ruby/hashargs/runme.rb
Executable file
26
SWIG/Examples/ruby/hashargs/runme.rb
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
require 'example'
|
||||
|
||||
include Example
|
||||
|
||||
# Pass arguments in as one or more key-value pairs
|
||||
setVitalStats("Fred",
|
||||
'age' => 42,
|
||||
'weight' => 270
|
||||
)
|
||||
|
||||
# The order doesn't matter
|
||||
setVitalStats("Sally",
|
||||
'age' => 29,
|
||||
'weight' => 115,
|
||||
'height' => 72
|
||||
)
|
||||
|
||||
# Can also pass a hash directly
|
||||
params = {
|
||||
'ears' => 2,
|
||||
'eyes' => 2
|
||||
}
|
||||
setVitalStats("Bob", params)
|
||||
|
||||
# An empty hash is fine too
|
||||
setVitalStats("Joe", {})
|
||||
Loading…
Add table
Add a link
Reference in a new issue