rtti.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. namespace boost {
  18. namespace rtti {
  19. // ************************************************************************** //
  20. // ************** rtti::type_id ************** //
  21. // ************************************************************************** //
  22. typedef std::ptrdiff_t id_t;
  23. namespace rtti_detail {
  24. template<typename T>
  25. struct rttid_holder {
  26. static id_t id() { return reinterpret_cast<id_t>( &inst() ); }
  27. private:
  28. struct rttid {};
  29. static rttid const& inst() { static rttid s_inst; return s_inst; }
  30. };
  31. } // namespace rtti_detail
  32. //____________________________________________________________________________//
  33. template<typename T>
  34. inline id_t
  35. type_id()
  36. {
  37. return rtti_detail::rttid_holder<T>::id();
  38. }
  39. //____________________________________________________________________________//
  40. #define BOOST_RTTI_SWITCH( type_id_ ) if( ::boost::rtti::id_t switch_by_id = type_id_ )
  41. #define BOOST_RTTI_CASE( type ) if( switch_by_id == ::boost::rtti::type_id<type>() )
  42. //____________________________________________________________________________//
  43. } // namespace rtti
  44. } // namespace boost
  45. #endif // BOOST_TEST_UTILS_RTTI_HPP