Private constructors / destructors

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Private constructors / destructors

Jacek Sieka
Hi,

I'm wrapping a class that has a private constructor and destructor (it's a singleton accessible
through a single instance function). I'm trying to make this class accessible to my lua scripts, but
can't figure out just quite how to do it. I'm using gcc 3.4.5, lua 5.1, luawrap 0.7 (with a few
extra #defines to make it compile)

I've tried setting up a fake pointer wrapper and using the smart pointer interface like so:
template<typename T>
struct pointer_wrapper {
        pointer_wrapper() : t(0) { }
        pointer_wrapper(T*) { }
       
        operator T*() { return t; }
        T* t;
};
namespace luabind {
        template<typename T> T* get_pointer(pointer_wrapper<T>& p) { return (T*)p; }
        template<typename T> pointer_wrapper<const T>* get_const_holder(pointer_wrapper<T>*) { return 0; }
}

and then mapping my class:

class A {
private:
        virtual ~A() { }
public:
        void hello() {}
};

typedef pointer_wrapper<A> LuaA;

A* getA() { return 0; /** not really but for this example */ }

void wrap_x(lua_State* L) {
        module(L, "dummy")
        [
                class_<A, LuaA>("A")
                        //.def("hello", &A::hello)
                ,def("getA", &getA)
        ];
}

This works as long as I don't uncomment the def("hello"... line. If I do, I get 5 km of template
expressions which boil down to the call static_cast<T*>(ptr)->~T(); (object_rep.hpp, line 108) being
called from if ((void*)ptr == (char*)target) destructor = detail::destruct_only_s<T>::apply;
(policy.hpp line 529)


Does anyone have any bright ideas for this situation (essentially, mapping types that should never
be neither created nor destroyed by scripts)?

Regards
Jacek Sieka



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Iterating through a table (luabind::object)

Jesse Van Beurden
Hi,
I'd like to iterate through a lua table just printing out the key/value  
along the way. I'm not really sure how to get the data out of the iterator  
though.

Here is what I have so far (myLuaObject is guaranteed to be a table):

//...
for(luabind::iterator i(myLuaObject), end; i != end; i++) {
    printf("%s = %s (or %d for 'number')\n", __KEY__, __VALUE__);
}
//...

I believe i.key() gives a luabind object? How do you get the string for  
the key out of it? And how do you extract the string (or number) for the  
value as well (type(value) is also guaranteed to always be either 'string'  
or 'number')? I haven't been able to find an answer by searching the  
mailing list, or reading the luabind source.

Thank you,
Jesse Van Beurden.



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Iterating through a table (luabind::object)

anders langlands
Dereferencing the iterator gives you a luabind::object, similarly object.key() gives you another object. Use object_cast<> to get your data back

for ( luabind::iterator i( myLuaObject ), end; i != end; ++i )
{
    string strKey = object_cast<string>( i.key() );

    luabind::object val = *i;

}



On 5/11/06, Jesse Van Beurden <[hidden email]> wrote:
Hi,
I'd like to iterate through a lua table just printing out the key/value
along the way. I'm not really sure how to get the data out of the iterator
though.

Here is what I have so far (myLuaObject is guaranteed to be a table):

//...
for(luabind::iterator i(myLuaObject), end; i != end; i++) {
    printf("%s = %s (or %d for 'number')\n", __KEY__, __VALUE__);
}
//...

I believe i.key() gives a luabind object? How do you get the string for
the key out of it? And how do you extract the string (or number) for the
value as well (type(value) is also guaranteed to always be either 'string'
or 'number')? I haven't been able to find an answer by searching the
mailing list, or reading the luabind source.

Thank you,
Jesse Van Beurden.



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

Reply | Threaded
Open this post in threaded view
|

Re: Iterating through a table (luabind::object)

anders langlands
In reply to this post by Jesse Van Beurden
Dereferencing the iterator gives you a luabind::object, similarly object.key() gives you another object. Use object_cast<> to get your data back

for ( luabind::iterator i( myLuaObject ), end; i != end; ++i )
{
    string strKey = object_cast<string>( i.key() );

    luabind::object val = *i;
   
    if ( luabind::type( val ) == LUA_TNUMBER )
    {
        // ... deal with number type e.g. object_cast<double>( val )
    }
    else if ( luabind::type( val ) == LUA_TSTRING )
    {
        // deal with string type e.g. object_cast<string>( val )
    }
    else
    {
        // handle error
    }
}

Anders

On 5/11/06, Jesse Van Beurden <[hidden email]> wrote:
Hi,
I'd like to iterate through a lua table just printing out the key/value
along the way. I'm not really sure how to get the data out of the iterator
though.

Here is what I have so far (myLuaObject is guaranteed to be a table):

//...
for(luabind::iterator i(myLuaObject), end; i != end; i++) {
    printf("%s = %s (or %d for 'number')\n", __KEY__, __VALUE__);
}
//...

I believe i.key() gives a luabind object? How do you get the string for
the key out of it? And how do you extract the string (or number) for the
value as well (type(value) is also guaranteed to always be either 'string'
or 'number')? I haven't been able to find an answer by searching the
mailing list, or reading the luabind source.

Thank you,
Jesse Van Beurden.



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

Reply | Threaded
Open this post in threaded view
|

Re: Iterating through a table (luabind::object)

Jesse Van Beurden
Thank you, it worked like a charm. I'd assumed it would be something along  
those lines :).

And apologies for nesting this discussion inside Jacek Sieka's post - it  
was late at night :).

Jesse.


On Thu, 11 May 2006 19:37:51 +0930, anders langlands  
<[hidden email]> wrote:

> Dereferencing the iterator gives you a luabind::object, similarly  
> object.key()
> gives you another object. Use object_cast<> to get your data back
>
> for ( luabind::iterator i( myLuaObject ), end; i != end; ++i )
> {
>     string strKey = object_cast<string>( i.key() );
>
>     luabind::object val = *i;
>
>     if ( luabind::type( val ) == LUA_TNUMBER )
>     {
>         // ... deal with number type e.g. object_cast<double>( val )
>     }
>     else if ( luabind::type( val ) == LUA_TSTRING )
>     {
>         // deal with string type e.g. object_cast<string>( val )
>     }
>     else
>     {
>         // handle error
>     }
> }
>
> Anders



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user