| 1 | #ifndef BOOST_DESCRIBE_ENUM_FROM_STRING_HPP_INCLUDED |
| 2 | #define BOOST_DESCRIBE_ENUM_FROM_STRING_HPP_INCLUDED |
| 3 | |
| 4 | // Copyright 2020, 2021 Peter Dimov |
| 5 | // Distributed under the Boost Software License, Version 1.0. |
| 6 | // https://www.boost.org/LICENSE_1_0.txt |
| 7 | |
| 8 | #include <boost/describe/detail/config.hpp> |
| 9 | |
| 10 | #if defined(BOOST_DESCRIBE_CXX14) |
| 11 | |
| 12 | #include <boost/describe/enumerators.hpp> |
| 13 | #include <boost/mp11/algorithm.hpp> |
| 14 | #include <cstring> |
| 15 | #include <type_traits> |
| 16 | |
| 17 | #if defined(_MSC_VER) && _MSC_VER == 1900 |
| 18 | # pragma warning(push) |
| 19 | # pragma warning(disable: 4100) // unreferenced formal parameter |
| 20 | #endif |
| 21 | |
| 22 | namespace boost |
| 23 | { |
| 24 | namespace describe |
| 25 | { |
| 26 | |
| 27 | template<class E, class De = describe_enumerators<E>> |
| 28 | bool enum_from_string( char const* name, E& e ) noexcept |
| 29 | { |
| 30 | bool found = false; |
| 31 | |
| 32 | mp11::mp_for_each<De>([&](auto D){ |
| 33 | |
| 34 | if( !found && std::strcmp( s1: D.name, s2: name ) == 0 ) |
| 35 | { |
| 36 | found = true; |
| 37 | e = D.value; |
| 38 | } |
| 39 | |
| 40 | }); |
| 41 | |
| 42 | return found; |
| 43 | } |
| 44 | |
| 45 | template<class S, class E, class De = describe_enumerators<E>, |
| 46 | class En = std::enable_if_t< |
| 47 | std::is_same<typename S::value_type, char>::value && |
| 48 | std::is_same<typename S::traits_type::char_type, char>::value |
| 49 | > |
| 50 | > |
| 51 | bool enum_from_string( S const& name, E& e ) noexcept |
| 52 | { |
| 53 | bool found = false; |
| 54 | |
| 55 | mp11::mp_for_each<De>([&](auto D){ |
| 56 | |
| 57 | if( !found && name == D.name ) |
| 58 | { |
| 59 | found = true; |
| 60 | e = D.value; |
| 61 | } |
| 62 | |
| 63 | }); |
| 64 | |
| 65 | return found; |
| 66 | } |
| 67 | |
| 68 | } // namespace describe |
| 69 | } // namespace boost |
| 70 | |
| 71 | #if defined(_MSC_VER) && _MSC_VER == 1900 |
| 72 | # pragma warning(pop) |
| 73 | #endif |
| 74 | |
| 75 | #endif // defined(BOOST_DESCRIBE_CXX14) |
| 76 | |
| 77 | #endif // #ifndef BOOST_DESCRIBE_ENUM_FROM_STRING_HPP_INCLUDED |
| 78 | |