rtti.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : simple facilities for accessing type information at runtime
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_RTTI_HPP
  14. #define BOOST_TEST_UTILS_RTTI_HPP
  15. // C Runtime
  16. #include <cstddef>
  17. #include <boost/test/detail/config.hpp>
  18. namespace boost {
  19. namespace rtti {
  20. // ************************************************************************** //
  21. // ************** rtti::type_id ************** //
  22. // ************************************************************************** //
  23. typedef std::ptrdiff_t id_t;
  24. namespace rtti_detail {
  25. template<typename T>
  26. struct BOOST_TEST_DECL rttid_holder {
  27. static id_t id() { return reinterpret_cast<id_t>( &inst() ); }
  28. private:
  29. struct rttid {};
  30. static rttid const& inst() { static rttid s_inst; return s_inst; }
  31. };
  32. } // namespace rtti_detail
  33. //____________________________________________________________________________//
  34. template<typename T>
  35. BOOST_TEST_DECL inline id_t
  36. type_id()
  37. {
  38. return rtti_detail::rttid_holder<T>::id();
  39. }
  40. //____________________________________________________________________________//
  41. #define BOOST_RTTI_SWITCH( type_id_ ) if( ::boost::rtti::id_t switch_by_id = type_id_ )
  42. #define BOOST_RTTI_CASE( type ) if( switch_by_id == ::boost::rtti::type_id<type>() )
  43. //____________________________________________________________________________//
  44. } // namespace rtti
  45. } // namespace boost
  46. #endif // BOOST_TEST_UTILS_RTTI_HPP