Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering String Types #140

Closed
jnbrq opened this issue Jul 14, 2016 · 4 comments
Closed

Registering String Types #140

jnbrq opened this issue Jul 14, 2016 · 4 comments
Assignees
Milestone

Comments

@jnbrq
Copy link

jnbrq commented Jul 14, 2016

This is not a bug report, just a question. Your library handles std::string very well. But I have another type of strings: wxString from wxWidgets. Can Sol2 and new string types work well together? How could I register them to a sol::state?

Thank you.

@ThePhD
Copy link
Owner

ThePhD commented Jul 14, 2016

You'd need to customize several points to make this work for you.

It'd look something like the following:

#include <sol.hpp> // or whatever you currently are using to include sol

namespace sol { namespace stack {
     template <>
     struct pusher< ::wxString > { // or whatever the full qualified name is
          static int push ( lua_State* L, const ::wxString& str ) { 
               return stack::push(L, str.c_str()); // or whatever would need to be done
               // documentation for wxString suggests that data can be stored UTF8/16/32, depending on compiler flags
               // You will need to find out how you want to handle that (we don't handle wchar_t or char16_t or char32_t strings since that means we'd need to also build into Sol unicode conversion routines
           }
     };

     template <>
     struct getter< ::wxString > {
          static ::wxString get ( lua_State* L, int index ) { 
               const char* luastr = stack::get<const char*>(L, index);
               // Lua generally works with ASCII or UTF8 strings, so you'd need to convert internally to what ::wxString offers
          }
     };
} // stack

template <>
struct lua_type_of< ::wxString > : std::integral_constant<type, type::string> {};
} //sol

That does look like a lot of work, though... I should consider making things more extensible for other basic types (like the many string types).

Either way, that's how you'd get wxString to get pushed into Lua like a string and read from Lua like a string. You're going to need to full in the blanks, as I don't know what you're compiling for wxWidgets and if you're going to need to do conversions. My hope is that you're doing UTF8, which means you'll be able to just c_str() and pass it off to the internal implementation.

@jnbrq
Copy link
Author

jnbrq commented Jul 14, 2016

Woww! That looks great! Thanks for fast reply! I will try it right now. 👍 :D

@jnbrq jnbrq closed this as completed Jul 14, 2016
@jnbrq jnbrq reopened this Aug 5, 2016
@jnbrq
Copy link
Author

jnbrq commented Aug 5, 2016

After the latest release, applying this method causes a lot of compiler problems. What should I change?

@ThePhD
Copy link
Owner

ThePhD commented Aug 5, 2016

Oops, right. I forgot to mention this.

I formalized the getter/pusher/checker architecture and documented it, so your changes would be minimal (you just need to handle the record& tracking parameter in all of them now as well): http://sol2.readthedocs.io/en/latest/tutorial/customization.html

It would probably look like this:

namespace sol { namespace stack {
     template <>
     struct pusher< ::wxString > {
          static int push ( lua_State* L, const ::wxString& str ) { 
               // same as before
           }
     };

     template <>
     struct getter< ::wxString > {
          static ::wxString get ( lua_State* L, int index, record& tracking ) {
               tracking.use(1); // THIS IS THE ONLY BIT THAT CHANGES
               const char* luastr = stack::get<const char*>(L, index);
               // same as before
          }
     };
} // stack

template <>
struct lua_type_of< ::wxString > : std::integral_constant<type, type::string> {};

@ThePhD ThePhD closed this as completed Aug 5, 2016
@ThePhD ThePhD self-assigned this Aug 5, 2016
@ThePhD ThePhD added this to the Already.Done milestone Aug 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants