Skip to content

Commit

Permalink
smartptr feature support - factor out common code
Browse files Browse the repository at this point in the history
  • Loading branch information
wsfulton committed Sep 25, 2015
1 parent 4677dbb commit faeaacf
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 132 deletions.
1 change: 1 addition & 0 deletions Source/CParse/cparse.h
Expand Up @@ -57,6 +57,7 @@ extern "C" {

/* util.c */
extern void Swig_cparse_replace_descriptor(String *s);
extern SwigType *Swig_cparse_smartptr(Node *n);
extern void cparse_normalize_void(Node *);
extern Parm *Swig_cparse_parm(String *s);
extern ParmList *Swig_cparse_parms(String *s, Node *file_line_node);
Expand Down
22 changes: 22 additions & 0 deletions Source/CParse/util.c
Expand Up @@ -70,6 +70,28 @@ void Swig_cparse_replace_descriptor(String *s) {
}
}

/* -----------------------------------------------------------------------------
* Swig_cparse_smartptr()
*
* Parse the type in smartptr feature and convert into a SwigType.
* Error out if the parsing fails as this is like a parser syntax error.
* ----------------------------------------------------------------------------- */

SwigType *Swig_cparse_smartptr(Node *n) {
SwigType *smart = 0;
String *smartptr = Getattr(n, "feature:smartptr");
if (smartptr) {
SwigType *cpt = Swig_cparse_type(smartptr);
if (cpt) {
smart = SwigType_typedef_resolve_all(cpt);
Delete(cpt);
} else {
Swig_error(Getfile(n), Getline(n), "Invalid type (%s) in 'smartptr' feature for class %s.\n", smartptr, SwigType_namestr(Getattr(n, "name")));
}
}
return smart;
}

/* -----------------------------------------------------------------------------
* cparse_normalize_void()
*
Expand Down
46 changes: 20 additions & 26 deletions Source/Modules/csharp.cxx
Expand Up @@ -1854,38 +1854,31 @@ class CSHARP:public Language {

// Add code to do C++ casting to base class (only for classes in an inheritance hierarchy)
if (derived) {
String *smartptr = Getattr(n, "feature:smartptr");
String *upcast_method = Swig_name_member(getNSpace(), getClassPrefix(), smartptr != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
SwigType *smart = Swig_cparse_smartptr(n);
String *upcast_method = Swig_name_member(getNSpace(), getClassPrefix(), smart != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
String *wname = Swig_name_wrapper(upcast_method);

Printv(imclass_cppcasts_code, "\n [global::System.Runtime.InteropServices.DllImport(\"", dllimport, "\", EntryPoint=\"", wname, "\")]\n", NIL);
Printf(imclass_cppcasts_code, " public static extern global::System.IntPtr %s(global::System.IntPtr jarg1);\n", upcast_method);

Replaceall(imclass_cppcasts_code, "$csclassname", proxy_class_name);

if (smartptr) {
SwigType *spt = Swig_cparse_type(smartptr);
if (spt) {
SwigType *smart = SwigType_typedef_resolve_all(spt);
Delete(spt);
SwigType *bsmart = Copy(smart);
SwigType *rclassname = SwigType_typedef_resolve_all(c_classname);
SwigType *rbaseclass = SwigType_typedef_resolve_all(c_baseclass);
Replaceall(bsmart, rclassname, rbaseclass);
Delete(rclassname);
Delete(rbaseclass);
String *smartnamestr = SwigType_namestr(smart);
String *bsmartnamestr = SwigType_namestr(bsmart);
Printv(upcasts_code,
"SWIGEXPORT ", bsmartnamestr, " * SWIGSTDCALL ", wname, "(", smartnamestr, " *jarg1) {\n",
" return jarg1 ? new ", bsmartnamestr, "(*jarg1) : 0;\n"
"}\n", "\n", NIL);
Delete(bsmartnamestr);
Delete(smartnamestr);
Delete(bsmart);
} else {
Swig_error(Getfile(n), Getline(n), "Invalid type (%s) in 'smartptr' feature for class %s.\n", smartptr, c_classname);
}
if (smart) {
SwigType *bsmart = Copy(smart);
SwigType *rclassname = SwigType_typedef_resolve_all(c_classname);
SwigType *rbaseclass = SwigType_typedef_resolve_all(c_baseclass);
Replaceall(bsmart, rclassname, rbaseclass);
Delete(rclassname);
Delete(rbaseclass);
String *smartnamestr = SwigType_namestr(smart);
String *bsmartnamestr = SwigType_namestr(bsmart);
Printv(upcasts_code,
"SWIGEXPORT ", bsmartnamestr, " * SWIGSTDCALL ", wname, "(", smartnamestr, " *jarg1) {\n",
" return jarg1 ? new ", bsmartnamestr, "(*jarg1) : 0;\n"
"}\n", "\n", NIL);
Delete(bsmartnamestr);
Delete(smartnamestr);
Delete(bsmart);
} else {
Printv(upcasts_code,
"SWIGEXPORT ", c_baseclass, " * SWIGSTDCALL ", wname, "(", c_classname, " *jarg1) {\n",
Expand All @@ -1894,6 +1887,7 @@ class CSHARP:public Language {
}
Delete(wname);
Delete(upcast_method);
Delete(smart);
}
Delete(baseclass);
}
Expand Down Expand Up @@ -3469,7 +3463,7 @@ class CSHARP:public Language {
Wrapper *code_wrap = NewWrapper();
Printf(code_wrap->def, "SWIGEXPORT void SWIGSTDCALL %s(void *objarg", wname);

if (Len(smartptr)) {
if (smartptr) {
Printf(code_wrap->code, " %s *obj = (%s *)objarg;\n", smartptr, smartptr);
Printf(code_wrap->code, " // Keep a local instance of the smart pointer around while we are using the raw pointer\n");
Printf(code_wrap->code, " // Avoids using smart pointer specific API.\n");
Expand Down
55 changes: 22 additions & 33 deletions Source/Modules/d.cxx
Expand Up @@ -3329,45 +3329,33 @@ class D : public Language {
/* ---------------------------------------------------------------------------
* D::writeClassUpcast()
* --------------------------------------------------------------------------- */
void writeClassUpcast(Node *n, const String* d_class_name,
String* c_class_name, String* c_base_name) {

String *smartptr = Getattr(n, "feature:smartptr");
String *upcast_name = Swig_name_member(getNSpace(), d_class_name,
(smartptr != 0 ? "SmartPtrUpcast" : "Upcast"));
void writeClassUpcast(Node *n, const String* d_class_name, String* c_class_name, String* c_base_name) {

SwigType *smart = Swig_cparse_smartptr(n);
String *upcast_name = Swig_name_member(getNSpace(), d_class_name, (smart != 0 ? "SmartPtrUpcast" : "Upcast"));
String *upcast_wrapper_name = Swig_name_wrapper(upcast_name);

writeImDModuleFunction(upcast_name, "void*", "(void* objectRef)",
upcast_wrapper_name);

if (smartptr) {
SwigType *spt = Swig_cparse_type(smartptr);
if (spt) {
SwigType *smart = SwigType_typedef_resolve_all(spt);
Delete(spt);
SwigType *bsmart = Copy(smart);
SwigType *rclassname = SwigType_typedef_resolve_all(c_class_name);
SwigType *rbaseclass = SwigType_typedef_resolve_all(c_base_name);
Replaceall(bsmart, rclassname, rbaseclass);
Delete(rclassname);
Delete(rbaseclass);
String *smartnamestr = SwigType_namestr(smart);
String *bsmartnamestr = SwigType_namestr(bsmart);
Printv(upcasts_code,
"SWIGEXPORT ", bsmartnamestr, " * ", upcast_wrapper_name,
"(", smartnamestr, " *objectRef) {\n",
" return objectRef ? new ", bsmartnamestr, "(*objectRef) : 0;\n"
"}\n",
"\n", NIL);
Delete(bsmartnamestr);
Delete(smartnamestr);
Delete(bsmart);
} else {
Swig_error(Getfile(n), Getline(n),
"Invalid type (%s) in 'smartptr' feature for class %s.\n",
smartptr, c_class_name);
}
if (smart) {
SwigType *bsmart = Copy(smart);
SwigType *rclassname = SwigType_typedef_resolve_all(c_class_name);
SwigType *rbaseclass = SwigType_typedef_resolve_all(c_base_name);
Replaceall(bsmart, rclassname, rbaseclass);
Delete(rclassname);
Delete(rbaseclass);
String *smartnamestr = SwigType_namestr(smart);
String *bsmartnamestr = SwigType_namestr(bsmart);
Printv(upcasts_code,
"SWIGEXPORT ", bsmartnamestr, " * ", upcast_wrapper_name,
"(", smartnamestr, " *objectRef) {\n",
" return objectRef ? new ", bsmartnamestr, "(*objectRef) : 0;\n"
"}\n",
"\n", NIL);
Delete(bsmartnamestr);
Delete(smartnamestr);
Delete(bsmart);
} else {
Printv(upcasts_code,
"SWIGEXPORT ", c_base_name, " * ", upcast_wrapper_name,
Expand All @@ -3382,6 +3370,7 @@ class D : public Language {

Delete(upcast_name);
Delete(upcast_wrapper_name);
Delete(smart);
}

/* ---------------------------------------------------------------------------
Expand Down
58 changes: 26 additions & 32 deletions Source/Modules/java.cxx
Expand Up @@ -1875,40 +1875,33 @@ class JAVA:public Language {

// Add code to do C++ casting to base class (only for classes in an inheritance hierarchy)
if (derived) {
String *smartptr = Getattr(n, "feature:smartptr");
String *upcast_method = Swig_name_member(getNSpace(), getClassPrefix(), smartptr != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
SwigType *smart = Swig_cparse_smartptr(n);
String *upcast_method = Swig_name_member(getNSpace(), getClassPrefix(), smart != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
String *jniname = makeValidJniName(upcast_method);
String *wname = Swig_name_wrapper(jniname);
Printf(imclass_cppcasts_code, " public final static native long %s(long jarg1);\n", upcast_method);
if (smartptr) {
SwigType *spt = Swig_cparse_type(smartptr);
if (spt) {
SwigType *smart = SwigType_typedef_resolve_all(spt);
Delete(spt);
SwigType *bsmart = Copy(smart);
SwigType *rclassname = SwigType_typedef_resolve_all(c_classname);
SwigType *rbaseclass = SwigType_typedef_resolve_all(c_baseclass);
Replaceall(bsmart, rclassname, rbaseclass);
Delete(rclassname);
Delete(rbaseclass);
String *smartnamestr = SwigType_namestr(smart);
String *bsmartnamestr = SwigType_namestr(bsmart);
Printv(upcasts_code,
"SWIGEXPORT jlong JNICALL ", wname, "(JNIEnv *jenv, jclass jcls, jlong jarg1) {\n",
" jlong baseptr = 0;\n"
" ", smartnamestr, " *argp1;\n"
" (void)jenv;\n"
" (void)jcls;\n"
" argp1 = *(", smartnamestr, " **)&jarg1;\n"
" *(", bsmartnamestr, " **)&baseptr = argp1 ? new ", bsmartnamestr, "(*argp1) : 0;\n"
" return baseptr;\n"
"}\n", "\n", NIL);
Delete(bsmartnamestr);
Delete(smartnamestr);
Delete(bsmart);
} else {
Swig_error(Getfile(n), Getline(n), "Invalid type (%s) in 'smartptr' feature for class %s.\n", smartptr, c_classname);
}
if (smart) {
SwigType *bsmart = Copy(smart);
SwigType *rclassname = SwigType_typedef_resolve_all(c_classname);
SwigType *rbaseclass = SwigType_typedef_resolve_all(c_baseclass);
Replaceall(bsmart, rclassname, rbaseclass);
Delete(rclassname);
Delete(rbaseclass);
String *smartnamestr = SwigType_namestr(smart);
String *bsmartnamestr = SwigType_namestr(bsmart);
Printv(upcasts_code,
"SWIGEXPORT jlong JNICALL ", wname, "(JNIEnv *jenv, jclass jcls, jlong jarg1) {\n",
" jlong baseptr = 0;\n"
" ", smartnamestr, " *argp1;\n"
" (void)jenv;\n"
" (void)jcls;\n"
" argp1 = *(", smartnamestr, " **)&jarg1;\n"
" *(", bsmartnamestr, " **)&baseptr = argp1 ? new ", bsmartnamestr, "(*argp1) : 0;\n"
" return baseptr;\n"
"}\n", "\n", NIL);
Delete(bsmartnamestr);
Delete(smartnamestr);
Delete(bsmart);
} else {
Printv(upcasts_code,
"SWIGEXPORT jlong JNICALL ", wname, "(JNIEnv *jenv, jclass jcls, jlong jarg1) {\n",
Expand All @@ -1922,6 +1915,7 @@ class JAVA:public Language {
Delete(wname);
Delete(jniname);
Delete(upcast_method);
Delete(smart);
}
Delete(baseclass);
}
Expand Down Expand Up @@ -3486,7 +3480,7 @@ class JAVA:public Language {
"SWIGEXPORT void JNICALL Java_%s%s_%s(JNIEnv *jenv, jclass jcls, jobject jself, jlong objarg, jboolean jswig_mem_own, "
"jboolean jweak_global) {\n", jnipackage, jni_imclass_name, swig_director_connect_jni);

if (Len(smartptr)) {
if (smartptr) {
Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", smartptr, smartptr);
Printf(code_wrap->code, " (void)jcls;\n");
Printf(code_wrap->code, " // Keep a local instance of the smart pointer around while we are using the raw pointer\n");
Expand Down
19 changes: 4 additions & 15 deletions Source/Modules/octave.cxx
Expand Up @@ -966,25 +966,13 @@ class OCTAVE:public Language {
SwigType *t = Copy(Getattr(n, "name"));
SwigType_add_pointer(t);

String *smartptr = Getattr(n, "feature:smartptr"); // Replace storing a pointer to underlying class with a smart pointer (intended for use with non-intrusive smart pointers)
SwigType *smart = 0;
if (smartptr) {
SwigType *cpt = Swig_cparse_type(smartptr);
if (cpt) {
smart = SwigType_typedef_resolve_all(cpt);
Delete(cpt);
} else {
// TODO: report line number of where the feature comes from
Swig_error(Getfile(n), Getline(n), "Invalid type (%s) in 'smartptr' feature for class %s.\n", smartptr, class_name);
}
}
// Replace storing a pointer to underlying class with a smart pointer (intended for use with non-intrusive smart pointers)
SwigType *smart = Swig_cparse_smartptr(n);
String *wrap_class = NewStringf("&_wrap_class_%s", class_name);
if(smart){
if (smart) {
SwigType_add_pointer(smart);
SwigType_remember_clientdata(smart, wrap_class);
}
Delete(smart);
Delete(smartptr);
//String *wrap_class = NewStringf("&_wrap_class_%s", class_name);
SwigType_remember_clientdata(t, wrap_class);

Expand Down Expand Up @@ -1064,6 +1052,7 @@ class OCTAVE:public Language {

Delete(base_class);
Delete(base_class_names);
Delete(smart);
Delete(t);
Delete(s_members_tab);
s_members_tab = 0;
Expand Down
23 changes: 5 additions & 18 deletions Source/Modules/python.cxx
Expand Up @@ -4161,15 +4161,11 @@ class PYTHON:public Language {
Printf(clientdata, "&%s_clientdata", templ);
SwigType_remember_mangleddata(pmname, clientdata);

String *smartptr = Getattr(n, "feature:smartptr");
if (smartptr) {
SwigType *spt = Swig_cparse_type(smartptr);
SwigType *smart = SwigType_typedef_resolve_all(spt);
SwigType *smart = Swig_cparse_smartptr(n);
if (smart) {
SwigType_add_pointer(smart);
String *smart_pmname = SwigType_manglestr(smart);
SwigType_remember_mangleddata(smart_pmname, clientdata);
Delete(spt);
Delete(smart);
Delete(smart_pmname);
}

Expand All @@ -4195,6 +4191,7 @@ class PYTHON:public Language {
Printv(f_init, " d = md;\n", NIL);

Delete(clientdata);
Delete(smart);
Delete(rname);
Delete(pname);
Delete(mname);
Expand Down Expand Up @@ -4392,18 +4389,8 @@ class PYTHON:public Language {
/* Complete the class */
if (shadow) {
/* Generate a class registration function */
String *smartptr = Getattr(n, "feature:smartptr"); // Replace storing a pointer to underlying class with a smart pointer (intended for use with non-intrusive smart pointers)
SwigType *smart = 0;
if (smartptr) {
SwigType *cpt = Swig_cparse_type(smartptr);
if (cpt) {
smart = SwigType_typedef_resolve_all(cpt);
Delete(cpt);
} else {
// TODO: report line number of where the feature comes from
Swig_error(Getfile(n), Getline(n), "Invalid type (%s) in 'smartptr' feature for class %s.\n", smartptr, real_classname);
}
}
// Replace storing a pointer to underlying class with a smart pointer (intended for use with non-intrusive smart pointers)
SwigType *smart = Swig_cparse_smartptr(n);
SwigType *ct = Copy(smart ? smart : real_classname);
SwigType_add_pointer(ct);
SwigType *realct = Copy(real_classname);
Expand Down
11 changes: 3 additions & 8 deletions Source/Modules/typepass.cxx
Expand Up @@ -256,11 +256,8 @@ class TypePass:private Dispatcher {
SwigType_inherit(clsname, bname, cast, 0);
String *smartptr = Getattr(first, "feature:smartptr");
if (smartptr) {
SwigType *smart = 0;
SwigType *spt = Swig_cparse_type(smartptr);
if (spt) {
smart = SwigType_typedef_resolve_all(spt);
Delete(spt);
SwigType *smart = Swig_cparse_smartptr(first);
if (smart) {
/* Record a (fake) inheritance relationship between smart pointer
and smart pointer to base class, so that smart pointer upcasts
are automatically generated. */
Expand All @@ -282,10 +279,8 @@ class TypePass:private Dispatcher {
Swig_warning(WARN_LANG_SMARTPTR_MISSING, Getfile(first), Getline(first), "Base class '%s' of '%s' is not similarly marked as a smart pointer.\n", SwigType_namestr(Getattr(bclass, "name")), SwigType_namestr(Getattr(first, "name")));
Delete(convcode);
Delete(bsmart);
Delete(smart);
} else {
Swig_error(Getfile(first), Getline(first), "Invalid type (%s) in 'smartptr' feature for class %s.\n", SwigType_namestr(smartptr), SwigType_namestr(clsname));
}
Delete(smart);
} else {
if (GetFlag(bclass, "feature:smartptr"))
Swig_warning(WARN_LANG_SMARTPTR_MISSING, Getfile(first), Getline(first), "Derived class '%s' of '%s' is not similarly marked as a smart pointer.\n", SwigType_namestr(Getattr(first, "name")), SwigType_namestr(Getattr(bclass, "name")));
Expand Down

0 comments on commit faeaacf

Please sign in to comment.