enum_to_string.hpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef BOOST_DESCRIBE_ENUM_TO_STRING_HPP_INCLUDED
  2. #define BOOST_DESCRIBE_ENUM_TO_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. #if defined(_MSC_VER) && _MSC_VER == 1900
  11. # pragma warning(push)
  12. # pragma warning(disable: 4100) // unreferenced formal parameter
  13. #endif
  14. namespace boost
  15. {
  16. namespace describe
  17. {
  18. template<class E, class De = describe_enumerators<E>>
  19. char const * enum_to_string( E e, char const* def ) noexcept
  20. {
  21. char const * r = def;
  22. mp11::mp_for_each<De>([&](auto D){
  23. if( e == D.value ) r = D.name;
  24. });
  25. return r;
  26. }
  27. } // namespace describe
  28. } // namespace boost
  29. #if defined(_MSC_VER) && _MSC_VER == 1900
  30. # pragma warning(pop)
  31. #endif
  32. #endif // defined(BOOST_DESCRIBE_CXX14)
  33. #endif // #ifndef BOOST_DESCRIBE_ENUM_TO_STRING_HPP_INCLUDED