How to get Lua class name from C++ class?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get Lua class name from C++ class?

Glen F
Hello there, I'm using luabind and trying to get the Lua subclass name from an instance of the base class in C++.

I want to use get_class_info(), but it expects an "argument" rather than an "object".  Perhaps there is a better way to do this, besides trying to figure out how to convert a luabind::object into a luabind::adl::argument…  Here is some example code that should illustrate the problem:

class BaseClass {
public:
    virtual ~BaseClass() {}
    std::string getClassName(lua_State* L);
    // ... more stuff...
};

class BaseClass_wrapper : public BaseClass, public luabind::wrap_base {
    // ... some stuff...
};

std::string BaseClass::getClassName(lua_State* L)
{
    BaseClass_wrapper* p = dynamic_cast<BaseClass_wrapper*>(this);
    std::string className("UNKNOWN");
    if (p != 0) {
        luabind::object myObject(L, p);
        luabind::class_info info = luabind::get_class_info(myObject); // no good!
        // no viable conversion from 'luabind::object' to 'const luabind::adl::argument'
        className = info.name;
    }
    return className;
}


Thanks for any suggestions,
Glen.


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
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 get Lua class name from C++ class?

eduard mueller
Hey,

you can
​ ​
use the "private" luabind::detail::object_rep* luabind::detail::get_instance(lua_State* L, int index)
​here ​
instead.
This is quite a hack, but we at found no other "official" way on doing this within a wrap_base:

​​
 std::string
​​ 
BaseClass::getClassName(lua_State* L)
 {
   luabind::detail::wrap_access::ref(*this).get(L);
   luabind::detail::object_rep* pObjectRep = luabind::detail::get_instance(L, -1);
   ::lua_pop(L, 1); //ref.get

   if (pObjectRep)
   {
     return
​ ​
std::string (pObjectRep->crep()->name());
   }
   else
   {
     throw std::runtime_error(
       "Internal Error: failed to resolve a Lua wrapper class name");
   }
 }

Hope this helps.

Best regards,
Eduard


On Wed, Aug 28, 2013 at 1:09 PM, Glen Fraser <[hidden email]> wrote:
>
> Hello there, I'm using luabind and trying to get the Lua subclass name from an instance of the base class in C++.
>
> I want to use get_class_info(), but it expects an "argument" rather than an "object".  Perhaps there is a better way to do this, besides trying to figure out how to convert a luabind::object into a luabind::adl::argument…  Here is some example code that should illustrate the problem:
>
> class BaseClass {
> public:
>     virtual ~BaseClass() {}
>     std::string getClassName(lua_State* L);
>     // ... more stuff...
> };
>
> class BaseClass_wrapper : public BaseClass, public luabind::wrap_base {
>     // ... some stuff...
> };
>
> std::string
> BaseClass::getClassName(lua_State* L)
> {
>     BaseClass_wrapper* p = dynamic_cast<BaseClass_wrapper*>(this);
>     std::string className("UNKNOWN");
>     if (p != 0) {
>         luabind::object myObject(L, p);
>         luabind::class_info info = luabind::get_class_info(myObject); // no good!
>         // no viable conversion from 'luabind::object' to 'const luabind::adl::argument'
>         className = info.name;

>     }
>     return className;
> }
>
>
> Thanks for any suggestions,
> Glen.
>
>
> ------------------------------------------------------------------------------
> Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
> Discover the easy way to master current and previous Microsoft technologies
> and advance your career. Get an incredible 1,500+ hours of step-by-step
> tutorial videos with LearnDevNow. Subscribe today and save!
> http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
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 get Lua class name from C++ class?

Christian N.
In reply to this post by Glen F

Just replace luabind::object with luabind::argument in your code. They work both the same, only that the first stores the referenced object in the registry, while the latter just leaves it on the stack.

 

If you ever have to convert one into another, just use the ones push() method and the others from_stack constructor (but avoid relative indices with luabind::argument).

 

From: Glen Fraser [mailto:[hidden email]]
Sent: Wednesday, August 28, 2013 1:09 PM
To: [hidden email]
Subject: [luabind] How to get Lua class name from C++ class?

 

Hello there, I'm using luabind and trying to get the Lua subclass name from an instance of the base class in C++.

 

I want to use get_class_info(), but it expects an "argument" rather than an "object".  Perhaps there is a better way to do this, besides trying to figure out how to convert a luabind::object into a luabind::adl::argument…  Here is some example code that should illustrate the problem:

 

class BaseClass {

public:

    virtual ~BaseClass() {}

    std::string getClassName(lua_State* L);

    // ... more stuff...

};

 

class BaseClass_wrapper : public BaseClass, public luabind::wrap_base {

    // ... some stuff...

};

 

std::string BaseClass::getClassName(lua_State* L)

{

    BaseClass_wrapper* p = dynamic_cast<BaseClass_wrapper*>(this);

    std::string className("UNKNOWN");

    if (p != 0) {

        luabind::object myObject(L, p);

        luabind::class_info info = luabind::get_class_info(myObject); // no good!

        // no viable conversion from 'luabind::object' to 'const luabind::adl::argument'

        className = info.name;

    }

    return className;

}

 

 

Thanks for any suggestions,

Glen.

 


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
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 get Lua class name from C++ class?

Glen F
<base href="x-msg://181/">Thanks, Christian, but I didn't manage to get anything to work in the way you describe.  I can't seem to simply substitute 'object' with 'argument'…  I also tried using myObject.push(L), then using the "from_stack" referenced argument to call get_class_info(), then doing a lua_pop(), but that just crashes on me (a bit later on)…and before that, it hasn't returned the correct class_info structure.  I think I'd need to see a simple example, sorry.

On the other hand, the "hacky" code from Eduard (see his earlier reply) does seem to work fine, as does another approach I came up with, where I just use luabind::call_function() to call the bound "class_info" function in Lua, then get the class_info structure by doing object_cast on the returned object.  Of course, this may be (?) a bit less efficient than doing the direct stack-fiddling you guys suggest, but it works.

Thanks,
Glen.

On Aug 28, 2013, at 4:07 PM, "Christian" <[hidden email]> wrote:

Just replace luabind::object with luabind::argument in your code. They work both the same, only that the first stores the referenced object in the registry, while the latter just leaves it on the stack.
 
If you ever have to convert one into another, just use the ones push() method and the others from_stack constructor (but avoid relative indices with luabind::argument).
 
From: Glen Fraser [mailto:holaglen@gmail.com] 
Sent: Wednesday, August 28, 2013 1:09 PM
To: [hidden email]
Subject: [luabind] How to get Lua class name from C++ class?
 
Hello there, I'm using luabind and trying to get the Lua subclass name from an instance of the base class in C++.
 
I want to use get_class_info(), but it expects an "argument" rather than an "object".  Perhaps there is a better way to do this, besides trying to figure out how to convert a luabind::object into a luabind::adl::argument…  Here is some example code that should illustrate the problem:
 
class BaseClass {
public:
    virtual ~BaseClass() {}
    std::string getClassName(lua_State* L);
    // ... more stuff...
};
 
class BaseClass_wrapper : public BaseClass, public luabind::wrap_base {
    // ... some stuff...
};
 
std::string BaseClass::getClassName(lua_State* L)
{
    BaseClass_wrapper* p = dynamic_cast<BaseClass_wrapper*>(this);
    std::string className("UNKNOWN");
    if (p != 0) {
        luabind::object myObject(L, p);
        luabind::class_info info = luabind::get_class_info(myObject); // no good!
        // no viable conversion from 'luabind::object' to 'const luabind::adl::argument'
        className = info.name;
    }
    return className;
}
 
 
Thanks for any suggestions,
Glen.
 
------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
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 get Lua class name from C++ class?

Christian N.
<base href="x-msg://181/">

You are right, argument has no constructor that takes a C++ object, sorry. But using push() and from_stack works, as in the following example (tested):

 

#include <luabind/luabind.hpp>

#include <luabind/class_info.hpp>

 

#include <iostream>

 

class Foo {};

 

int main()

{

   lua_State* L = luaL_newstate();

    luabind::open(L);

    luabind::module(L) [

        luabind::class_<Foo>("LuaFoo")

    ];

   

    Foo foo;

   

    luabind::object(L, &foo).push(L);

    // Note: -1 instead of lua_gettop(L) would not work in the following line

    // because of a bug in luabind::argument's ctor.

    luabind::argument myArg(luabind::from_stack(L, lua_gettop(L)));

    luabind::class_info info = luabind::get_class_info(myArg);

    std::cout << info.name << '\n'; // Prints "LuaFoo".

}

 

 

From: Glen Fraser [mailto:[hidden email]]
Sent: Wednesday, August 28, 2013 5:38 PM
To: [hidden email]
Subject: Re: [luabind] How to get Lua class name from C++ class?

 

Thanks, Christian, but I didn't manage to get anything to work in the way you describe.  I can't seem to simply substitute 'object' with 'argument'…  I also tried using myObject.push(L), then using the "from_stack" referenced argument to call get_class_info(), then doing a lua_pop(), but that just crashes on me (a bit later on)…and before that, it hasn't returned the correct class_info structure.  I think I'd need to see a simple example, sorry.

 

On the other hand, the "hacky" code from Eduard (see his earlier reply) does seem to work fine, as does another approach I came up with, where I just use luabind::call_function() to call the bound "class_info" function in Lua, then get the class_info structure by doing object_cast on the returned object.  Of course, this may be (?) a bit less efficient than doing the direct stack-fiddling you guys suggest, but it works.

 

Thanks,

Glen.

 

On Aug 28, 2013, at 4:07 PM, "Christian" <[hidden email]> wrote:



Just replace luabind::object with luabind::argument in your code. They work both the same, only that the first stores the referenced object in the registry, while the latter just leaves it on the stack.

 

If you ever have to convert one into another, just use the ones push() method and the others from_stack constructor (but avoid relative indices with luabind::argument).

 

From: Glen Fraser [mailto:holaglen@gmail.com] 
Sent: Wednesday, August 28, 2013 1:09 PM
To: [hidden email]
Subject: [luabind] How to get Lua class name from C++ class?

 

Hello there, I'm using luabind and trying to get the Lua subclass name from an instance of the base class in C++.

 

I want to use get_class_info(), but it expects an "argument" rather than an "object".  Perhaps there is a better way to do this, besides trying to figure out how to convert a luabind::object into a luabind::adl::argument…  Here is some example code that should illustrate the problem:

 

class BaseClass {

public:

    virtual ~BaseClass() {}

    std::string getClassName(lua_State* L);

    // ... more stuff...

};

 

class BaseClass_wrapper : public BaseClass, public luabind::wrap_base {

    // ... some stuff...

};

 

std::string BaseClass::getClassName(lua_State* L)

{

    BaseClass_wrapper* p = dynamic_cast<BaseClass_wrapper*>(this);

    std::string className("UNKNOWN");

    if (p != 0) {

        luabind::object myObject(L, p);

        luabind::class_info info = luabind::get_class_info(myObject); // no good!

        // no viable conversion from 'luabind::object' to 'const luabind::adl::argument'

        className = info.name;

    }

    return className;

}

 

 

Thanks for any suggestions,

Glen.

 

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

 


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
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 get Lua class name from C++ class?

Glen F
<base href="x-msg://181/">Okay, thanks, that works.  I also need to do a pop afterwards, or the stack keeps growing, right? (Seems to be the case, judging by printing out lua_gettop(L) before and after the method.)


    luabind::object(L, &foo).push(L);
    // Note: -1 instead of lua_gettop(L) would not work in the following line
    // because of a bug in luabind::argument's ctor.
    luabind::argument myArg(luabind::from_stack(L, lua_gettop(L)));
    luabind::class_info info = luabind::get_class_info(myArg);

    ::lua_pop(L, 1);

Great, thanks -- I guess my problems were because of that bug you mention, because I was using -1 in luabind::argument's ctor.  Didn't know there was any problem with that.

Glen.

On Aug 28, 2013, at 6:23 PM, Christian <[hidden email]> wrote:

You are right, argument has no constructor that takes a C++ object, sorry. But using push() and from_stackworks, as in the following example (tested):
 
#include <luabind/luabind.hpp>
#include <luabind/class_info.hpp>
 
#include <iostream>
 
class Foo {};
 
int main()
{
   lua_State* L = luaL_newstate();
    luabind::open(L);
    luabind::module(L) [
        luabind::class_<Foo>("LuaFoo")
    ];
   
    Foo foo;
   
    luabind::object(L, &foo).push(L);
    // Note: -1 instead of lua_gettop(L) would not work in the following line
    // because of a bug in luabind::argument's ctor.
    luabind::argument myArg(luabind::from_stack(L, lua_gettop(L)));
    luabind::class_info info = luabind::get_class_info(myArg);
    std::cout << info.name << '\n'; // Prints "LuaFoo".
}
 
 
From: Glen Fraser [mailto:holaglen@gmail.com] 
Sent: Wednesday, August 28, 2013 5:38 PM
To: [hidden email]
Subject: Re: [luabind] How to get Lua class name from C++ class?
 
Thanks, Christian, but I didn't manage to get anything to work in the way you describe.  I can't seem to simply substitute 'object' with 'argument'…  I also tried using myObject.push(L), then using the "from_stack" referenced argument to call get_class_info(), then doing a lua_pop(), but that just crashes on me (a bit later on)…and before that, it hasn't returned the correct class_info structure.  I think I'd need to see a simple example, sorry.
 
On the other hand, the "hacky" code from Eduard (see his earlier reply) does seem to work fine, as does another approach I came up with, where I just use luabind::call_function() to call the bound "class_info" function in Lua, then get the class_info structure by doing object_cast on the returned object.  Of course, this may be (?) a bit less efficient than doing the direct stack-fiddling you guys suggest, but it works.
 
Thanks,
Glen.
 

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user