how to derive lua class from c++ class

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

how to derive lua class from c++ class

kunshou-2

 

 

发件人: kunshou [mailto:[hidden email]]
发送时间: 201212 2:24
收件人: [hidden email]
主题: how to derive lua class from c++ class

 

Hi

I just stepped into luabind 0.9.1. I am trying to derive a class in lua from a c++ class.

My c++ class looks like

class testclass

{

public:

    testclass(const char* s): m_string(s)

    {std::cout << m_string << " @created." << std::endl;}

    void print_string() { std::cout <<"testclass:" <<m_string << "\n"; }

 

private:

    std::string m_string;

};

 

The lua code

 

class 'derivedclass' (testclass)

  function derivedclass:__init(name)

    testclass:__init(name);

  end

 

testderivedclass = derivedclass("hi")

testderivedclass:print_string()

 

when I run this lua code in c++ using luaL_dofile, the program always crashes.

 

Could anyone please tell what’s wrong with my code, or give a right example?

 

Many thanks


------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: how to derive lua class from c++ class

David Osipyan-3
Hi,

Here is a good sample
http://forums.tigsource.com/index.php?topic=3149.0

Best,
David

2012/1/8 kunshou <[hidden email]>

 

 

发件人: kunshou [mailto:[hidden email]]
发送时间: 201212 2:24
收件人: [hidden email]
主题: how to derive lua class from c++ class

 

Hi

I just stepped into luabind 0.9.1. I am trying to derive a class in lua from a c++ class.

My c++ class looks like

class testclass

{

public:

    testclass(const char* s): m_string(s)

    {std::cout << m_string << " @created." << std::endl;}

    void print_string() { std::cout <<"testclass:" <<m_string << "\n"; }

 

private:

    std::string m_string;

};

 

The lua code

 

class 'derivedclass' (testclass)

  function derivedclass:__init(name)

    testclass:__init(name);

  end

 

testderivedclass = derivedclass("hi")

testderivedclass:print_string()

 

when I run this lua code in c++ using luaL_dofile, the program always crashes.

 

Could anyone please tell what’s wrong with my code, or give a right example?

 

Many thanks


------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: how to derive lua class from c++ class

Алексей-36
In reply to this post by kunshou-2
Hi
Here is very clear described how to derive a class in Lua from C++ class.
For your example you should have something like that:

class testclass

{

public:

    testclass(const char* s): m_string(s)

    {std::cout << m_string << " @created." << std::endl;}

    virtual void print_string() { std::cout <<"testclass:" <<m_string << "\n"; }

 

private:

    std::string m_string;

};
struct testclass_wrapper : testclass, luabind:wrap_base
{
    testclass_wrapper(const char* s):
        testclass(s) {}

    virtual void print_string()
    {
        luabind::call_member<void>(this,"print_string");
    }

    static void default_print_string(testclass * ptr)
    {
        (ptr->testclass::print_string());
    }
};

...

module(L)
[
    class_<testclass, testclass_wrapper>("testclass")
    .def(constructor<const char*>())
    .def("print_string", &testclass::print_string, &testclass_wrapper::default_print_string)
];

The Lua code:

class 'derivedclass' (testclass)


function derivedclass:__init(name)

   testclass.__init(self, name)

end


function derivedclass:print_string()

   testclass.print_string(self)

   print("This is derived class in Lua")

end

 

testderivedclass = derivedclass("hi")

testderivedclass:print_string()


2012/1/8 kunshou <[hidden email]>

 

 

发件人: kunshou [mailto:[hidden email]]
发送时间: 201212 2:24
收件人: [hidden email]
主题: how to derive lua class from c++ class

 

Hi

I just stepped into luabind 0.9.1. I am trying to derive a class in lua from a c++ class.

My c++ class looks like

class testclass

{

public:

    testclass(const char* s): m_string(s)

    {std::cout << m_string << " @created." << std::endl;}

    void print_string() { std::cout <<"testclass:" <<m_string << "\n"; }

 

private:

    std::string m_string;

};

 

The lua code

 

class 'derivedclass' (testclass)

  function derivedclass:__init(name)

    testclass:__init(name);

  end

 

testderivedclass = derivedclass("hi")

testderivedclass:print_string()

 

when I run this lua code in c++ using luaL_dofile, the program always crashes.

 

Could anyone please tell what’s wrong with my code, or give a right example?

 

Many thanks


------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




--
Best regards,
Aliaksei Baiburyn

------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

答复: how to derive lua class from c++ class

kunshou-2

Thanks so much.

I misunderstood the manual the first time I read it

Anyway, Problem solved

 

 

发件人: Алексей [mailto:[hidden email]]
发送时间: 201219 1:57
收件人: [hidden email]
主题: Re: [luabind] how to derive lua class from c++ class

 

Hi
Here is very clear described how to derive a class in Lua from C++ class.
For your example you should have something like that:

class testclass

{

public:

    testclass(const char* s): m_string(s)

    {std::cout << m_string << " @created." << std::endl;}

    virtual void print_string() { std::cout <<"testclass:" <<m_string << "\n"; }

 

private:

    std::string m_string;

};
struct testclass_wrapper : testclass, luabind:wrap_base
{
    testclass_wrapper(const char* s):
        testclass(s) {}

    virtual void print_string()
    {
        luabind::call_member<void>(this,"print_string");

    }

    static void default_print_string(testclass * ptr)
    {
        (ptr->testclass::print_string());

    }
};

...

module(L)
[
    class_<testclass, testclass_wrapper>("testclass")

    .def(constructor<const char*>())
    .def("print_string", &testclass::print_string, &testclass_wrapper::default_print_string)
];

The Lua code:

class 'derivedclass' (testclass)

 

function derivedclass:__init(name)

   testclass.__init(self, name)

end

 

function derivedclass:print_string()

   testclass.print_string(self)

   print("This is derived class in Lua")

end

 

testderivedclass = derivedclass("hi")

testderivedclass:print_string()

2012/1/8 kunshou <[hidden email]>

 

 

发件人: kunshou [mailto:[hidden email]]
发送时间: 201212 2:24
收件人: [hidden email]
主题: how to derive lua class from c++ class

 

Hi

I just stepped into luabind 0.9.1. I am trying to derive a class in lua from a c++ class.

My c++ class looks like

class testclass

{

public:

    testclass(const char* s): m_string(s)

    {std::cout << m_string << " @created." << std::endl;}

    void print_string() { std::cout <<"testclass:" <<m_string << "\n"; }

 

private:

    std::string m_string;

};

 

The lua code

 

class 'derivedclass' (testclass)

  function derivedclass:__init(name)

    testclass:__init(name);

  end

 

testderivedclass = derivedclass("hi")

testderivedclass:print_string()

 

when I run this lua code in c++ using luaL_dofile, the program always crashes.

 

Could anyone please tell whats wrong with my code, or give a right example?

 

Many thanks


------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




--
Best regards,
Aliaksei Baiburyn


------------------------------------------------------------------------------
RSA(R) Conference 2012
Mar 27 - Feb 2
Save $400 by Jan. 27
Register now!
http://p.sf.net/sfu/rsa-sfdev2dev2
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user