Change %extend example which said that char arrays were problematic to wrap, when in fact they are not
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11764 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
2c74fbf9e4
commit
09a801dd9e
4 changed files with 53 additions and 28 deletions
|
|
@ -2506,41 +2506,53 @@ of the object.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
A similar technique can also be used to work with problematic data members.
|
A similar technique can also be used to work with data members that you want to process.
|
||||||
For example, consider this interface:
|
For example, consider this interface:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<div class="code">
|
<div class="code">
|
||||||
<pre>
|
<pre>
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name[50];
|
char name[50];
|
||||||
...
|
...
|
||||||
} Person;
|
} Person;
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
By default, the <tt>name</tt> attribute is read-only because SWIG does not
|
Say you wanted to ensure <tt>name</tt> was always upper case, you can rewrite
|
||||||
normally know how to modify arrays. However, you can rewrite the interface
|
the interface as follows to ensure this occurs whenever a name is read or written to:
|
||||||
as follows to change this:
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<div class="code">
|
<div class="code">
|
||||||
<pre>
|
<pre>
|
||||||
typedef struct {
|
typedef struct {
|
||||||
%extend {
|
%extend {
|
||||||
char *name;
|
char name[50];
|
||||||
}
|
}
|
||||||
...
|
...
|
||||||
} Person;
|
} Person;
|
||||||
|
|
||||||
// Specific implementation of set/get functions
|
|
||||||
%{
|
%{
|
||||||
char *Person_name_get(Person *p) {
|
#include <string.h>
|
||||||
return p->name;
|
#include <ctype.h>
|
||||||
|
|
||||||
|
void make_upper(char *name) {
|
||||||
|
char *c;
|
||||||
|
for (c = name; *c; ++c)
|
||||||
|
*c = (char)toupper((int)*c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Specific implementation of set/get functions forcing capitalization */
|
||||||
|
|
||||||
|
char *Person_name_get(Person *p) {
|
||||||
|
make_upper(p->name);
|
||||||
|
return p->name;
|
||||||
|
}
|
||||||
|
|
||||||
void Person_name_set(Person *p, char *val) {
|
void Person_name_set(Person *p, char *val) {
|
||||||
strncpy(p->name,val,50);
|
strncpy(p->name,val,50);
|
||||||
|
make_upper(p->name);
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
@ -2550,7 +2562,7 @@ void Person_name_set(Person *p, char *val) {
|
||||||
Finally, it should be stressed that even though <tt>%extend</tt>
|
Finally, it should be stressed that even though <tt>%extend</tt>
|
||||||
can be used to add new data members, these new members can not require
|
can be used to add new data members, these new members can not require
|
||||||
the allocation of additional storage in the object (e.g., their values must
|
the allocation of additional storage in the object (e.g., their values must
|
||||||
be entirely synthesized from existing attributes of the structure).
|
be entirely synthesized from existing attributes of the structure or obtained elsewhere).
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
@ -2633,7 +2645,7 @@ $o->{intRep}->{ivalue} = 7 # Change value of o.intRep.ivalue
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
If you have a lot nested structure declarations, it is
|
If you have a lot of nested structure declarations, it is
|
||||||
advisable to double-check them after running SWIG. Although,
|
advisable to double-check them after running SWIG. Although,
|
||||||
there is a good chance that they will work, you may have to
|
there is a good chance that they will work, you may have to
|
||||||
modify the interface file in certain cases.
|
modify the interface file in certain cases.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
%module memberin_extend
|
%module memberin_extend
|
||||||
|
|
||||||
// Tests memberin typemap. The default char * memberin typemap will be used.
|
// Tests memberin typemap is not used for %extend.
|
||||||
// The test extends the struct with a pseudo member variable
|
// The test extends the struct with a pseudo member variable
|
||||||
|
|
||||||
%inline %{
|
%inline %{
|
||||||
|
|
|
||||||
|
|
@ -4,22 +4,35 @@
|
||||||
|
|
||||||
%{
|
%{
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name[50];
|
char name[50];
|
||||||
} Person;
|
} Person;
|
||||||
%}
|
%}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
%extend {
|
%extend {
|
||||||
char *name;
|
char name[50];
|
||||||
}
|
}
|
||||||
} Person;
|
} Person;
|
||||||
|
|
||||||
/* Specific implementation of set/get functions */
|
|
||||||
%{
|
%{
|
||||||
char *Person_name_get(Person *p) {
|
#include <ctype.h>
|
||||||
return p->name;
|
#include <string.h>
|
||||||
|
|
||||||
|
void make_upper(char *name) {
|
||||||
|
char *c;
|
||||||
|
for (c = name; *c; ++c)
|
||||||
|
*c = (char)toupper((int)*c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Specific implementation of set/get functions forcing capitalization */
|
||||||
|
|
||||||
|
char *Person_name_get(Person *p) {
|
||||||
|
make_upper(p->name);
|
||||||
|
return p->name;
|
||||||
|
}
|
||||||
|
|
||||||
void Person_name_set(Person *p, char *val) {
|
void Person_name_set(Person *p, char *val) {
|
||||||
strncpy(p->name,val,50);
|
strncpy(p->name,val,50);
|
||||||
|
make_upper(p->name);
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import memberin_extend_c
|
import memberin_extend_c
|
||||||
|
|
||||||
t = memberin_extend_c.Person()
|
t = memberin_extend_c.Person()
|
||||||
t.name = "some name"
|
t.name = "Fred Bloggs"
|
||||||
if t.name != "some name":
|
if t.name != "FRED BLOGGS":
|
||||||
raise RuntimeError("some name wrong")
|
raise RuntimeError("name wrong")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue