Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Send string to C++ Code (SWIG tcl VS Express)

90 views
Skip to first unread message

de Ribet Clément

unread,
Jul 17, 2012, 3:38:45 AM7/17/12
to
Hi,
I have recently started to use Swig to be able to use TCL info in C++ code.
I have a slight problem that I cannot solve...
I have a list that i want to process with a DLL coded with C++. I (think) i transformed my list to be readable under C++ and i made a pointer to this list.

Here is the code i have to do this :
BEGIN CODE

In C++ environnement :

#include<stdlib.h>
#include<stdio.h>

double *double_array(int size) {
return (double *) malloc(size*sizeof(double));
}

/* Get a value from an array */
double double_get(double *a, int index) {
return a[index];
}
/* Set a value in the array */
double double_set(double *a, int index, double value) {
return (a[index] = value);
}
In Tcl Environnement :
proc ::Process_Results::DoubleListToArray { l } {

set length [llength $l]
set a [double_array $length]
set i 0
foreach item $l {
double_set $a $i $item
incr i 1
}
return $a
}
proc ::Process_Results::mainproc { args }{

$listeElem and listeEp are listes from a software i got back

set adrlisteElem [::Process_Results::DoubleListToArray $listeElem ]
set adrlisteEp [::Process_Results::DoubleListToArray $listeEp ]

$ with this stuff i get back a pointer on my lists


ENDCODE
The lists are build with numbers only.
My question is : How do i pass this pointer to a c++ function in my DLL.
Someone could give me a prototype of function please.

Thanks a lot in advance !

Clément

Robert Heller

unread,
Jul 17, 2012, 5:57:52 AM7/17/12
to
At Tue, 17 Jul 2012 00:38:45 -0700 (PDT) =?ISO-8859-1?Q?de_Ribet_Cl=E9ment?= <der...@gmail.com> wrote:

>
> Hi,
> I have recently started to use Swig to be able to use TCL info in C++ code.
> I have a slight problem that I cannot solve...
> I have a list that i want to process with a DLL coded with C++. I (think) i=
> transformed my list to be readable under C++ and i made a pointer to this =
> list.

Read the SWIG documentation *carefully*, especially the chapter on
typemaps and especially the part about typemaps for arrays.

>
> Here is the code i have to do this :
> BEGIN CODE
>
> In C++ environnement :
>
> #include<stdlib.h>
> #include<stdio.h>
>
> double *double_array(int size) {
> return (double *) malloc(size*sizeof(double));
> }
>
> /* Get a value from an array */
> double double_get(double *a, int index) {
> return a[index];
> }
> /* Set a value in the array */
> double double_set(double *a, int index, double value) {
> return (a[index] =3D value);
> }
> In Tcl Environnement :
> proc ::Process_Results::DoubleListToArray { l } {
> =20
> set length [llength $l]
> set a [double_array $length]
> set i 0
> foreach item $l {
> double_set $a $i $item
> incr i 1
> }
> return $a
> }
> proc ::Process_Results::mainproc { args }{
>
> $listeElem and listeEp are listes from a software i got back
>
> set adrlisteElem [::Process_Results::DoubleListToArray $listeElem ]
> set adrlisteEp [::Process_Results::DoubleListToArray $listeEp ]
>
> $ with this stuff i get back a pointer on my lists
>
>
> ENDCODE
> The lists are build with numbers only.
> My question is : How do i pass this pointer to a c++ function in my DLL.=20
> Someone could give me a prototype of function please.
>
> Thanks a lot in advance !
>
> Cl=E9ment
>

--
Robert Heller -- 978-544-6933 / hel...@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments



Georgios Petasis

unread,
Jul 17, 2012, 6:32:42 AM7/17/12
to
Στις 17/7/2012 12:57, ο/η Robert Heller έγραψε:
> At Tue, 17 Jul 2012 00:38:45 -0700 (PDT) =?ISO-8859-1?Q?de_Ribet_Cl=E9ment?= <der...@gmail.com> wrote:
>
> Read the SWIG documentation *carefully*, especially the chapter on
> typemaps and especially the part about typemaps for arrays.
>

I second that. Why don't you simply use vectors?

%include "std_vector.i"

%template(Item) std::vector<int>;

This will create the "Item" command in Tcl, that creates vectors.

George

de Ribet Clément

unread,
Jul 18, 2012, 8:58:47 AM7/18/12
to
Le mardi 17 juillet 2012 12:32:42 UTC+2, Georgios Petasis a écrit :
> Στις 17/7/2012 12:57, ο/η Robert Heller έγραψε:
> &gt; At Tue, 17 Jul 2012 00:38:45 -0700 (PDT) =?ISO-8859-1?Q?de_Ribet_Cl=E9ment?= &lt;der...@gmail.com&gt; wrote:
> &gt;
> &gt; Read the SWIG documentation *carefully*, especially the chapter on
> &gt; typemaps and especially the part about typemaps for arrays.
> &gt;
>
> I second that. Why don&#39;t you simply use vectors?
>
> %include &quot;std_vector.i&quot;
>
> %template(Item) std::vector&lt;int&gt;;
>
> This will create the &quot;Item&quot; command in Tcl, that creates vectors.
>
> George

Thanks for the answers.
I managed to make it work a bit
I tried also
%template (Matrix) std::vector<std::vector<double>>
to make a matrix, but it don't work.
it won't work either i guess if i do

set a [DoubleVector]
set b [DoubleVector]

$a push $b

An idea ?

thank you for the time spent !

Clément

Georgios Petasis

unread,
Jul 18, 2012, 9:10:54 AM7/18/12
to der...@gmail.com
Στις 18/7/2012 15:58, ο/η de Ribet Clément έγραψε:
> Thanks for the answers.
> I managed to make it work a bit
> I tried also
> %template (Matrix) std::vector<std::vector<double>>
> to make a matrix, but it don't work.
> it won't work either i guess if i do
>
> set a [DoubleVector]
> set b [DoubleVector]
>
> $a push $b
>
> An idea ?
>
> thank you for the time spent !
>
> Clément
>

I see no reason it won't work. What exactly have you tried?

George

de Ribet Clément

unread,
Jul 18, 2012, 9:18:52 AM7/18/12
to der...@gmail.com
Le mercredi 18 juillet 2012 15:10:54 UTC+2, Georgios Petasis a écrit :
> Στις 18/7/2012 15:58, ο/η de Ribet Clément έγραψε:
> &gt; Thanks for the answers.
> &gt; I managed to make it work a bit
> &gt; I tried also
> &gt; %template (Matrix) std::vector&lt;std::vector&lt;double&gt;&gt;
> &gt; to make a matrix, but it don&#39;t work.
> &gt; it won&#39;t work either i guess if i do
> &gt;
> &gt; set a [DoubleVector]
> &gt; set b [DoubleVector]
> &gt;
> &gt; $a push $b
> &gt;
> &gt; An idea ?
> &gt;
> &gt; thank you for the time spent !
> &gt;
> &gt; Clément
> &gt;
>
> I see no reason it won&#39;t work. What exactly have you tried?
>
> George

set dv [DoubleVector] sends me back smtg like _305cd400_p_std__vectorT_double_t in the dv variable and in my .I i have %template(DoubleVector) vector<double>;

%template(DoubleVector) vector<vector<double>>; throws me back an error when creating the wrap file.

Clément

de Ribet Clément

unread,
Jul 18, 2012, 9:47:59 AM7/18/12
to der...@gmail.com
Le mercredi 18 juillet 2012 15:18:52 UTC+2, de Ribet Clément a écrit :
> Le mercredi 18 juillet 2012 15:10:54 UTC+2, Georgios Petasis a écrit :
> &gt; Στις 18/7/2012 15:58, ο/η de Ribet Clément έγραψε:
> &gt; &amp;gt; Thanks for the answers.
> &gt; &amp;gt; I managed to make it work a bit
> &gt; &amp;gt; I tried also
> &gt; &amp;gt; %template (Matrix) std::vector&amp;lt;std::vector&amp;lt;double&amp;gt;&amp;gt;
> &gt; &amp;gt; to make a matrix, but it don&amp;#39;t work.
> &gt; &amp;gt; it won&amp;#39;t work either i guess if i do
> &gt; &amp;gt;
> &gt; &amp;gt; set a [DoubleVector]
> &gt; &amp;gt; set b [DoubleVector]
> &gt; &amp;gt;
> &gt; &amp;gt; $a push $b
> &gt; &amp;gt;
> &gt; &amp;gt; An idea ?
> &gt; &amp;gt;
> &gt; &amp;gt; thank you for the time spent !
> &gt; &amp;gt;
> &gt; &amp;gt; Clément
> &gt; &amp;gt;
> &gt;
> &gt; I see no reason it won&amp;#39;t work. What exactly have you tried?
> &gt;
> &gt; George
>
> set dv [DoubleVector] sends me back smtg like _305cd400_p_std__vectorT_double_t in the dv variable and in my .I i have %template(DoubleVector) vector&lt;double&gt;;
>
> %template(DoubleVector) vector&lt;vector&lt;double&gt;&gt;; throws me back an error when creating the wrap file.
>
> Clément

my bad, a little syntax problem with the namespaces.
In My .i file i had
namespace std {
%template(DoubleVector) vector<double>;
}
deleting the "namespace std" and adding the std:: , it worked.
thanks a lot
0 new messages