How to access pointers in Lua?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to access pointers in Lua?

DONGIEUX-AKKA Christophe (SAGEM DEFENSE SECURITE)
Hello

I have a C++ class in which there is a pointer to another class.
I'd like to access this second class in Lua, but I don't know how to do. When I try, Lua does not crash but does not work as expected, and I can't get my class back in C++...

Here is some code to explain:

C++:
class Base_p
{
public:
        Base_p() { std::cout << "Base_p()" << std::endl; m_toto = 123; }
        int getToto() { return m_toto; }
        void setToto( int val ) { m_toto = val; }
private:
        int m_toto;
};

class Base
{
public:
        Base() { std::cout << "Base()" << std::endl; p_ptr = new Base_p; }
        virtual ~Base() { std::cout << "~Base()" << std::endl; delete p_ptr; p_ptr = 0; }
        virtual void description() { std::cout << "Base::description()" << std::endl; }

        Base_p* p_ptr;
};

...

// Export our class with LuaBind
        luabind::module( pState ) [

                luabind::class_< Base_p >( "Base_p" )
                        .def( luabind::constructor<>() )
                        .def( "getToto", &Base_p::getToto )
                        .def( "setToto", &Base_p::setToto ),

                luabind::class_< Base >( "Base" )
                        .def( luabind::constructor<>() )
                        .def( "description", &Base::description )
                        .def_readwrite( "p_ptr", &Base::p_ptr )
        ];

...

I process the below Lua script with luaL_dostring()
And I get my Base instance back in C++:
luabind::object global_vars = luabind::globals( pState );
Base* base = luabind::object_cast<Base*>( global_vars["b"] ); // return a null pointer...



Lua:
b = Base()
b:description()
foo = b:p_ptr
print( "Lua: toto=", foo:getToto() )
print( "Lua: setToto(321)", foo:setToto(321) )


Do you have an idea? Thanks in advance.

Chris.
#
" Ce courriel et les documents qui lui sont joints peuvent contenir des
informations confidentielles ou ayant un caractère privé. S'ils ne vous sont
pas destinés, nous vous signalons qu'il est strictement interdit de les
divulguer, de les reproduire ou d'en utiliser de quelque manière que ce
soit le contenu. Si ce message vous a été transmis par erreur, merci d'en
informer l'expéditeur et de supprimer immédiatement de votre système
informatique ce courriel ainsi que tous les documents qui y sont attachés."
                                                                ******
" This e-mail and any attached documents may contain confidential or
proprietary information. If you are not the intended recipient, you are
notified that any dissemination, copying of this e-mail and any attachments
thereto or use of their contents by any means whatsoever is strictly
prohibited. If you have received this e-mail in error, please advise the
sender immediately and delete this e-mail and all attached documents
from your computer system."
#
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user