user_defined_typeinfo.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2013-2019 Antony Polukhin
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See the accompanying file LICENSE_1_0.txt
  4. // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  5. //[type_index_my_type_index_worldwide_macro
  6. /*`
  7. There is an easy way to force `boost::typeindex::type_id` to use your own type_index class.
  8. All we need to do is just define `BOOST_TYPE_INDEX_USER_TYPEINDEX` to the full path to header file
  9. of your type index class:
  10. */
  11. // BOOST_TYPE_INDEX_USER_TYPEINDEX must be defined *BEFORE* first inclusion of <boost/type_index.hpp>
  12. #define BOOST_TYPE_INDEX_USER_TYPEINDEX <boost/../libs/type_index/examples/user_defined_typeinfo.hpp>
  13. #include <boost/type_index.hpp>
  14. //] [/type_index_my_type_index_worldwide_macro]
  15. #include <boost/core/lightweight_test.hpp>
  16. #ifdef assert
  17. # undef assert
  18. #endif
  19. #define assert(X) BOOST_TEST(X)
  20. using namespace my_namespace;
  21. int main() {
  22. //[type_index_my_type_index_usage
  23. /*`
  24. Finally we can use the my_type_index class for getting type indexes:
  25. */
  26. my_type_index
  27. cl1 = my_type_index::type_id<my_class>(),
  28. st1 = my_type_index::type_id<my_struct>(),
  29. st2 = my_type_index::type_id<my_struct>(),
  30. vec = my_type_index::type_id<my_classes>()
  31. ;
  32. assert(cl1 != st1);
  33. assert(st2 == st1);
  34. assert(vec.pretty_name() == "my_classes");
  35. assert(cl1.pretty_name() == "my_class");
  36. //] [/type_index_my_type_index_usage]
  37. //[type_index_my_type_index_type_id_runtime_test
  38. /*`
  39. Now the following example will compile and work.
  40. */
  41. my_struct str;
  42. my_class& reference = str;
  43. assert(my_type_index::type_id<my_struct>() == my_type_index::type_id_runtime(reference));
  44. //][/type_index_my_type_index_type_id_runtime_test]
  45. //[type_index_my_type_index_worldwide_usage
  46. /*`
  47. That's it! Now all TypeIndex global methods and typedefs will be using your class:
  48. */
  49. boost::typeindex::type_index worldwide = boost::typeindex::type_id<my_classes>();
  50. assert(worldwide.pretty_name() == "my_classes");
  51. assert(worldwide == my_type_index::type_id<my_classes>());
  52. //][/type_index_my_type_index_worldwide_usage]
  53. //<-
  54. return boost::report_errors();
  55. //->
  56. }