Home | History | Annotate | Download | only in r
      1 
      2 /* These map the primitive C types to the appropriate R type
      3    for use in class representations. 
      4  */
      5 
      6 %typemap("rtype") int, int *, int &      "integer";
      7 %typemap("rtype") long, long *, long &      "integer";
      8 %typemap("rtype") float, float*, float & "numeric";
      9 %typemap("rtype") double, double*, double & "numeric";
     10 %typemap("rtype") char *, char ** "character";
     11 %typemap("rtype") char            "character";
     12 %typemap("rtype") string, string *, string & "character";
     13 %typemap("rtype") std::string, std::string *, std::string & "character";
     14 %typemap("rtype") bool, bool *    "logical";
     15 %typemap("rtype") enum SWIGTYPE   "character";
     16 %typemap("rtype") enum SWIGTYPE *   "character";
     17 %typemap("rtype") enum SWIGTYPE *const   "character";
     18 %typemap("rtype") enum SWIGTYPE &  "character";
     19 %typemap("rtype") SWIGTYPE * "$R_class";
     20 %typemap("rtype") SWIGTYPE *const "$R_class";
     21 %typemap("rtype") SWIGTYPE & "$R_class";
     22 %typemap("rtype") SWIGTYPE "$&R_class";
     23 
     24 %typemap("rtypecheck") int, int &, long, long &
     25   %{ (is.integer($arg) || is.numeric($arg)) && length($arg) == 1 %}
     26 %typemap("rtypecheck") int *, long *
     27   %{ is.integer($arg) || is.numeric($arg) %}
     28 
     29 
     30 %typemap("rtypecheck") float, double
     31   %{ is.numeric($arg) && length($arg) == 1 %}
     32 %typemap("rtypecheck") float *, double *
     33   %{ is.numeric($arg) %}
     34 
     35 %typemap("rtypecheck") bool, bool &
     36   %{ is.logical($arg) && length($arg) == 1 %}
     37 %typemap("rtypecheck") bool *
     38   %{ is.logical($arg) %}
     39 
     40 /*
     41   Set up type checks to insure overloading precedence.
     42   We would like non pointer items to shadow pointer items, so that 
     43   they get called if length = 1
     44 */
     45 
     46 %typecheck(SWIG_TYPECHECK_BOOL) bool {}
     47 %typecheck(SWIG_TYPECHECK_UINT32) unsigned int {}
     48 %typecheck(SWIG_TYPECHECK_INTEGER) int {}
     49 %typecheck(SWIG_TYPECHECK_FLOAT) float {}
     50 %typecheck(SWIG_TYPECHECK_DOUBLE) double {}
     51 
     52 %typecheck(SWIG_TYPECHECK_BOOL_PTR) bool * {}
     53 %typecheck(SWIG_TYPECHECK_INT32_PTR) int * {}
     54 %typecheck(SWIG_TYPECHECK_FLOAT_PTR) float * {}
     55 %typecheck(SWIG_TYPECHECK_DOUBLE_PTR) double * {}
     56 %typecheck(SWIG_TYPECHECK_CHAR_PTR) char * {}
     57 
     58 %typecheck(SWIG_TYPECHECK_INT32_ARRAY) int[ANY] {}
     59 %typecheck(SWIG_TYPECHECK_FLOAT_ARRAY) float[ANY] {}
     60 %typecheck(SWIG_TYPECHECK_DOUBLE_ARRAY) double [ANY] {}
     61 
     62 /* Have to be careful that as(x, "numeric") is different from as.numeric(x).  
     63    The latter makes a REALSXP, whereas the former leaves an INTSXP as an
     64    INTSXP.
     65 */
     66 
     67 /* Force coercion of integer, since by default R sets all constants to
     68    numeric, which means that you can't directly call a function with an 
     69    integer using an R numercal literal */
     70 
     71 %typemap(scoercein) int, int *, int &
     72   %{  $input = as.integer($input);     %}
     73 %typemap(scoercein) long, long *, long &
     74   %{  $input = as.integer($input);     %}
     75 %typemap(scoercein) float, float*, float &,
     76   double, double *, double &
     77   %{ %}
     78 %typemap(scoercein) char, char *, char &
     79   %{  $input = as($input, "character");     %}
     80 %typemap(scoercein) string, string *, string &
     81   %{  $input = as($input, "character");     %}
     82 %typemap(scoercein) std::string, std::string *, std::string &
     83   %{  $input = as($input, "character");     %}
     84 %typemap(scoercein) enum SWIGTYPE 
     85   %{  $input = enumToInteger($input, "$R_class"); %}
     86 %typemap(scoercein) enum SWIGTYPE &
     87   %{  $input = enumToInteger($input, "$R_class"); %}
     88 %typemap(scoercein) enum SWIGTYPE *
     89   %{  $input = enumToInteger($input, "$R_class"); %}
     90 %typemap(scoercein) enum SWIGTYPE *const
     91   %{  $input = enumToInteger($input, "$R_class"); %}
     92 
     93 %typemap(scoercein) SWIGTYPE, SWIGTYPE *, SWIGTYPE *const, SWIGTYPE &  
     94  %{ if (inherits($input, "ExternalReference")) $input = slot($input,"ref") %}
     95 
     96 /*
     97 %typemap(scoercein) SWIGTYPE *, SWIGTYPE *const
     98   %{ $input = coerceIfNotSubclass($input, "$R_class") %}
     99 
    100 %typemap(scoercein) SWIGTYPE & 
    101   %{ $input = coerceIfNotSubclass($input, "$R_class") %}
    102 
    103 %typemap(scoercein) SWIGTYPE  
    104   %{ $input = coerceIfNotSubclass($input, "$&R_class") %}
    105 */
    106 
    107 %typemap(scoercein) SWIGTYPE[ANY]  
    108  %{
    109     if(is.list($input))
    110       assert(all(sapply($input, class) == "$R_class"));
    111  %}
    112 
    113 
    114 /* **************************************************************** */
    115 
    116 %typemap(scoercein) bool, bool *, bool & 
    117 		    "$input = as.logical($input);";
    118 %typemap(scoercein) int, 
    119                     int *, 
    120 		    int &,
    121 		    long,
    122 		    long *,
    123 		    long &
    124   "$input = as.integer($input);";
    125 
    126 %typemap(scoercein) char *, string, std::string,
    127 string &, std::string &
    128 %{  $input = as($input, "character"); %}
    129 
    130 %typemap(scoerceout) enum SWIGTYPE 
    131   %{  $result = enumFromInteger($result, "$R_class"); %}
    132 
    133 %typemap(scoerceout) enum SWIGTYPE &
    134   %{  $result = enumFromInteger($result, "$R_class"); %}
    135 
    136 %typemap(scoerceout) enum SWIGTYPE *
    137   %{  $result = enumToInteger($result, "$R_class"); %}
    138 
    139 %typemap(scoerceout) enum SWIGTYPE *const
    140   %{  $result = enumToInteger($result, "$R_class"); %}
    141 
    142 #%typemap(scoerceout) SWIGTYPE 
    143 #  %{ class($result) <- "$&R_class"; %}
    144 
    145 #%typemap(scoerceout) SWIGTYPE & 
    146 #  %{ class($result) <- "$R_class"; %}
    147 
    148 #%typemap(scoerceout) SWIGTYPE * 
    149 #  %{ class($result) <- "$R_class"; %}
    150 
    151 #%typemap(scoerceout) SWIGTYPE *const
    152 #  %{ class($result) <- "$R_class"; %}
    153 
    154  %typemap(scoerceout) SEXP %{ %}
    155 
    156  %typemap(scoerceout) SWIGTYPE 
    157  %{ $result <- new("$&R_class", ref=$result); %}
    158  
    159  %typemap(scoerceout) SWIGTYPE & 
    160  %{ $result <- new("$R_class", ref=$result) ; %}
    161  
    162  %typemap(scoerceout) SWIGTYPE * 
    163  %{ $result <- new("$R_class", ref=$result) ; %}
    164  
    165  %typemap(scoerceout) SWIGTYPE *const
    166  %{ $result <- new("$R_class", ref=$result) ; %}
    167 
    168 
    169 /* Override the SWIGTYPE * above. */
    170 %typemap(scoerceout) char,
    171 		     char *,
    172 		     char &,
    173                      float,
    174 		     double,
    175 		     float*,
    176 		     double*,
    177 		     float &,
    178 		     double &,
    179                      int,
    180 		     int &,
    181 		     long,
    182 		     long &,
    183                      bool,
    184 		     bool &,
    185 		     string,
    186 		     std::string,
    187 		     string &,
    188 		     std::string &, 
    189 		     void,
    190 		     signed int,
    191 		     signed int &,		     
    192 		     unsigned int,
    193 		     unsigned int &,		     
    194 		     short,
    195 		     short &,		     
    196 		     unsigned short,
    197 		     unsigned short &,		     
    198 		     long long,
    199 		     signed long long,
    200 		     signed long long &,		     
    201 		     unsigned long long,
    202 		     unsigned long long &,		     
    203 		     signed long,
    204 		     signed long &,		     
    205 		     unsigned long,
    206 		     unsigned long &,
    207 		     signed char,
    208 		     signed char &,
    209 		     unsigned char,
    210 		     unsigned char &
    211  %{    %}
    212 
    213 %apply int {size_t,
    214 std::size_t,
    215 ptrdiff_t,
    216 std::ptrdiff_t,
    217 signed int,
    218 unsigned int,
    219 short,
    220 unsigned short,
    221 signed char,
    222 unsigned char}
    223 
    224 %apply int* {size_t[],
    225 std::size_t[],
    226 ptrdiff_t[],
    227 std::ptrdiff_t[],
    228 signed int[],
    229 unsigned int[],
    230 short[],
    231 unsigned short[],
    232 signed char[],
    233 unsigned char[]}
    234 
    235 %apply int* {size_t[ANY],
    236 std::size_t[ANY],
    237 ptrdiff_t[ANY],
    238 std::ptrdiff_t[ANY],
    239 signed int[ANY],
    240 unsigned int[ANY],
    241 short[ANY],
    242 unsigned short[ANY],
    243 signed char[ANY],
    244 unsigned char[ANY]}
    245 
    246 %apply int* {size_t*,
    247 std::size_t*,
    248 ptrdiff_t*,
    249 std::ptrdiff_t*,
    250 signed int*,
    251 unsigned int*,
    252 short*,
    253 unsigned short*,
    254 signed char*,
    255 unsigned char*}
    256 
    257 %apply long {
    258        long long,
    259        signed long long,
    260        unsigned long long,
    261        signed long,
    262        unsigned long}
    263 
    264 %apply long* {
    265        long long*,
    266        signed long long*,
    267        unsigned long long*,
    268        signed long*,
    269        unsigned long*,
    270        long long[],
    271        signed long long[],
    272        unsigned long long[],
    273        signed long[],
    274        unsigned long[],
    275        long long[ANY],
    276        signed long long[ANY],
    277        unsigned long long[ANY],
    278        signed long[ANY],
    279        unsigned long[ANY]}
    280 
    281 %apply float* {
    282        float[],
    283        float[ANY]
    284 }
    285 %apply double * {
    286        double[],
    287        double[ANY]
    288 }
    289 
    290 %apply bool* {
    291        bool[],
    292        bool[ANY]
    293 }
    294 
    295 #if 0
    296  Just examining the values for a SWIGTYPE.
    297 
    298 %typemap(scoerceout) SWIGTYPE  %{
    299 
    300   name = $1_name
    301   type = $1_type
    302   ltype = $1_ltype
    303 
    304   mangle = $1_mangle
    305   descriptor = $1_descriptor
    306 
    307   pointer type = $*1_type
    308   pointer ltype = $*1_ltype
    309 
    310   pointer descriptor = $*1_descriptor
    311   basetype = $*_basetype
    312 
    313 %}
    314 #endif
    315 
    316 
    317