Re: std::string vs other strings

Roman Kubiak <[email protected]> Tue, 13 Nov 2012 18:27:09 +0100
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <CACugK=YjJH=SWwD-kqVjVywq6pWH+5gD2NK-s=o1w7+AcmXCSQ@mail.gmail.com>
My converter is pretty simple:
namespace luabind
{
template <> struct default_converter<String> : native_converter_base<String>
{
static int compute_score(lua_State* L, int index)
{
_DBG("default_converter<String>::compute_score got userdata");
return (lua_type(L, index) == LUA_TSTRING) ? 0 : -1;
}
 String from(lua_State* L, int index)
{
_DBG("default_converter<String>::from from");
return String(lua_tostring(L, index));
}
 void to(lua_State* L, String const &value)
{
_DBG("default_converter<String>::from to");
lua_pushlstring(L, value.toUTF8(), value.length());
}
};

template <> struct default_converter<String const&> :
default_converter<String>
{
};
}

When i do in lua:
console("helo")

i get this output:
(25757538) default_converter<String>::compute_score got userdata
(25757539.7) default_converter<String>::from from
helo

When i do:
str = String("helo")
console(str)

i get this:
(25807812.1) default_converter<String>::compute_score got userdata
ERROR: No matching overload found, candidates:
void console(String const&)


====================================================================
         _ _                              _           _ _        _
__ _____| | |_ __ _ __ _ ___   __ ___ _ _| |_ _ _ ___| | |___ __| |
\ V / _ \ |  _/ _` / _` / -_) / _/ _ \ ' \  _| '_/ _ \ | / -_) _` |
 \_/\___/_|\__\__,_\__, \___| \__\___/_||_\__|_| \___/_|_\___\__,_|
                   |___/
====================================================================






On Tue, Nov 13, 2012 at 5:42 AM, Cory Riddell <[email protected]> wrote:

>  I don't have your String class, so I tried the same thing you describing
> using MFC CStrings.
>
> In my C++ code, I defined the following:
>
> void console(const CString &textToPrint)
> {
>     // write output to a debug window and echo it to a log file
>     printToOutput(std::string(textToPrint));
> }
>
> CString getName()
> {
>     return CString("my name");
> }
>
> I bound them:
>     luabind::module(myLuaState) [
>         luabind::def("console", console),
>         luabind::def("getName", getName)
>     ];
>
> Then called them in a lua script:
>     console(getName())
>
> Everything seemed to work just fine. The string "my name" is written to my
> debug window and echoed to my log file by my printToOutput function.
>
> Maybe post your converter definitions and how you are binding to lua.
>
> Cory
>
>
> On 11/12/2012 12:59 PM, Roman Kubiak wrote:
>
> I got back to the problem and tried again with the conversion, i noticed
> that it matters when the converter is defined in the include files, so now
> i get the converter working, but too much.
> i have a simple method that prints a string to the console defined as
> void console (const String &textToPrint);
> and i have loads of methods/functions that return a String() object for
> example:
> String getName() { return (String("my name")); }
>
>  now when the converter is registered i can't do
> console (getName())
>
>  this causes an error saying there are no valid candidates for the
> console call, i noticed that the converter is
> using the compute_score() to determine if a String() object is a string
> (lua_type(L, index) == LUA_TSTRING) and i'ts not
> it's LUA_TUSERDATA, now if i return 1 in compute_score() the function
> from() will be called but it will have the
> String() object on the stack, and i can't convert it using
> luabind::object() since it's object_cast<>() methos will
> call the converter again, and it will loop forever.
>
>  What's the solution to this ?
>
> ====================================================================
>          _ _                              _           _ _        _
> __ _____| | |_ __ _ __ _ ___   __ ___ _ _| |_ _ _ ___| | |___ __| |
> \ V / _ \ |  _/ _` / _` / -_) / _/ _ \ ' \  _| '_/ _ \ | / -_) _` |
>  \_/\___/_|\__\__,_\__, \___| \__\___/_||_\__|_| \___/_|_\___\__,_|
>                    |___/
> ====================================================================
>
>
>
>
>
>
> On Mon, Nov 12, 2012 at 4:47 PM, Cory Riddell <[email protected]> wrote:
>
>> Thank you for the pointer. It ended up being incredibly simple. Here's
>> my code for CString:
>>
>> namespace luabind
>> {
>>     template <>
>>     struct default_converter<CString>
>>       : native_converter_base<CString>
>>     {
>>         static int compute_score(lua_State* L, int index)
>>         {
>>             return lua_type(L, index) == LUA_TSTRING ? 0 : -1;
>>         }
>>
>>         CString from(lua_State* L, int index)
>>         {
>>             return CString(lua_tostring(L, index), lua_rawlen(L, index));
>>         }
>>
>>         void to(lua_State* L, CString const& value)
>>         {
>>             lua_pushlstring(L, (LPCTSTR)value, value.GetLength());
>>         }
>>     };
>>
>>     template <>
>>     struct default_converter<CString const>
>>       : default_converter<CString>
>>     {};
>>
>>     template <>
>>     struct default_converter<CString const&>
>>       : default_converter<CString>
>>     {};
>> }
>>
>>
>> The only possible problem that I can see here is needing to convert from
>> wchar_t to char (utf-8) when making a unicode build.
>>
>> Cory
>>
>>
>>
>> On 11/9/2012 3:35 PM, Willi Schinmeyer wrote:
>> > The std::string converter seems to be defined in detail/policy.hpp:756
>> > as a specialization of default_converter. I'd start my investigation
>> there.
>> >
>> >
>> ------------------------------------------------------------------------------
>> > Everyone hates slow websites. So do we.
>> > Make your web apps faster with AppDynamics
>> > Download AppDynamics Lite for free today:
>> > http://p.sf.net/sfu/appdyn_d2d_nov
>> > _______________________________________________
>> > luabind-user mailing list
>> > [email protected]
>> > https://lists.sourceforge.net/lists/listinfo/luabind-user
>> >
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Everyone hates slow websites. So do we.
>> Make your web apps faster with AppDynamics
>> Download AppDynamics Lite for free today:
>> http://p.sf.net/sfu/appdyn_d2d_nov
>> _______________________________________________
>> luabind-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/luabind-user
>>
>
>
>
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!http://p.sf.net/sfu/zoho_dev2dev_nov
>
>
>
> _______________________________________________
> luabind-user mailing [email protected]://lists.sourceforge.net/lists/listinfo/luabind-user
>
>
>
>
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>

------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user