4
%insert("header") "swiglabels.swg"
6
%insert("header") "swigerrors.swg"
7
%insert("init") "swiginit.swg"
8
%insert("runtime") "swigrun.swg"
9
%insert("runtime") "rrun.swg"
12
SWIGEXPORT void SWIG_init(void) {
15
#define %Rruntime %insert("s")
17
#define SWIG_Object SEXP
18
#define VOID_Object R_NilValue
20
#define %append_output(obj) SET_VECTOR_ELT($result, $n, obj)
22
%define %set_constant(name, obj) %begin_block
27
%define %raise(obj,type,desc)
31
%insert("sinit") "srun.swg"
33
%insert("sinitroutine") %{
35
SWIG_InitializeModule(0);
38
%include <typemaps/swigmacros.swg>
39
%typemap(in) (double *x, int len) %{
45
Need to worry about inheritance, e.g. if B extends A
46
and we are looking for an A[], then B elements are okay.
48
%typemap(scheck) SWIGTYPE[ANY]
50
# assert(length($input) > $1_dim0)
51
assert(all(sapply($input, class) == "$R_class"))
54
%typemap(out) void "";
56
%typemap(in) int *, int[ANY] %{
60
%typemap(in) double *, double[ANY] %{
64
/* Shoul dwe recycle to make the length correct.
65
And warn if length() > the dimension.
67
%typemap(scheck) SWIGTYPE [ANY] %{
68
# assert(length($input) >= $1_dim0)
71
/* Handling vector case to avoid warnings,
72
although we just use the first one. */
73
%typemap(scheck) unsigned int %{
74
assert(length($input) == 1 && $input >= 0, "All values must be non-negative")
78
%typemap(scheck) int %{
79
if(length($input) > 1) {
80
warning("using only the first element of $input")
85
%include <typemaps/swigmacros.swg>
86
%include <typemaps/fragments.swg>
87
%include <rfragments.swg>
89
%include <typemaps/swigtypemaps.swg>
92
%apply int[ANY] { enum SWIGTYPE[ANY] };
94
%typemap(in,noblock=1) enum SWIGTYPE[ANY] {
95
$1 = %reinterpret_cast(INTEGER($input), $1_ltype);
98
%typemap(in,noblock=1) char* {
99
$1 = %reinterpret_cast(CHAR(STRING_ELT($input, 0)), $1_ltype);
102
%typemap(in,noblock=1) char *[ANY] {
103
$1 = %reinterpret_cast(CHAR(STRING_ELT($input, 0)), $1_ltype);
106
%typemap(in,noblock=1) char[ANY]
107
"$1 = CHAR(STRING_ELT($input, 0));";
109
%typemap(in,noblock=1) char[]
110
"$1 = CHAR(STRING_ELT($input, 0));";
112
%typemap(memberin) char[] %{
113
if ($input) strcpy($1, $input);
118
%typemap(globalin) char[] %{
119
if ($input) strcpy($1, $input);
124
%typemap(out,noblock=1) char*
125
{ $result = $1 ? mkString(%reinterpret_cast($1,char *)) : R_NilValue; }
127
%typemap(in,noblock=1) char {
128
$1 = %static_cast(CHAR(STRING_ELT($input, 0))[0],$1_ltype);
131
%typemap(out,noblock=1) char
135
$result = mkString(tmp);
139
%typemap(in,noblock=1) int {
140
$1 = %static_cast(INTEGER($input)[0], $1_ltype);
143
%typemap(out,noblock=1) int
144
"$result = ScalarInteger($1);";
147
%typemap(in,noblock=1) bool
148
"$1 = LOGICAL($input)[0];";
151
%typemap(out,noblock=1) bool
152
"$result = ScalarLogical($1);";
154
%typemap(in,noblock=1) unsigned int,
160
$1 = %static_cast(REAL($input)[0], $1_ltype);
164
%typemap(out,noblock=1) unsigned int *
165
"$result = ScalarReal(*($1));";
168
setMethod('[', "ExternalReference",
169
function(x,i,j, ..., drop=TRUE)
170
if (!is.null(x$"__getitem__"))
171
sapply(i, function(n) x$"__getitem__"(i=as.integer(n-1))))
173
setMethod('[<-' , "ExternalReference",
174
function(x,i,j, ..., value)
175
if (!is.null(x$"__setitem__")) {
176
sapply(1:length(i), function(n)
177
x$"__setitem__"(i=as.integer(i[n]-1), x=value[n]))
181
setAs('ExternalReference', 'character',
182
function(from) {if (!is.null(from$"__str__")) from$"__str__"()})
184
setMethod('print', 'ExternalReference',
185
function(x) {print(as(x, "character"))})