first pass at removing string comparisons for set/get methods

This commit is contained in:
Richard Beare 2018-05-10 19:13:39 +10:00
commit c42fb730f4

View file

@ -15,10 +15,6 @@
#include "swigmod.h" #include "swigmod.h"
#include "cparse.h" #include "cparse.h"
#define R_MEMBER_NORMAL 0
#define R_MEMBER_SET 1
#define R_MEMBER_GET 2
static String* replaceInitialDash(const String *name) static String* replaceInitialDash(const String *name)
{ {
String *retval; String *retval;
@ -289,7 +285,10 @@ protected:
int generateCopyRoutines(Node *n); int generateCopyRoutines(Node *n);
int DumpCode(Node *n); int DumpCode(Node *n);
int OutputMemberReferenceMethod(String *className, int isSet, List *el, File *out); //int OutputMemberReferenceMethod(String *className, int isSet, List *el, File *out);
int OutputMemberReferenceMethod(String *className, int isSet,
List *memberList, List *nameList,
List *typeList, File *out);
int OutputArrayMethod(String *className, List *el, File *out); int OutputArrayMethod(String *className, List *el, File *out);
int OutputClassMemberTable(Hash *tb, File *out); int OutputClassMemberTable(Hash *tb, File *out);
int OutputClassMethodsTable(File *out); int OutputClassMethodsTable(File *out);
@ -338,7 +337,7 @@ protected:
void addAccessor(String *memberName, Wrapper *f, void addAccessor(String *memberName, Wrapper *f,
String *name, int isSet = -1); String *name, String *methodSetGet);
static int getFunctionPointerNumArgs(Node *n, SwigType *tt); static int getFunctionPointerNumArgs(Node *n, SwigType *tt);
@ -371,6 +370,9 @@ protected:
String *member_name; String *member_name;
String *class_name; String *class_name;
String *R_MEMBER_NORMAL;
String *R_MEMBER_SET;
String *R_MEMBER_GET;
int processing_class_member_function; int processing_class_member_function;
// List *class_member_functions; // List *class_member_functions;
@ -439,13 +441,16 @@ R::R() :
processing_member_access_function(0), processing_member_access_function(0),
member_name(0), member_name(0),
class_name(0), class_name(0),
R_MEMBER_NORMAL(NewString("normal")),
R_MEMBER_SET(NewString("set")),
R_MEMBER_GET(NewString("get")),
processing_class_member_function(0), processing_class_member_function(0),
// class_member_functions(0), // class_member_functions(0),
// class_member_set_functions(0), // class_member_set_functions(0),
class_member_function_types(0), class_member_function_types(0),
class_member_function_names(0), class_member_function_names(0),
class_member_function_wrappernames(0),
class_member_function_membernames(0), class_member_function_membernames(0),
class_member_function_wrappernames(0),
ClassMemberTable(0), ClassMemberTable(0),
ClassMethodsTable(0), ClassMethodsTable(0),
SClassDefs(0), SClassDefs(0),
@ -990,7 +995,7 @@ int R::OutputClassMemberTable(Hash *tb, File *out) {
} }
// OutputArrayMethod(className, el, out); // OutputArrayMethod(className, el, out);
OutputMemberReferenceMethod(className, isSet, el, out); //OutputMemberReferenceMethod(className, isSet, el, out);
if(outputNamespaceInfo) if(outputNamespaceInfo)
Printf(s_namespace, "\"%s\"%s", className, i < n-1 ? "," : ""); Printf(s_namespace, "\"%s\"%s", className, i < n-1 ? "," : "");
@ -1002,7 +1007,6 @@ int R::OutputClassMemberTable(Hash *tb, File *out) {
return n; return n;
} }
/* -------------------------------------------------------------- /* --------------------------------------------------------------
* Write the methods for $ or $<- for accessing a member field in an * Write the methods for $ or $<- for accessing a member field in an
* struct or union (or class). * struct or union (or class).
@ -1015,9 +1019,10 @@ int R::OutputClassMemberTable(Hash *tb, File *out) {
* out - the stream where we write the code. * out - the stream where we write the code.
* --------------------------------------------------------------*/ * --------------------------------------------------------------*/
int R::OutputMemberReferenceMethod(String *className, int isSet, int R::OutputMemberReferenceMethod(String *className, int isSet,
List *el, File *out) { List *memberList, List *nameList,
int numMems = Len(el), j; List *typeList, File *out) {
int numMems = Len(memberList), j;
int varaccessor = 0; int varaccessor = 0;
if (numMems == 0) if (numMems == 0)
return SWIG_OK; return SWIG_OK;
@ -1032,13 +1037,20 @@ int R::OutputMemberReferenceMethod(String *className, int isSet,
Node *itemList = NewHash(); Node *itemList = NewHash();
bool has_prev = false; bool has_prev = false;
for(j = 0; j < numMems; j+=3) { for(j = 0; j < numMems; j++) {
String *item = Getitem(el, j); String *item = Getitem(memberList, j);
String *dup = Getitem(el, j + 1); String *dup = Getitem(nameList, j);
char *ptr = Char(dup); String *setgetmethod = Getitem(typeList, j);
ptr = &ptr[Len(dup) - 3];
if (!strcmp(ptr, "get")) // skip this one if it isn't a set method but we're
// creating a modification method
if (isSet && (setgetmethod != R_MEMBER_SET))
continue;
// skip the set methods when creating accessor methods
if ((!isSet) && (setgetmethod == R_MEMBER_SET))
continue;
if ((!isSet) && (setgetmethod == R_MEMBER_GET))
varaccessor++; varaccessor++;
if (Getattr(itemList, item)) if (Getattr(itemList, item))
@ -1068,17 +1080,15 @@ int R::OutputMemberReferenceMethod(String *className, int isSet,
if (!isSet && varaccessor > 0) { if (!isSet && varaccessor > 0) {
Printf(f->code, "%svaccessors = c(", tab8); Printf(f->code, "%svaccessors = c(", tab8);
int first = 1; bool first = true;
for(j = 0; j < numMems; j+=3) { for(j = 0; j < numMems; j++) {
String *item = Getitem(el, j); String *item = Getitem(memberList, j);
String *dup = Getitem(el, j + 1); String *setgetmethod = Getitem(typeList, j);
char *ptr = Char(dup);
ptr = &ptr[Len(dup) - 3];
// Check the type here instead of the name // Check the type here instead of the name
if (!strcmp(ptr, "get")) { if (setgetmethod == R_MEMBER_GET) {
Printf(f->code, "%s'%s'", first ? "" : ", ", item); Printf(f->code, "%s'%s'", first ? "" : ", ", item);
first = 0; first = false;
} }
} }
Printf(f->code, ");\n"); Printf(f->code, ");\n");
@ -1351,7 +1361,7 @@ int R::variableWrapper(Node *n) {
* --------------------------------------------------------------*/ * --------------------------------------------------------------*/
void R::addAccessor(String *memberName, Wrapper *wrapper, String *name, void R::addAccessor(String *memberName, Wrapper *wrapper, String *name,
int isSet) { String *methodSetGet) {
#if 0 #if 0
if(isSet < 0) { if(isSet < 0) {
int n = Len(name); int n = Len(name);
@ -1384,7 +1394,7 @@ void R::addAccessor(String *memberName, Wrapper *wrapper, String *name,
class_member_function_wrappernames = NewList(); class_member_function_wrappernames = NewList();
class_member_function_types = NewList(); class_member_function_types = NewList();
} }
Append(class_member_function_types, isSet); Append(class_member_function_types, methodSetGet);
Append(class_member_function_names, name); Append(class_member_function_names, name);
Append(class_member_function_membernames, memberName); Append(class_member_function_membernames, memberName);
@ -1846,10 +1856,10 @@ int R::functionWrapper(Node *n) {
/* Add the name of this member to a list for this class_name. /* Add the name of this member to a list for this class_name.
We will dump all these at the end. */ We will dump all these at the end. */
int n = Len(iname); int nlen = Len(iname);
char *ptr = Char(iname); char *ptr = Char(iname);
bool isSet(0); bool isSet(0);
if (n > 4) isSet = Strcmp(NewString(&ptr[n-4]), "_set") == 0; if (nlen > 4) isSet = Strcmp(NewString(&ptr[nlen-4]), "_set") == 0;
String *tmp = NewString(""); String *tmp = NewString("");
@ -2292,10 +2302,10 @@ int R::functionWrapper(Node *n) {
Would like to be able to do this so that we can potentially insert Would like to be able to do this so that we can potentially insert
*/ */
if(processing_member_access_function || processing_class_member_function) { if(processing_member_access_function || processing_class_member_function) {
int method_type(R_MEMBER_NORMAL); String *method_type = R_MEMBER_NORMAL;
if (GetFlag("memberset", n)) { if (GetFlag(n, "memberset")) {
method_type = R_MEMBER_SET; method_type = R_MEMBER_SET;
} else if (GetFlag("memberget", n)) { } else if (GetFlag(n, "memberget")) {
method_type = R_MEMBER_GET; method_type = R_MEMBER_GET;
} }
addAccessor(member_name, sfun, iname, method_type); addAccessor(member_name, sfun, iname, method_type);
@ -2500,9 +2510,20 @@ int R::classDeclaration(Node *n) {
if (class_member_set_functions) if (class_member_set_functions)
OutputMemberReferenceMethod(name, 1, class_member_set_functions, sfile); OutputMemberReferenceMethod(name, 1, class_member_set_functions, sfile);
#else #else
// filter the class_member_lists by type, then call if (class_member_function_types) {
// OutputMemberReferenceMethod // count the number of set methods
unsigned setcount = 0;
Iterator ItType;
for (ItType = First(class_member_function_types) ; ItType.item; ItType = Next(ItType)) {
if (ItType.item == R_MEMBER_SET) ++setcount;
}
if (Len(class_member_function_types) - setcount > 0) {
OutputMemberReferenceMethod(name, 0, class_member_function_membernames, class_member_function_names, class_member_function_types, sfile);
}
if (setcount > 0) {
OutputMemberReferenceMethod(name, 1, class_member_function_membernames, class_member_function_names, class_member_function_types, sfile);
}
}
#endif #endif
// if(class_member_functions) { // if(class_member_functions) {