473,408 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to access an Array and a struct within a struct wrapped by Swig ?

Hello,

I spent almost a week to be able to embed Python within my C++ game
engine.
I wrote a mini-tutorial of what I was able to do so far here :
http://forums.indiegamer.com/showpos...52&postcount=9

At the end of my tutorial, I wrote : "You can do the same with C++
classes, and with structures within structures. Swig can parse & wrap
most C/C++ declarations."

Gloups, I was so wrong..! :-S

It seems that I can read a struct within my struct, but I cannot write
values to it.
Also, I cannot access the array from within my struct.

Any help to achieve this would be very welcome.

Here the problem in detail :
I'm using Python 2.5.2 & Swig 1.3.36.

The structures :
struct SIn
{
int in1, in2;
};

struct SPythoned
{
SIn Inner;
float Skill[16];
};

The python code :
=======================
def TestObject(o):
print(type(o))
print(type(o.Skill))
print(o.Skill)
print(type(o.Inner))
print(type(o.Inner.in1))
print(o.Inner.in1)
print(o.Inner.in2)

o.Inner.in1 = 12
o.Inner.in2 = 17

print(o.Inner.in1)
print(o.Inner.in2)
=======================
It gives me this output :
=======================
<class 'TE2008.SPythoned'>
<type 'PySwigObject'>
_20fd1300_p_float
<class 'TE2008.SIn'>
<type 'int'>
2
22
2
22
=======================

So althought I can read the value, the "o.Inner.inX = Y" are without
effect..! (and checking the value in the object in the C part confirms
this).

Moreover, if I add these lines :
for i in range(16) :
print(o.Skill[i])

I get this error :
Traceback (most recent call last):
File "d:\TE 2008\PyTest.py", line 32, in TestObject
print(o.Skill[i])
TypeError: 'PySwigObject' object is unsubscriptable

Any idea how to make this work ..?
Aug 16 '08 #1
5 7198
Ok, Swig needs the structure to be declared like this :
typedef struct SIn
{
int in1, in2;
} SIn;

Else, it doesn't understand it's a structure... :-S
Now, I can write to it.

I also found out that Swig didn't allow to write to the array :
http://www.swig.org/Doc1.3/SWIG.html#SWIG_nn34 ... :-S
So I just have to find out how to read the array, and I'll be all
good..!
Aug 16 '08 #2
Hi,
If you have struct defined in .h file then it's ok or can dea easyily
with struct written with .i file,
in .h file,
struct def
{
int a;
char name[10];
};
can be referenced as
>>strvar=new_def() //now strvar is your
object for struct def
and can access a and name as, get and
set for individual member see,
>>def_a_set(strvar,10)
def_a_get(strvar) will return 10.
or you can define struct as,

typedef struct
{
int sage;
}coll;
within .h file and access similarly.

regard's
Anish Chapagain
www.mysticnepal.com

On Aug 16, 11:38*am, emmanuel.rivo...@gmail.com wrote:
Hello,

I spent almost a week to be able to embed Python within my C++ game
engine.
I wrote a mini-tutorial of what I was able to do so far here :http://forums.indiegamer.com/showpos...52&postcount=9

At the end of my tutorial, I wrote : "You can do the same with C++
classes, and with structures within structures. Swig can parse & wrap
most C/C++ declarations."

Gloups, I was so wrong..! :-S

It seems that I can read a struct within my struct, but I cannot write
values to it.
Also, I cannot access the array from within my struct.

Any help to achieve this would be very welcome.

Here the problem in detail :
I'm using Python 2.5.2 & Swig 1.3.36.

The structures :
struct SIn
{
* * * * int in1, in2;

};

struct SPythoned
{
* * * * SIn Inner;
* * * * float Skill[16];

};

The python code :
=======================
def TestObject(o):
* * * * print(type(o))
* * * * print(type(o.Skill))
* * * * print(o.Skill)
* * * * print(type(o.Inner))
* * * * print(type(o.Inner.in1))
* * * * print(o.Inner.in1)
* * * * print(o.Inner.in2)

* * * * o.Inner.in1 = 12
* * * * o.Inner.in2 = 17

* * * * print(o.Inner.in1)
* * * * print(o.Inner.in2)
=======================

It gives me this output :
=======================
<class 'TE2008.SPythoned'>
<type 'PySwigObject'>
_20fd1300_p_float
<class 'TE2008.SIn'>
<type 'int'>
2
22
2
22
=======================

So althought I can read the value, the "o.Inner.inX = Y" are without
effect..! (and checking the value in the object in the C part confirms
this).

Moreover, if I add these lines :
* * * * for i in range(16) :
* * * * * * * * print(o.Skill[i])

I get this error :
Traceback (most recent call last):
* File "d:\TE 2008\PyTest.py", line 32, in TestObject
* * print(o.Skill[i])
TypeError: 'PySwigObject' object is unsubscriptable

Any idea how to make this work ..?
---------------
Aug 16 '08 #3
Anish Chapagain, I already know how to use structure, as my example
shown it.
I had trouble only with the nested structures, and it was coz of
missing typedef (ie: Swig need C struct declaration, not C++).

And I still cannot find how to get my arrays... :-S
Aug 18 '08 #4
On Aug 17, 11:30*pm, emmanuel.rivo...@gmail.com wrote:
Anish Chapagain, I already know how to use structure, as my example
shown it.
I had trouble only with the nested structures, and it was coz of
missing typedef (ie: Swig need C struct declaration, not C++).

And I still cannot find how to get my arrays... :-S
I was having the same problem with arrays and found a workaround here:
http://www.swig.org/papers/PyTutoria...Tutorial97.pdf
slide 39, "Helper Functions" I got it to work, hope this helps
Aug 19 '08 #5
Thanks to point this out.
It confirmed that I thought : must _manually_ write set/get functions
for every array in a every structure (it looks like middle-age ;) ).
Coz of this and a few other stuff, while looking at my code, I came to
the conclusion that I will be better writing interface C structs
between my code & the Python modules. It's really not the most
convenient way as any change to my initial structs will have to be
forwarded the interface structs. But hopefully it won't be too much
work, as my engine is already quite mature, and thus, shouldn't see
many changes anymore...
Aug 21 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
1
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will...
5
by: Robert | last post by:
Hi, This might be a strange question but i would like to know how to return an array from a function. Do you have to use pointers for this? Thanks in advance, Robert
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
5
by: Krish | last post by:
Hello People I hope I am On Topic. Anyways, here is my problem. Any insights would be really appreciated. I have wrapped a C IO module using SWIG -> Python Module. Suppose the name of the...
5
by: partha.p.das | last post by:
Here is my class: class MyClass { public: MyClass(); ~MyClass(); private:
4
by: Jazi | last post by:
Hello All, I have a function in C++ that takes an array of struct. I want to convert this function to java using SWIG. Here what I have structs.h typedef struct myStruct{ char name;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.