scoped_enum_emulation.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // scoped_enum_emulation.hpp ---------------------------------------------------------//
  2. // Copyright Beman Dawes, 2009
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // Generates C++0x scoped enums if the feature is present, otherwise emulates C++0x
  6. // scoped enums with C++03 namespaces and enums. The Boost.Config BOOST_NO_SCOPED_ENUMS
  7. // macro is used to detect feature support.
  8. //
  9. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf for a
  10. // description of the scoped enum feature. Note that the committee changed the name
  11. // from strongly typed enum to scoped enum.
  12. //
  13. // Caution: only the syntax is emulated; the semantics are not emulated and
  14. // the syntax emulation doesn't include being able to specify the underlying
  15. // representation type.
  16. //
  17. // The emulation is via struct rather than namespace to allow use within classes.
  18. // Thanks to Andrey Semashev for pointing that out.
  19. //
  20. // Helpful comments and suggestions were also made by Kjell Elster, Phil Endecott,
  21. // Joel Falcou, Mathias Gaunard, Felipe Magno de Almeida, Matt Calabrese, Vincente
  22. // Botet, and Daniel James.
  23. //
  24. // Sample usage:
  25. //
  26. // BOOST_SCOPED_ENUM_START(algae) { green, red, cyan }; BOOST_SCOPED_ENUM_END
  27. // ...
  28. // BOOST_SCOPED_ENUM(algae) sample( algae::red );
  29. // void foo( BOOST_SCOPED_ENUM(algae) color );
  30. // ...
  31. // sample = algae::green;
  32. // foo( algae::cyan );
  33. #ifndef BOOST_SCOPED_ENUM_EMULATION_HPP
  34. #define BOOST_SCOPED_ENUM_EMULATION_HPP
  35. #include <boost/config.hpp>
  36. #ifdef BOOST_NO_SCOPED_ENUMS
  37. # define BOOST_SCOPED_ENUM_START(name) struct name { enum enum_t
  38. # define BOOST_SCOPED_ENUM_END };
  39. # define BOOST_SCOPED_ENUM(name) name::enum_t
  40. #else
  41. # define BOOST_SCOPED_ENUM_START(name) enum class name
  42. # define BOOST_SCOPED_ENUM_END
  43. # define BOOST_SCOPED_ENUM(name) name
  44. #endif
  45. #endif // BOOST_SCOPED_ENUM_EMULATION_HPP