Quantcast

lua c++ extend functionality

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

lua c++ extend functionality

Luis C
Hi All,

I have a lua interface and I want to print some c++ objects  as the documentation says (using __str__() method). My interface have a lot of classes that have the method statistics implemented and I would like to have the interface more clean, instead of copy pastes of the same extension

%extend Object1 {
   const char *__str__() {
       static char tmp[1024];
       std::ostringstream ss;

       $self->statistics(ss);

       snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
       return tmp_buffer;
    }
}'

%extend Object2 {
   const char *__str__() {
       static char tmp[1024];
       std::ostringstream ss;

       $self->statistics(ss);

       snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
       return tmp_buffer;
    }
}'

%extend Object3{
    /* More and more */

Could be possible to have a generic function that just receive the $self parameter as the next example?

const char __str__(SWIG_Object a) {
       static char tmp[1024];
       std::ostringstream ss;

       a->statistics(ss);

       snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
       return tmp_buffer;
}

And on the extends just have

%extend Object1 {
    const char *__str__() { return __str__($self); }
};
%extend Object2 {
    const char *__str__() { return __str__($self); }
};
%extend Object3 {
    const char *__str__() { return __str__($self); }
};
%extend Object4 {
    const char *__str__() { return __str__($self); }
};

Thanks and regards,
Luis

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lua c++ extend functionality

William S Fulton
On 3 November 2016 at 14:24, Luis C <[hidden email]> wrote:

> Hi All,
>
> I have a lua interface and I want to print some c++ objects  as the
> documentation says (using __str__() method). My interface have a lot of
> classes that have the method statistics implemented and I would like to have
> the interface more clean, instead of copy pastes of the same extension
>
> %extend Object1 {
>    const char *__str__() {
>        static char tmp[1024];
>        std::ostringstream ss;
>
>        $self->statistics(ss);
>
>        snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
>        return tmp_buffer;
>     }
> }'
>
> %extend Object2 {
>    const char *__str__() {
>        static char tmp[1024];
>        std::ostringstream ss;
>
>        $self->statistics(ss);
>
>        snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
>        return tmp_buffer;
>     }
> }'
>
> %extend Object3{
>     /* More and more */
>
> Could be possible to have a generic function that just receive the $self
> parameter as the next example?
>
> const char __str__(SWIG_Object a) {
>        static char tmp[1024];
>        std::ostringstream ss;
>
>        a->statistics(ss);
>
>        snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
>        return tmp_buffer;
> }
>
> And on the extends just have
>
> %extend Object1 {
>     const char *__str__() { return __str__($self); }
> };
> %extend Object2 {
>     const char *__str__() { return __str__($self); }
> };
> %extend Object3 {
>     const char *__str__() { return __str__($self); }
> };
> %extend Object4 {
>     const char *__str__() { return __str__($self); }
> };
>
Either:

1) Write a SWIG macro containing the contents of the extend and then
invoke the macro for each class, so then you have a one liner for each
class.
2) Use a C++ template to implement the 'generic' function for your 4
%extend functions shown above.
3) Combination of both suggestions above.

William

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lua c++ extend functionality

Luis C
Hi William

Here is the solution with macros if someone is interested

%define OUTPUTHELPER(type,name)
    std::ostringstream ss;
    static char tmp[1024*1024];

    name->statistics(ss);
    snprintf(tmp,1024*1024,"%s",ss.str().c_str());
    return tmp;
%enddef

%extend Object1 {
const char *__str__() { OUTPUTHELPER(Object1,$self); }
}
%extend Object2 {
const char *__str__() { OUTPUTHELPER(Object2,$self); }
}
%extend Object3 {
const char *__str__() { OUTPUTHELPER(Object3,$self); }
}

Thanks for your idea William!

Regards,
Luis


2016-11-05 21:59 GMT+00:00 William S Fulton <[hidden email]>:
On 3 November 2016 at 14:24, Luis C <[hidden email]> wrote:
> Hi All,
>
> I have a lua interface and I want to print some c++ objects  as the
> documentation says (using __str__() method). My interface have a lot of
> classes that have the method statistics implemented and I would like to have
> the interface more clean, instead of copy pastes of the same extension
>
> %extend Object1 {
>    const char *__str__() {
>        static char tmp[1024];
>        std::ostringstream ss;
>
>        $self->statistics(ss);
>
>        snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
>        return tmp_buffer;
>     }
> }'
>
> %extend Object2 {
>    const char *__str__() {
>        static char tmp[1024];
>        std::ostringstream ss;
>
>        $self->statistics(ss);
>
>        snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
>        return tmp_buffer;
>     }
> }'
>
> %extend Object3{
>     /* More and more */
>
> Could be possible to have a generic function that just receive the $self
> parameter as the next example?
>
> const char __str__(SWIG_Object a) {
>        static char tmp[1024];
>        std::ostringstream ss;
>
>        a->statistics(ss);
>
>        snprintf(tmp_buffer,1024, "%s",ss.str().c_str());
>        return tmp_buffer;
> }
>
> And on the extends just have
>
> %extend Object1 {
>     const char *__str__() { return __str__($self); }
> };
> %extend Object2 {
>     const char *__str__() { return __str__($self); }
> };
> %extend Object3 {
>     const char *__str__() { return __str__($self); }
> };
> %extend Object4 {
>     const char *__str__() { return __str__($self); }
> };
>
Either:

1) Write a SWIG macro containing the contents of the extend and then
invoke the macro for each class, so then you have a one liner for each
class.
2) Use a C++ template to implement the 'generic' function for your 4
%extend functions shown above.
3) Combination of both suggestions above.

William


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...