Luabind and enums

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

Luabind and enums

Alexander Gladysh
Hi all!

Luabind b7, Lua 5.0.2.

How can I bind my enum to global namespace? Why Luabind does not have
luabind::enum_, but only luabind::class_::enum_?

Furthermore, the following code does not compile. Am I missing something?

[cpp]
#include <iostream>
#include <string>

extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

#pragma warning(push, 4)
#pragma warning(disable : 4100 4675 4511 4512 4702 4127)

#include "luabind/luabind.hpp"
#include "luabind/object.hpp"

#pragma warning(pop)

enum Bar
{
  barZero,
  barOne,
  barTwo
};

class Foo
{
public:
  void Print(const std::string & str)
  {
    std::cout << str << std::endl;
  }
};

int main()
{
  lua_State * pState = lua_open();
  if (pState)
  {
    luaopen_base(pState);
    luaopen_string(pState);
    luaopen_table(pState);
    luaopen_math(pState);
    luaopen_debug(pState);
    luaopen_io(pState);
    luabind::open(pState);

    luabind::module(pState)
    [
      luabind::class_<Foo>("Foo_t")
        .def("Print", Foo::Print)
        .enum_("Bar")
        [
            value("barZero", barZero)
          , value("barOne", barOne)
          , value("barTwo", barTwo)
        ] // (*) Line 58
    ];

    lua_close(pState);
    pState = NULL;
  }
  else
  {
    std::cout << "Failed to create Lua state." << std::endl;
  }

  return 0;
}
[/cpp]

My VC++ 2003 gives following error messages on line (*):

[q]
test.cpp(58) : warning C4709: comma operator within array index expression
test.cpp(58) : error C2593: 'operator [' is ambiguous
        luabind\luabind\detail\enum_maker.hpp(104): could be
'luabind::class_<T>::self_t
&luabind::detail::enum_maker<From>::operator [](const
luabind::value_vector &)'
        with
        [
            T=Foo,
            From=luabind::class_<Foo>::self_t
        ]
        luabind\luabind\detail\enum_maker.hpp(98): or      
'luabind::class_<T>::self_t
&luabind::detail::enum_maker<From>::operator [](const luabind::value
&)'
        with
        [
            T=Foo,
            From=luabind::class_<Foo>::self_t
        ]
        while trying to match the argument list
'(luabind::detail::enum_maker<From>, 'unknown-type')'
        with
        [
            From=luabind::class_<Foo>::self_t
        ]
test.cpp(55) : error C3861: 'value': identifier not found, even with
argument-dependent lookup
test.cpp(56) : error C3861: 'value': identifier not found, even with
argument-dependent lookup
test.cpp(57) : error C3861: 'value': identifier not found, even with
argument-dependent lookup
[/q]

Thanks,
Alexander.

P.S. Forgot to ask in my previous post. Would
luabind::object_cast<lua_State *> work properly on luabind::object
with type LUA_TTHREAD?


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind and enums

Arvid Norberg

On Feb 27, 2006, at 07:10, Alexander Gladysh wrote:

> Hi all!
>
> Luabind b7, Lua 5.0.2.
>
> How can I bind my enum to global namespace? Why Luabind does not have
> luabind::enum_, but only luabind::class_::enum_?

The reason for this is that the enums are implemented using  
metatables on the class objects. The only way of implementing them in  
the global namespace would be to create a metatable for the globals  
table. We decided that forcing a metatable on the globals table would  
be too intrusive for such a small gain.

> Furthermore, the following code does not compile. Am I missing  
> something?
>
> [cpp]
> #include <iostream>
> #include <string>
>
> extern "C"
> {
> #include <lua.h>
> #include <lualib.h>
> #include <lauxlib.h>
> }
>
> #pragma warning(push, 4)
> #pragma warning(disable : 4100 4675 4511 4512 4702 4127)
>
> #include "luabind/luabind.hpp"
> #include "luabind/object.hpp"
>
> #pragma warning(pop)
>
> enum Bar
> {
>   barZero,
>   barOne,
>   barTwo
> };
>
> class Foo
> {
> public:
>   void Print(const std::string & str)
>   {
>     std::cout << str << std::endl;
>   }
> };
>
> int main()
> {
>   lua_State * pState = lua_open();
>   if (pState)
>   {
>     luaopen_base(pState);
>     luaopen_string(pState);
>     luaopen_table(pState);
>     luaopen_math(pState);
>     luaopen_debug(pState);
>     luaopen_io(pState);
>     luabind::open(pState);
>
>     luabind::module(pState)
>     [
>       luabind::class_<Foo>("Foo_t")
>         .def("Print", Foo::Print)

GCC gives an error here:
test.cpp:51: error: invalid use of non-static member function ‘void  
Foo::Print(const std::string&)’

you should put an & in front of your function to get the pointer to it.

>         .enum_("Bar")
>         [
>             value("barZero", barZero)
>           , value("barOne", barOne)
>           , value("barTwo", barTwo)
>         ] // (*) Line 58

GCC gives this error:
test.cpp:54: error: ‘value’ was not declared in this scope

You need to either put using namespace luabind; in your function or  
qualify the references to "value".

>     ];
>
>     lua_close(pState);
>     pState = NULL;
>   }
>   else
>   {
>     std::cout << "Failed to create Lua state." << std::endl;
>   }
>
>   return 0;
> }
> [/cpp]
>
> My VC++ 2003 gives following error messages on line (*):
> [...]
>
> Thanks,
> Alexander.
>
> P.S. Forgot to ask in my previous post. Would
> luabind::object_cast<lua_State *> work properly on luabind::object
> with type LUA_TTHREAD?

No. I guess that converter should be added. At least from lua to c++.


--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Luabind and enums

Alexander Gladysh
> > How can I bind my enum to global namespace? Why Luabind does not have
> > luabind::enum_, but only luabind::class_::enum_?
>
> The reason for this is that the enums are implemented using
> metatables on the class objects. The only way of implementing them in
> the global namespace would be to create a metatable for the globals
> table. We decided that forcing a metatable on the globals table would
> be too intrusive for such a small gain.

That is so. But since we're specifying name of enum anyway, why not allow

         luabind::enum_("Bar")
         [
             value("barZero", barZero)
         ];

which would create a table (like luabind::class_ does), and setup all
necessary metatable stuff to have one constant member Bar.barZero? It
would make creation of enums more convenient.

> >     luabind::module(pState)
> >     [
> >       luabind::class_<Foo>("Foo_t")
> >         .def("Print", Foo::Print)
>
> GCC gives an error here:
> test.cpp:51: error: invalid use of non-static member function 'void
> Foo::Print(const std::string&)'
>
> you should put an & in front of your function to get the pointer to it.

Yes, sorry, I've corrupted by my MSVC 2003. ;)

> >         .enum_("Bar")
> >         [
> >             value("barZero", barZero)
> >           , value("barOne", barOne)
> >           , value("barTwo", barTwo)
> >         ] // (*) Line 58
>
> GCC gives this error:
> test.cpp:54: error: 'value' was not declared in this scope
>
> You need to either put using namespace luabind; in your function or
> qualify the references to "value".

Aha, I see now, thanks.

> > P.S. Forgot to ask in my previous post. Would
> > luabind::object_cast<lua_State *> work properly on luabind::object
> > with type LUA_TTHREAD?
>
> No. I guess that converter should be added. At least from lua to c++.

Thank you,
Alexander.


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user