Ruby and Swig -- creating Pointers (typedef)

R

RubyQuestions

I have been using Swig for the first time since Lyle mentioned Phil
Tomson's article in C/C++ Users Journal (http://www.cuj.com)

My goal is to use my company's C/C++ API with Ruby.
I think it would add a great deal of flexibility to my current
programming.

I let Swig automatically create the wrapper for me (using the "truly
lazy way" interface...just pointing it to the header file.)

Amazingly, everything is working.
I'm able to load the shared library into Ruby.
I can see all the methods, constants, etc...

However, some of the methods I want to use require pointers.
Sorry for my ignorance, but I have only programmed in Java and Ruby...
so I don't have any true exposure to C/C++ or the concept of pointers.

In the header file (API) I have are some typedef statments:

/********************************/
/* */
/* Global Data Types */
/* */
/********************************/
typedef char CoolBoolean;
typedef char* CoolString;
typedef int CoolClientHandle;
typedef int CoolTableHandle;



Now I want use the following connect method.

/****************************************************************/
/* */
/* Opening and Closing Connection */
/* */
/****************************************************************/

extern DllDecl CoolReturnCode CoolConnect
PROTO((char *srvHostName,
int serverNumber,
CoolClientHandle *handle));


How do I create a pointer of type "CoolClientHandle" in Ruby?
Am I thinking about this incorrectly?

I have read over the pointer documentation, however I have been unable
to make it work.

Any advice would be appreciated.

Thanks.
 
P

Phil Tomson

I have been using Swig for the first time since Lyle mentioned Phil
Tomson's article in C/C++ Users Journal (http://www.cuj.com)

My goal is to use my company's C/C++ API with Ruby.
I think it would add a great deal of flexibility to my current
programming.

I let Swig automatically create the wrapper for me (using the "truly
lazy way" interface...just pointing it to the header file.)

Amazingly, everything is working.
I'm able to load the shared library into Ruby.
I can see all the methods, constants, etc...

However, some of the methods I want to use require pointers.
Sorry for my ignorance, but I have only programmed in Java and Ruby...
so I don't have any true exposure to C/C++ or the concept of pointers.

In the header file (API) I have are some typedef statments:

/********************************/
/* */
/* Global Data Types */
/* */
/********************************/
typedef char CoolBoolean;
typedef char* CoolString;
typedef int CoolClientHandle;
typedef int CoolTableHandle;



Now I want use the following connect method.

/****************************************************************/
/* */
/* Opening and Closing Connection */
/* */
/****************************************************************/

extern DllDecl CoolReturnCode CoolConnect
PROTO((char *srvHostName,
int serverNumber,
CoolClientHandle *handle));


How do I create a pointer of type "CoolClientHandle" in Ruby?
Am I thinking about this incorrectly?

I have read over the pointer documentation, however I have been unable
to make it work.

Any advice would be appreciated.

Thanks.

You need to include the typedefs in the declaration portion of the .i file
(or you need to include the .h file where the typedef is defined).

for example, let's say you have the typedef in the file foo.h:

//foo.h
typedef int CoolClientHandle;
typedef int CoolTableHandle;
///end of file///

You need to include it in your .i file like:

%{
/*this get's put in your wrapper code*/
#include "foo.h"
%}
/*no you've got to tell SWIG about it*/
typedef int CoolClientHandle;
typedef int CoolTableHandle; //yeah, I know it seems redundant



You should be able to pass in an integer from the Ruby side, swig will take
care of the pointer referencing/dereferencing.

If it's still not working, please post your .i file.

Phil
 
P

Phil Tomson

(e-mail address removed) (RubyQuestions) wrote in message


You need to include the typedefs in the declaration portion of the .i file
(or you need to include the .h file where the typedef is defined).

for example, let's say you have the typedef in the file foo.h:

//foo.h
typedef int CoolClientHandle;
typedef int CoolTableHandle;
///end of file///

A couple of corrections:
You need to include it in your .i file like:

%{
/*this get's put in your wrapper code*/
#include "foo.h"
%}
/*now you've got to tell SWIG about it*/
typedef int CoolClientHandle;
typedef int CoolTableHandle; //yeah, I know it seems redundant
It would be better if you just did:

%include "foo.h"
You should be able to pass in an integer from the Ruby side, swig will take
care of the pointer referencing/dereferencing.


See Lyle's post on pointers being used as output parameters.

Phil
 
R

RubyQuestions

Lyle and Phil,

Thanks for the advice...it worked perfectly.

changing the way the method passes back a "CoolClientHandle" value for
*handle...
%apply int *OUTPUT { CoolClientHandle *handle };

and then in Ruby grabbing the handle output worked...
returnCode, handle = Cool.CoolConnect("myServer", 6667)

However, I now ran into a "char*** problem".

For example, I want to use the following method.

Here is what the header file looks like...

extern DllDecl CoolReturnCode CoolGetRelChildren
PROTO((CoolClientHandle handle,
char *relName,
int *numChildren,
char ***children));


I'm pretty sure that char ***children is suppose to give me an array
of strings.

Unfortunately, I can't do "%apply char* *OUTPUT {char ***children}".
How do I get an array or some other type of object pointer back (like
a struct with many values and variables).

Is this where I start to use "typemap(out)" definition or
"typemap(varout)"?

I hope this does not seem too basic.
Thank you for getting me up an running.

P.S. Where is the best starting place for someone with my background
(only using Java and Ruby) to start learning C?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top