1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/* */
%insert("header") "swiglabels.swg"
%insert("header") "swigerrors.swg"
%insert("init") "swiginit.swg"
%insert("runtime") "swigrun.swg"
%insert("runtime") "rrun.swg"
%init %{
SWIGEXPORT void SWIG_init(void) {
%}
%include <rkw.swg>
#define %Rruntime %insert("s")
#define SWIG_Object SEXP
#define VOID_Object R_NilValue
#define %append_output(obj) SET_VECTOR_ELT($result, $n, obj)
%define %set_constant(name, obj) %begin_block
SEXP _obj = obj;
assign(name, _obj);
%end_block %enddef
%define %raise(obj,type,desc)
return R_NilValue;
%enddef
%insert("sinit") "srun.swg"
%insert("sinitroutine") %{
SWIG_init();
SWIG_InitializeModule(0);
%}
%include <typemaps/swigmacros.swg>
%typemap(in) (double *x, int len) %{
$1 = REAL(x);
$2 = Rf_length(x);
%}
/* XXX
Need to worry about inheritance, e.g. if B extends A
and we are looking for an A[], then B elements are okay.
*/
%typemap(scheck) SWIGTYPE[ANY]
%{
# assert(length($input) > $1_dim0)
assert(all(sapply($input, class) == "$R_class"));
%}
%typemap(out) void "";
%typemap(in) int *, int[ANY],
signed int *, signed int[ANY],
unsigned int *, unsigned int[ANY],
short *, short[ANY],
signed short *, signed short[ANY],
unsigned short *, unsigned short[ANY],
long *, long[ANY],
signed long *, signed long[ANY],
unsigned long *, unsigned long[ANY],
long long *, long long[ANY],
signed long long *, signed long long[ANY],
unsigned long long *, unsigned long long[ANY]
{
{ int _rswigi;
int _rswiglen = LENGTH($input);
$1 = %static_cast(calloc(sizeof($1_basetype), _rswiglen), $1_ltype);
for (_rswigi=0; _rswigi< _rswiglen; _rswigi++) {
$1[_rswigi] = INTEGER($input)[_rswigi];
}
}
}
%typemap(in) float *, float[ANY],
double *, double[ANY]
{
{ int _rswigi;
int _rswiglen = LENGTH($input);
$1 = %static_cast(calloc(sizeof($1_basetype), _rswiglen), $1_ltype);
for (_rswigi=0; _rswigi<_rswiglen; _rswigi++) {
$1[_rswigi] = REAL($input)[_rswigi];
}
}
}
%typemap(freearg,noblock=1) int *, int[ANY],
signed int *, signed int[ANY],
unsigned int *, unsigned int[ANY],
short *, short[ANY],
signed short *, signed short[ANY],
unsigned short *, unsigned short[ANY],
long *, long[ANY],
signed long *, signed long[ANY],
unsigned long *, unsigned long[ANY],
long long *, long long[ANY],
signed long long *, signed long long[ANY],
unsigned long long *, unsigned long long[ANY],
float *, float[ANY],
double *, double[ANY]
%{
free($1);
%}
%typemap(freearg, noblock=1) int *OUTPUT,
signed int *OUTPUT,
unsigned int *OUTPUT,
short *OUTPUT,
signed short *OUTPUT,
unsigned short *OUTPUT,
long *OUTPUT,
signed long *OUTPUT,
unsigned long *OUTPUT,
long long *OUTPUT,
signed long long *OUTPUT,
unsigned long long *OUTPUT,
float *OUTPUT,
double *OUTPUT,
char *OUTPUT,
signed char *OUTPUT,
unsigned char *OUTPUT
{}
/* Should we recycle to make the length correct.
And warn if length() > the dimension.
*/
%typemap(scheck) SWIGTYPE [ANY] %{
# assert(length($input) >= $1_dim0)
%}
/* Handling vector case to avoid warnings,
although we just use the first one. */
%typemap(scheck) unsigned int %{
assert(length($input) == 1 && $input >= 0, "All values must be non-negative");
%}
%typemap(scheck) int, long %{
if(length($input) > 1) {
warning("using only the first element of $input");
};
%}
%include <typemaps/fragments.swg>
%include <rfragments.swg>
%include <ropers.swg>
%include <typemaps/swigtypemaps.swg>
%include <rtype.swg>
%typemap(in,noblock=1) enum SWIGTYPE[ANY] {
$1 = %reinterpret_cast(INTEGER($input), $1_ltype);
}
%typemap(in,noblock=1,fragment="SWIG_strdup") char * {
$1 = %reinterpret_cast(SWIG_strdup(CHAR(STRING_ELT($input, 0))), $1_ltype);
}
%typemap(freearg,noblock=1) char * {
free($1);
}
%typemap(in,noblock=1,fragment="SWIG_strdup") char *[ANY] {
$1 = %reinterpret_cast(SWIG_strdup(CHAR(STRING_ELT($input, 0))), $1_ltype);
}
%typemap(freearg,noblock=1) char *[ANY] {
free($1);
}
%typemap(in,noblock=1,fragment="SWIG_strdup") char[ANY] {
$1 = SWIG_strdup(CHAR(STRING_ELT($input, 0)));
}
%typemap(freearg,noblock=1) char[ANY] {
free($1);
}
%typemap(in,noblock=1,fragment="SWIG_strdup") char[] {
$1 = SWIG_strdup(CHAR(STRING_ELT($input, 0)));
}
%typemap(freearg,noblock=1) char[] {
free($1);
}
%typemap(memberin) char[] %{
if ($input) strcpy($1, $input);
else
strcpy($1, "");
%}
%typemap(globalin) char[] %{
if ($input) strcpy($1, $input);
else
strcpy($1, "");
%}
%typemap(out,noblock=1) char *
{ $result = $1 ? Rf_mkString(%reinterpret_cast($1,char *)) : R_NilValue; }
%typemap(in,noblock=1) char {
$1 = %static_cast(CHAR(STRING_ELT($input, 0))[0],$1_ltype);
}
%typemap(out) char
{
char tmp[2] = "x";
tmp[0] = $1;
$result = Rf_mkString(tmp);
}
%typemap(in,noblock=1) int, long
{
$1 = %static_cast(INTEGER($input)[0], $1_ltype);
}
%typemap(out,noblock=1) int, long
"$result = Rf_ScalarInteger($1);";
%typemap(in,noblock=1) bool
"$1 = LOGICAL($input)[0] ? true : false;";
%typemap(out,noblock=1) bool
"$result = Rf_ScalarLogical($1);";
%typemap(in,noblock=1)
float,
double
{
$1 = %static_cast(REAL($input)[0], $1_ltype);
}
/* Why is this here ? */
/* %typemap(out,noblock=1) unsigned int *
"$result = ScalarReal(*($1));"; */
%Rruntime %{
setMethod('[', "ExternalReference",
function(x,i,j, ..., drop=TRUE)
if (!is.null(x$"__getitem__"))
sapply(i, function(n) x$"__getitem__"(i=as.integer(n-1))))
setMethod('[<-' , "ExternalReference",
function(x,i,j, ..., value)
if (!is.null(x$"__setitem__")) {
sapply(1:length(i), function(n)
x$"__setitem__"(i=as.integer(i[n]-1), x=value[n]))
x
})
setAs('ExternalReference', 'character',
function(from) {if (!is.null(from$"__str__")) from$"__str__"()})
suppressMessages(suppressWarnings(setMethod('print', 'ExternalReference',
function(x) {print(as(x, "character"))})))
%}
|