Skip to content

Commit

Permalink
Add an overload of enum_from_string that takes a string-like type (su…
Browse files Browse the repository at this point in the history
…ch as string_view). Fixes #41.
  • Loading branch information
pdimov committed Sep 9, 2023
1 parent 6e7d49c commit cfddb57
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
24 changes: 24 additions & 0 deletions include/boost/describe/enum_from_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <boost/describe/enumerators.hpp>
#include <boost/mp11/algorithm.hpp>
#include <cstring>
#include <type_traits>

#if defined(_MSC_VER) && _MSC_VER == 1900
# pragma warning(push)
Expand Down Expand Up @@ -41,6 +42,29 @@ bool enum_from_string( char const* name, E& e ) noexcept
return found;
}

template<class S, class E, class De = describe_enumerators<E>,
class En = std::enable_if_t<
std::is_same<typename S::value_type, char>::value &&
std::is_same<typename S::traits_type::char_type, char>::value
>
>
bool enum_from_string( S const& name, E& e ) noexcept
{
bool found = false;

mp11::mp_for_each<De>([&](auto D){

if( !found && name == D.name )
{
found = true;
e = D.value;
}

});

return found;
}

} // namespace describe
} // namespace boost

Expand Down
2 changes: 2 additions & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ run pedantic_bases_test.cpp
run pedantic_members_test.cpp
: : : <warnings>pedantic ;

run enum_from_string_test2.cpp ;

# examples

obj describe_cxx14 : describe_cxx14.cpp ;
Expand Down
78 changes: 78 additions & 0 deletions test/enum_from_string_test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2021 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/describe/enum_from_string.hpp>
#include <boost/describe/enum.hpp>
#include <boost/core/detail/string_view.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>

#if !defined(BOOST_DESCRIBE_CXX14)

#include <boost/config/pragma_message.hpp>

BOOST_PRAGMA_MESSAGE("Skipping test because C++14 is not available")
int main() {}

#else

enum E1 { v101 = 101, v102 = 102 };
BOOST_DESCRIBE_ENUM(E1, v101, v102)

enum class E2 { v201 = 201, v202 = 202 };
BOOST_DESCRIBE_ENUM(E2, v201, v202)

BOOST_DEFINE_ENUM(E3, v301, v302)
BOOST_DEFINE_ENUM_CLASS(E4, v401, v402)

template<class St> void test()
{
using boost::describe::enum_from_string;

{
E1 w{};
BOOST_TEST( enum_from_string( St( "v101" ), w ) ) && BOOST_TEST_EQ( w, v101 );
BOOST_TEST( enum_from_string( St( "v102" ), w ) ) && BOOST_TEST_EQ( w, v102 );
BOOST_TEST_NOT( enum_from_string( St( "v103" ), w ) );
}

{
E2 w{};
BOOST_TEST( enum_from_string( St( "v201" ), w ) ) && BOOST_TEST_EQ( (int)w, (int)E2::v201 );
BOOST_TEST( enum_from_string( St( "v202" ), w ) ) && BOOST_TEST_EQ( (int)w, (int)E2::v202 );
BOOST_TEST_NOT( enum_from_string( St( "v203" ), w ) );
}

{
E3 w{};
BOOST_TEST( enum_from_string( St( "v301" ), w ) ) && BOOST_TEST_EQ( w, v301 );
BOOST_TEST( enum_from_string( St( "v302" ), w ) ) && BOOST_TEST_EQ( w, v302 );
BOOST_TEST_NOT( enum_from_string( St( "v303" ), w ) );
}

{
E4 w{};
BOOST_TEST( enum_from_string( St( "v401" ), w ) ) && BOOST_TEST_EQ( (int)w, (int)E4::v401 );
BOOST_TEST( enum_from_string( St( "v402" ), w ) ) && BOOST_TEST_EQ( (int)w, (int)E4::v402 );
BOOST_TEST_NOT( enum_from_string( St( "v403" ), w ) );
}
}

#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
# include <string_view>
#endif

int main()
{
test<std::string>();
test<boost::core::string_view>();

#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
test<std::string_view>();
#endif

return boost::report_errors();
}

#endif // !defined(BOOST_DESCRIBE_CXX14)

0 comments on commit cfddb57

Please sign in to comment.