enum_from_string.hpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef BOOST_DESCRIBE_ENUM_FROM_STRING_HPP_INCLUDED
  2. #define BOOST_DESCRIBE_ENUM_FROM_STRING_HPP_INCLUDED
  3. // Copyright 2020, 2021 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #include <boost/describe/detail/config.hpp>
  7. #if defined(BOOST_DESCRIBE_CXX14)
  8. #include <boost/describe/enumerators.hpp>
  9. #include <boost/mp11/algorithm.hpp>
  10. #include <cstring>
  11. #if defined(_MSC_VER) && _MSC_VER == 1900
  12. # pragma warning(push)
  13. # pragma warning(disable: 4100) // unreferenced formal parameter
  14. #endif
  15. namespace boost
  16. {
  17. namespace describe
  18. {
  19. template<class E, class De = describe_enumerators<E>>
  20. bool enum_from_string( char const* name, E& e ) noexcept
  21. {
  22. bool found = false;
  23. mp11::mp_for_each<De>([&](auto D){
  24. if( !found && std::strcmp( D.name, name ) == 0 )
  25. {
  26. found = true;
  27. e = D.value;
  28. }
  29. });
  30. return found;
  31. }
  32. } // namespace describe
  33. } // namespace boost
  34. #if defined(_MSC_VER) && _MSC_VER == 1900
  35. # pragma warning(pop)
  36. #endif
  37. #endif // defined(BOOST_DESCRIBE_CXX14)
  38. #endif // #ifndef BOOST_DESCRIBE_ENUM_FROM_STRING_HPP_INCLUDED