type_id.qbk 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. [section boost/python/type_id.hpp]
  2. [section Introduction]
  3. <boost/python/type_id.hpp> provides types and functions for runtime type identification like those of of `<typeinfo>`. It exists mostly to work around certain compiler bugs and platform-dependent interactions with shared libraries.
  4. [endsect]
  5. [section Class template `type_info`]
  6. `type_info` instances identify a type. As `std::type_info` is specified to (but unlike its implementation in some compilers), `boost::python::type_info` never represents top-level references or cv-qualification (see section 5.2.8 in the C++ standard). Unlike `std::type_info`, `boost::python::type_info` instances are copyable, and comparisons always work reliably across shared library boundaries.
  7. ``
  8. namespace boost { namespace python
  9. {
  10. class type_info : totally_ordered<type_info>
  11. {
  12. public:
  13. // constructor
  14. type_info(std::type_info const& = typeid(void));
  15. // comparisons
  16. bool operator<(type_info const& rhs) const;
  17. bool operator==(type_info const& rhs) const;
  18. // observers
  19. char const* name() const;
  20. };
  21. }}
  22. ``
  23. [section Class template `type_info` constructor]
  24. ``type_info(std::type_info const& = typeid(void));``
  25. [variablelist
  26. [[Effects][constructs a `type_info` object which identifies the same type as its argument.]]
  27. [[Rationale][Since it is occasionally necessary to make an array of `type_info` objects a benign default argument is supplied. Note: this constructor does not correct for non-conformance of compiler `typeid()` implementations. See `type_id`, below.]]
  28. ]
  29. [endsect]
  30. [section Class template `type_info` comparison]
  31. ``bool operator<(type_info const &rhs) const;``
  32. [variablelist
  33. [[Effects][yields a total order over `type_info` objects.]]
  34. ]
  35. ``bool operator==(type_info const &rhs) const;``
  36. [variablelist
  37. [[Returns][`true` iff the two values describe the same type.]]
  38. [[Note][The use of `totally_ordered<type_info>` as a private base class supplies operators `<=`, `>=`, `>`, and `!=`]]
  39. ]
  40. [endsect]
  41. [section Class template `type_info` observers]
  42. ``
  43. char const* name() const;
  44. ``
  45. [variablelist
  46. [[Returns][The result of calling `name()` on the argument used to construct the object.]]
  47. ]
  48. [endsect]
  49. [endsect]
  50. [section Functions]
  51. ``
  52. std::ostream& operator<<(std::ostream&s, type_info const&x);
  53. ``
  54. [variablelist
  55. [[Effects][Writes a description of the type described by to `x` into s.]]
  56. [[Rationale][Not every C++ implementation provides a truly human-readable `type_info::name()` string, but for some we may be able to decode the string and produce a reasonable representation.]]
  57. [[Note][On some non-conforming C++ implementations, the code is not actually as simple as described above; the semantics are adjusted to work as-if the C++ implementation were conforming.]]
  58. ]
  59. ``
  60. template <class T> type_info type_id()
  61. ``
  62. [variablelist
  63. [[Returns][`type_info(typeid(T))`]]
  64. [[Note][On some non-conforming C++ implementations, the code is not actually as simple as described above; the semantics are adjusted to work as-if the C++ implementation were conforming.]]
  65. ]
  66. [endsect]
  67. [section Example]
  68. The following example, though silly, illustrates how the type_id facility might be used
  69. ``
  70. #include <boost/python/type_id.hpp>
  71. // Returns true iff the user passes an int argument
  72. template <class T>
  73. bool is_int(T x)
  74. {
  75. using boost::python::type_id;
  76. return type_id<T>() == type_id<int>();
  77. }
  78. ``
  79. [endsect]
  80. [endsect]