#31 Dynamic retrieving of attributes/methods of a class

open
nobody
None
5
2015-02-11
2005-04-02
NMTT
No

In order to make a generic binary file viewer in Tcl, I have extended
each C struct class with a method returning the list of the attributes :

%define CREATE_MEMBERS_ACCESSOR(TYPE_T)
%extend TYPE_T {
void getMembers(int *pNbMembers, char ***pppMembersList)
{
#define ATTRIBUTE_VAR swig_ ## TYPE_T ## _attributes
extern swig_attribute ATTRIBUTE_VAR[];
swig_attribute *pAttrInit = &ATTRIBUTE_VAR[0];
#undef ATTRIBUTE_VAR
swig_attribute *pAttr;
int i;

\*pNbMembers = 0;
for \(pAttr = pAttrInit; pAttr->name; pAttr++\)
\{
  \(\*pNbMembers\)++;
\}

if \(\*pNbMembers\)
\{
  \*pppMembersList = malloc\(\*pNbMembers\*sizeof\(char \*\)\);
  for \(pAttr = pAttrInit,i=0; pAttr->name; pAttr++,i++\)
  \{
    \(\*pppMembersList\)\[i\] = \(\(char \*\)pAttr->name\)+1;
  \}
\}
else
\{
  \*pppMembersList = 0;
\}

}
};
%enddef

It is not very elegant, and I would like to have this feature directly
implemented in swig.

Discussion


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks