473,667 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Sk ill))
print(o.Skill)
print(type(o.In ner))
print(type(o.In ner.in1))
print(o.Inner.i n1)
print(o.Inner.i n2)

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

print(o.Inner.i n1)
print(o.Inner.i n2)
=============== ========
It gives me this output :
=============== ========
<class 'TE2008.SPython ed'>
<type 'PySwigObject'>
_20fd1300_p_flo at
<class 'TE2008.SIn'>
<type 'int'>
2
22
2
22
=============== ========

So althought I can read the value, the "o.Inner.in X = 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 7219
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_de f() //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(str var,10)
def_a_get(str var) 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.Sk ill))
* * * * print(o.Skill)
* * * * print(type(o.In ner))
* * * * print(type(o.In ner.in1))
* * * * print(o.Inner.i n1)
* * * * print(o.Inner.i n2)

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

* * * * print(o.Inner.i n1)
* * * * print(o.Inner.i n2)
=============== ========

It gives me this output :
=============== ========
<class 'TE2008.SPython ed'>
<type 'PySwigObject'>
_20fd1300_p_flo at
<class 'TE2008.SIn'>
<type 'int'>
2
22
2
22
=============== ========

So althought I can read the value, the "o.Inner.in X = 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
1885
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 physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining
7
8852
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 want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
24
3799
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
1782
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 call INDV. The struct looks like // Some info used for the struct typedef unsigned char uint8; /* 8 bits */
5
1909
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
3513
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 accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can store a value in. The actual key argument is always of the same pointer type (as seen in the caller,...
5
7316
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 module is "imageio" and the reader function from the file is image_read() which returns an object ( "filled" C structure) with a lot of members.
5
2454
by: partha.p.das | last post by:
Here is my class: class MyClass { public: MyClass(); ~MyClass(); private:
4
7952
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
8458
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8366
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.