Re: Problems with custom converter

Daniel Wallin <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
On Thu, Jun 24, 2010 at 01:53:14PM +0200, Szymon Gatner wrote:
> Hi,
> 
> recently I am having very rough time with code using luabind so I
> finally decided to ask here.
> 
> In my code I have a class like this, color.h:
> 
> struct Color3b
> {
>   const static Color3b White;
>   const static Color3b Black;
> 
>   Color3b() {}
> 
>   Color3b(Byte r_, Byte g_, Byte b_) : r(r_), g(g_), b(b_) {}
> 
>   Byte r, g, b;
> };
> 
> then in some cpp file:
> 
> namespace luabind
> {
>   template <>
>   struct default_converter<Color3b> : native_converter_base<Color3b>
>   {
>     static int compute_score(lua_State* L, int index)
>     {
>       return lua_type(L, index) == LUA_TTABLE ? 0 : -1;
>     }
> 
>     Color3b from(lua_State* L, int index)
>     {
>       Color3b ret;
> 
>      //snip
> 
>       return ret;
>     }
> 
>     void to(lua_State* L, Color3b const& color)
>     {
>       object obj = newtable(L);
> 
>       obj[1] = color.r;
>       obj[2] = color.g;
>       obj[3] = color.b;
> 
>       obj.push(L);
>     }
>   };
> 
>   template <>
>   struct default_converter<Color3b const&>
>     : default_converter<Color3b>
>   {};
> }
> 
> then later in same file:
> 
> Color3b color3b = luabind::object_cast<Color3b>(params["normal"]);
> 
> Problem is that this cast always results with exception "unable to
> make cast". Program never enters compute_score() for provided
> converter, in fact break point is marked invalid because this code
> seem to be unused at all. I know that params["normal'] is a valid lua
> object having needed 3 values inside
> (in lua it is like: color = {1123, 123, 123}).
> 
> What is the possible reason for object_cast not to see this converter?

If the specialization is indeed visible when object_cast<> is
instantiated, I don't know. Can you reduce to a complete failing
example?

> Why is compile-time expression object_cast<Color3b> not a compile
> error if no proper converter/conversion trait is found?

Because the user might have exported the Color3b class using class_<>,
and you can't tell until runtime.

-- 
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.