std::string vs other strings

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

std::string vs other strings

Cory Riddell
I can bind a function like:

const char* say_hello()
{
    return "hello";
}

or

std::string say_hello()
{
    return std::string("hello");
}

and use it with luabind easily. If I want to return another string
format like MFC's CString or Qt's QString, what do I need to do? I
grepped the source looking for something like:
  class_<std::string>("???")
    .def( ... )

I figured I would do the same thing for other string types, but I didn't
find anything. Why does std::string just seem to work?

Cory

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: std::string vs other strings

Willi Schinmeyer
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: std::string vs other strings

Roman Kubiak
In reply to this post by Cory Riddell
I found this some time ago:

But i never got it to work with JUCE's String class, if anyone could share a piece of converter code that works with a custom String class that would be nice.

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






On Fri, Nov 9, 2012 at 10:14 PM, Cory Riddell <[hidden email]> wrote:
I can bind a function like:

const char* say_hello()
{
    return "hello";
}

or

std::string say_hello()
{
    return std::string("hello");
}

and use it with luabind easily. If I want to return another string
format like MFC's CString or Qt's QString, what do I need to do? I
grepped the source looking for something like:
  class_<std::string>("???")
    .def( ... )

I figured I would do the same thing for other string types, but I didn't
find anything. Why does std::string just seem to work?

Cory

------------------------------------------------------------------------------
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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: std::string vs other strings

Cory Riddell
In reply to this post by Willi Schinmeyer
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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: std::string vs other strings

Roman Kubiak
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 <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: std::string vs other strings

Cory Riddell
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 <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: std::string vs other strings

Roman Kubiak
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 <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
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
[hidden email]
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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: std::string vs other strings

Roman Kubiak
I thought that maybe policies are for that. What i mean is to pass a policy to the console() .def() call and say, don't convert the first parameter for that function, just pass the reference to it. But i don't know how to do that.

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






On Tue, Nov 13, 2012 at 6:27 PM, Roman Kubiak <[hidden email]> wrote:
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:
<a href="tel:%2825757538" value="+4825757538" target="_blank">(25757538) default_converter<String>::compute_score got userdata
<a href="tel:%2825757539.7" value="+48257575397" target="_blank">(25757539.7) default_converter<String>::from from
helo

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

i get this:
<a href="tel:%2825807812.1" value="+48258078121" target="_blank">(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 <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
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
[hidden email]
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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: std::string vs other strings

Cory Riddell
In reply to this post by Roman Kubiak
I'm not sure if it's needed, but I also copied the converter for a const string by value parameter. For your code, it would look something like:

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

Cory


On 11/13/2012 11:27 AM, Roman Kubiak wrote:
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 <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
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
[hidden email]
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
[hidden email]
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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user