2149
2169
return SWIG_OK;
2153
int R::defineArrayAccessors(SwigType *type) {
2155
SwigType *base = SwigType_base(type);
2156
String *rclass = NewStringf("%sArray", base);
2157
char *prclassName = Char(rclass);
2158
if(strncmp(prclassName, "struct ", 7) == 0)
2161
Node *n = NewHash();
2162
Setattr(n, "type", base);
2164
String *rclassName = getRClassName(base);
2165
String *rclassBase = getRClassName(base, 0);
2167
String *cGetName = NewStringf("R_SWIG_%s_get_item_", prclassName);
2168
String *cSetName = NewStringf("R_SWIG_%s_set_item_", prclassName);
2170
Wrapper *cGetItem = NewWrapper();
2172
String *getItem = NewString(""),
2173
*setItem = NewString("");
2175
Printf(getItem, "function(x, i, j, ..., drop = TRUE) {\n");
2177
Printf(getItem, "%sif(i < 1 || i > x@dims[1])\n%sstop('index must be between 1 and ', x@dims[1])\n", tab4, tab8);
2178
Printf(getItem, "%s.Call('%s', x@ref, as.integer(i-1), PACKAGE = '%s')\n", tab4, cGetName, Rpackage);
2179
Printf(getItem, "}\n");
2183
Printf(setItem, "function(x, i, j, ..., value) {\n");
2185
Printf(setItem, "%sif(i < 1 || i > x@dims[1])\n%sstop('index must be between 1 and ', x@dims[1])\n", tab4, tab8);
2187
/* Do the SCOERCEIN and the SCHECK here */
2188
tm = Swig_typemap_lookup_new("scoercein", n, "value", 0);
2190
Replaceall(tm, "$input", "s_value");
2191
Replaceall(tm, "$R_class", rclassName);
2192
Replaceall(tm, "$*R_class", rclassBase);
2194
Printf(setItem, "%s%s\n", tab4, tm);
2197
tm = Swig_typemap_lookup_new("scheck", n, "value", 0);
2199
Replaceall(tm, "$input", "s_value");
2200
Replaceall(tm, "$R_class", rclassName);
2201
Replaceall(tm, "$*R_class", rclassBase);
2203
Printf(setItem, "%s%s\n", tab4, tm);
2208
Printf(setItem, "%s.Call('%s', x@ref, as.integer(i-1), value, PACKAGE = '%s')\n", tab4, cSetName, Rpackage);
2209
Printf(setItem, "%sx\n}\n", tab4);
2213
Printf(cGetItem->def, "SEXP\n%s(SEXP s_x, SEXP s_i)\n{\n", cGetName);
2215
String *tmp = NewStringf("%s *ptr", SwigType_lstr(base, 0));
2216
String *tmp1 = NewStringf("%s result", SwigType_lstr(base, 0));
2218
Wrapper_add_localv(cGetItem, "r_vmax", "VMAXTYPE", "r_vmax = vmaxget()", NIL);
2219
Wrapper_add_local(cGetItem, "ptr", tmp);
2220
Wrapper_add_local(cGetItem, "r_ans", "SEXP r_ans");
2221
Wrapper_add_local(cGetItem, "result", tmp1);
2222
Wrapper_add_local(cGetItem, "r_nprotect", "int r_nprotect = 0");
2224
Printf(cGetItem->code, "ptr = (%s *) R_SWIG_resolveExternalRef(s_x, \"\", \"s_x\", 0);\n", SwigType_lstr(base, 0));
2225
Printf(cGetItem->code, "result = ptr[INTEGER(s_i)[0]];\n");
2228
tm = Swig_typemap_lookup_new("out", n, "result", 0);
2230
Replaceall(tm, "$result", "r_ans");
2231
Replaceall(tm,"$owner", "R_SWIG_EXTERNAL");
2232
Printf(cGetItem->code, "%s\n", tm);
2235
Delete(tmp); Delete(tmp1);
2237
Printf(cGetItem->code, "%s\nreturn r_ans;\n}\n\n", UnProtectWrapupCode);
2240
/******************************/
2242
R_SWIG_..._set_item(SEXP x, SEXP s_i, SEXP s_value) {
2243
char *r_vmax = vmaxget();
2245
type *ptr, *el, value;
2247
ptr = (type *) R_SWIG_resolveExternalRef(s_x, "", "s_x", 0);
2249
ptr[INTEGER(s_i)[0]] = *el;
2255
Wrapper *cSetItem = NewWrapper();
2258
Printf(cSetItem->def, "SEXP\n%s(SEXP s_x, SEXP s_i, SEXP s_value)\n{\n", cSetName);
2260
tmp = NewStringf("%s *ptr", SwigType_lstr(base, 0));
2261
tmp1 = NewStringf("%s value", SwigType_lstr(base, 0));
2263
Wrapper_add_localv(cSetItem, "r_vmax", "VMAXTYPE", "r_vmax = vmaxget()", NIL);
2264
Wrapper_add_local(cSetItem, "r_nprotect", "int r_nprotect = 0");
2266
Wrapper_add_local(cSetItem, "ptr", tmp);
2267
Wrapper_add_local(cSetItem, "value", tmp1);
2268
Replaceall(tmp, "*ptr", "*el = &value");
2269
Wrapper_add_local(cSetItem, "el", tmp);
2271
Printf(cSetItem->code,
2272
"ptr = (%s *) R_SWIG_resolveExternalRef(s_x, \"\", \"s_x\", 0);\n",
2273
SwigType_lstr(base, 0));
2276
String *tm = Swig_typemap_lookup_new("in", n, "value", 0);
2278
String *rclassName = getRClassName(base);
2279
String *rclassBase = getRClassName(base, 0);
2280
Replaceall(tm, "$input", "s_value");
2281
Replaceall(tm, "$*1", "value");
2282
//XXX think about what we really mean here.
2283
Replaceall(tm, "$1", "el");
2285
Replaceall(tm, "$R_class", rclassName);
2286
Replaceall(tm, "$*R_class", rclassBase);
2288
Printf(cSetItem->code, "%s\n", tm);
2290
Delete(rclassName); Delete(rclassBase);
2292
Printf(cSetItem->code, "ptr[INTEGER(s_i)[0]] = *el;\n");
2294
Printf(cSetItem->code, "%s\nreturn R_NilValue;\n}\n\n", UnProtectWrapupCode);
2296
/*************************/
2299
Wrapper_print(cGetItem, f_wrapper);
2300
Wrapper_print(cSetItem, f_wrapper);
2302
String *elClass = NewStringf("_p%s", SwigType_manglestr(base));
2303
if(!Getattr(SClassDefs, elClass)) {
2305
Printf(stderr, "<defineArrayAccessors> Defining class %s\n", elClass);
2306
Printf(s_classes, "setClass('%s', contains = 'ExternalReference')\n", elClass);
2308
Setattr(SClassDefs, elClass, elClass);
2312
if(!Getattr(SClassDefs, rclassName)) {
2313
Setattr(SClassDefs, rclassName, rclassName);
2314
Printf(s_classes, "setClass('%s', contains = 'SWIGArray')\n", rclassName);
2317
Printf(s_classes, "setMethod('[', '%s',\n%s)\n", rclassName, getItem);
2318
Printf(s_classes, "setMethod('[<-', '%s',\n%s)\n", rclassName, setItem);
2322
DelWrapper(cGetItem);
2323
DelWrapper(cSetItem);
2331
2172
/*****************************************************
2332
2173
Add the specified routine name to the collection of
2333
2174
generated routines that are called from R functions.
2565
// This procedure is for reference
2566
int R::generateCopyRoutinesObsolete(Node *n) {
2567
Wrapper *toC = NewWrapper();
2568
Wrapper *toCRef = NewWrapper();
2569
Wrapper *toR = NewWrapper();
2571
Wrapper *copyToR = NewWrapper();
2572
Wrapper *copyToC = NewWrapper();
2575
String *name = Getattr(n, "name");
2576
String *tdname = Getattr(n, "tdname");
2577
String *kind = Getattr(n, "kind");
2581
type = Copy(tdname);
2583
type = NewStringf("%s %s", kind, name);
2587
#ifdef R_SWIG_VERBOSE
2589
Printf(stderr, "generateCopyRoutines: name = %s, %s\n", name, type);
2592
String *rclassNameRef = getRClassName(type, 1);
2593
String *copyRefRefName = NewStringf("R_swig_copy_%sRef_%sRef", rclassNameRef, rclassNameRef);
2594
String *toCName = NewStringf("R_swig_copy_%sRef_to_C", name);
2595
String *toRName = NewStringf("R_swig_copy_%sRef_to_R", name);
2597
addRegistrationRoutine(copyRefRefName, 2);
2598
addRegistrationRoutine(toCName, 2);
2599
addRegistrationRoutine(toRName, 1);
2601
Printf(toC->def, "int\n%s(SEXP sobj, %s *value)", toCName, type);
2602
Printf(toR->def, "SEXP\n%s(%s *value)", toRName, type);
2603
Printf(toCRef->def, "SEXP\n%s(SEXP s_src, SEXP s_dest) {", copyRefRefName);
2608
String *tmp = NewStringf("%s *src", type);
2609
Wrapper_add_local(toCRef, "src", tmp);
2611
tmp = NewStringf("%s *dest", type);
2612
Wrapper_add_local(toCRef, "dest", tmp);
2615
Printf(toCRef->code, "src = (%s *) R_SWIG_resolveExternalRef(s_src, \"%sRef\", \"s_src\", (Rboolean) FALSE);\n",
2616
type, rclassNameRef);
2617
Printf(toCRef->code, "dest = (%s *) R_SWIG_resolveExternalRef(s_dest, \"%sRef\", \"s_dest\", (Rboolean) FALSE);\n",
2618
type, rclassNameRef);
2619
Printf(toCRef->code, "memcpy(dest, src, sizeof(*src));\nreturn R_NilValue;\n}\n\n");
2623
Wrapper_add_localv(toR, "r_obj", "SEXP", "r_obj", NIL);
2624
Wrapper_add_localv(toR, "r_vmax", "VMAXTYPE", "r_vmax = vmaxget()", NIL);
2625
Wrapper_add_localv(toR, "_tmp_sexp", "SEXP", "_tmp_sexp", NIL);
2626
Wrapper_add_local(toR, "r_nprotect", "int r_nprotect = 0");
2627
Wrapper_add_local(toC, "ecode", "int ecode = 0");
2629
Printf(copyToR->def, "%sCopyToR = function(value, obj = new(\"%s\"))\n{\n", name, name);
2630
Printf(copyToC->def, "%sCopyToC = function(value, obj)\n{\n", name);
2633
Printf(toR->code, "Rf_protect(r_obj = NEW_OBJECT(MAKE_CLASS(\"%s\")));\nr_nprotect++;\n\n", name);
2635
Wrapper_add_localv(toC, "_tmp_sexp", "SEXP", "_tmp_sexp", NIL);
2637
Node *c = firstChild(n);
2638
// Swig_typemap_attach_parms("in", c, toR);
2639
// Swig_typemap_attach_parms("out", c, toR);
2641
for(; c; c = nextSibling(c)) {
2642
String *elName = Getattr(c, "name");
2646
String *tp = Swig_typemap_lookup_new("rtype", c, "", 0);
2650
/* The S functions to get and set the member value. */
2651
String *symname = Getattr(c, "sym:name");
2652
String *ClassPrefix = Getattr(n, "sym:name");
2653
String *get = Swig_name_get(Swig_name_member(Char(ClassPrefix), symname));
2654
String *set = Swig_name_set(Swig_name_member(Char(ClassPrefix), symname));
2658
This is already done now in getRType().
2659
If that for some reason no longer gets called, this had better go back.
2660
SwigType *elTT = Getattr(c, "type");
2661
SwigType *decl = Getattr(c, "decl");
2662
SwigType_push(elTT, decl);
2666
Printf(copyToR->code, "obj@%s = %s(value)\n", elName, get);
2667
Printf(copyToC->code, "%s(obj, value@%s)\n", set, elName);
2670
String *field = NewStringf("value->%s", elName);
2671
SwigType *elType = Getattr(c, "type");
2674
String *tm = Swig_typemap_lookup_new("out", c, field, 0);
2676
#ifdef R_SWIG_VERBOSE
2678
Printf(stderr, "Got conversion to R for '%s': '%s' '%s' -> '%s'\n", elName, elType, elTT, tm);
2681
//XXX Get the field in as the rhs.
2682
// What about looking in the "memberin"/"memberout" typemaps.
2683
Replaceall(tm, "$1", Char(field));
2684
Replaceall(tm, "$result", "_tmp_sexp");
2685
Replaceall(tm,"$owner", "R_SWIG_EXTERNAL");
2686
replaceRClass(tm,elType);
2689
Printf(toR->code, "%s\nRf_protect(_tmp_sexp);\nr_nprotect++;\n", tm);
2690
Printf(toR->code, "Rf_protect(r_obj = R_do_slot_assign(r_obj, Rf_mkString(\"%s\"), _tmp_sexp));\nr_nprotect++;\n\n", elName);
2692
Printf(stderr, "*** Can't convert field %s in \n", elName);
2697
char *field_p = Char(field);
2698
tm = Swig_typemap_lookup_new("in", c, field_p, 0);
2699
if(tm && !GetFlag(c, "feature:immutable")) {
2700
replaceRClass(tm,elType);
2702
Printf(stderr, "typemap (in) for %s => %s\n",
2703
SwigType_str(elType, 0), tm);
2705
NewStringf("%s val", SwigType_lstr(elType, 0));
2706
Wrapper_add_local(toC, "val", tmp1);
2707
Replaceall(tm, "$input", "_tmp_sexp");
2709
Printf(stderr, "Got conversion to C for %s: %s. %s\n",
2711
#ifdef R_SWIG_VERBOSE
2713
Printf(toC->code, "_tmp_sexp = GET_SLOT(sobj, Rf_mkString(\"%s\"));\n%s\n\n", elName, tm);
2718
Replaceall(conversion, "$1", field);
2719
Printf(toC->code, "value->%s = ;\n", name);
2723
Printv(toR->code, UnProtectWrapupCode,
2724
"\nreturn(r_obj);\n}\n", NIL);
2726
Printf(toC->code, "\nreturn(1);\n}\n");
2729
Printf(f_wrapper, "%s;\n", toR->def);
2730
Printf(f_wrapper, "%s;\n", toC->def);
2732
Printf(toR->def, "\n{\n");
2733
Printf(toC->def, "\n{\n");
2735
String *rclassName = getRClassName(type, 0); // without the Ref.
2737
Printf(copyToR->code, "obj\n}\n\n");
2739
Printf(sfile, "# Start definition of copy functions & methods for %s\n", rclassName);
2741
Wrapper_print(copyToR, sfile);
2742
Printf(copyToC->code, "obj\n}\n\n");
2743
Wrapper_print(copyToC, sfile);
2746
Printf(sfile, "# Start definition of copy methods for %s\n", rclassName);
2747
Printf(sfile, "setMethod('copyToR', '%sRef', %sCopyToR)\n", rclassName, name);
2748
Printf(sfile, "setMethod('copyToC', '%s', %sCopyToC)\n\n", rclassName, name);
2749
Printv(sfile, "setMethod('copyToC', c('", rclassName, "Ref', '", rclassName, "Ref'),",
2750
" function(value, obj) {\n",
2751
tab4, ".Call(\"", copyRefRefName, "\", value, obj, PACKAGE = \"",
2752
Rpackage, "\")\n})\n\n", NIL);
2753
Printf(sfile, "# End definition of copy methods for %s\n", rclassName);
2754
Printf(sfile, "# End definition of copy functions & methods for %s\n", rclassName);
2756
String *m = NewStringf("%sCopyToR", name);
2757
addNamespaceMethod(m);
2758
char *tt = Char(m); tt[Len(m)-1] = 'C';
2759
addNamespaceMethod(m);
2765
DelWrapper(copyToR);
2766
DelWrapper(copyToC);
2768
Wrapper_print(toR, f_wrapper);
2769
Wrapper_print(toC, f_wrapper);
2770
Wrapper_print(toCRef, f_wrapper);
2779
2408
int R::generateCopyRoutines(Node *n) {
2780
2409
Wrapper *copyToR = NewWrapper();
2781
2410
Wrapper *copyToC = NewWrapper();