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

support for string_view #55

Open
akonskarm opened this issue Feb 6, 2018 · 6 comments
Open

support for string_view #55

akonskarm opened this issue Feb 6, 2018 · 6 comments
Milestone

Comments

@akonskarm
Copy link

akonskarm commented Feb 6, 2018

Hi

Have you considered adding support for string_view for the _from_string methods?
It doesn't convert to char * so _names_match, _from_string and _from_string_loop would need to be overloaded.

BETTER_ENUM(Channel, char, Red = 1, Green, Blue)
int main() {
  std::experimental::string_view s("Blue");
  auto c = Channel::_from_string_nothrow(s);
  std::cout << *c << std::endl;
  return(0);
}
@aantron
Copy link
Owner

aantron commented Feb 6, 2018

Hi :)

I've looked at it briefly, and it seems like a good thing to add. I haven't had time to properly do it, though.

@aantron
Copy link
Owner

aantron commented Feb 6, 2018

One minor annoyance is it will probably need conditional compilation, based on compiler version and an override macro to force the overloads to be enabled for when the version detection does the wrong thing.

@akonskarm
Copy link
Author

akonskarm commented Feb 6, 2018

Yes, also because some compiler versions support only <experimental/string_view> and newer ones have <string_view>

The code below worked for me

BETTER_ENUMS_CONSTEXPR_ inline bool _names_match(const char *stringizedName,
                                                 std::experimental::string_view &referenceName,
                                                 std::size_t index = 0)
{
    return
        _ends_name(stringizedName[index]) ? index == referenceName.size() :
        index == referenceName.size() ? false :
        stringizedName[index] != referenceName[index] ? false :
        _names_match(stringizedName, referenceName, index + 1);
}

    BETTER_ENUMS_CONSTEXPR_ static _optional                                    \
    _from_string_nothrow(std::experimental::string_view s);                     \
\
    BETTER_ENUMS_CONSTEXPR_ static _optional_index                              \
    _from_string_loop(std::experimental::string_view &s, std::size_t index = 0);\

BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional_index                            \
Enum::_from_string_loop(std::experimental::string_view &s, std::size_t index)   \
{                                                                               \
    return                                                                      \
        index == _size() ? _optional_index() :                                  \
        ::better_enums::_names_match(                                           \
            BETTER_ENUMS_NS(Enum)::_raw_names()[index], s) ?                    \
            _optional_index(index) :                                            \
            _from_string_loop(s, index + 1);                                    \
}                                                                               \
\
BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional                                  \
Enum::_from_string_nothrow(std::experimental::string_view s)                    \
{                                                                               \
    return                                                                      \
        ::better_enums::_map_index<Enum>(                                       \
            BETTER_ENUMS_NS(Enum)::_value_array, _from_string_loop(s));         \
}

@aantron
Copy link
Owner

aantron commented Feb 6, 2018

Thanks for that.

I won't have the time to integrate it in the immediate future (because of all that conditional compilation testing that would be needed).

If you'd like to give it a try, you're welcome to open a PR and test in the Better Enums CI setup. I think it might need to be updated to use some more recent compilers in its build rows.

I'm not sure what to do about std::experimental. Is it right that that namespace is not forwards-compatible (I never knew whether it is or isn't, actually), or something in there is guaranteed to stay? If not, we would probably want to avoid it entirely, and support only compilers that have std::string_view.

@aantron aantron added this to the 0.11.3 milestone Aug 17, 2019
@sdebionne
Copy link

I am also interested in this feature. I think support for std::string_view is good enough. I might have a shot at this, is there anyone currently working on this feature request?

@aantron
Copy link
Owner

aantron commented Nov 28, 2019

Thanks. Not to my knowledge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants