2 // Copyright (c) Antony Polukhin, 2013-2015.
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_TYPE_INDEX_TYPE_INDEX_FACADE_HPP
10 #define BOOST_TYPE_INDEX_TYPE_INDEX_FACADE_HPP
12 #include <boost/config.hpp>
16 #if !defined(BOOST_NO_IOSTREAM)
17 #if !defined(BOOST_NO_IOSFWD)
18 #include <iosfwd> // for std::basic_ostream
24 #ifdef BOOST_HAS_PRAGMA_ONCE
28 // Forward declaration from #include <boost/functional/hash_fwd.hpp>
30 template <class It> std::size_t hash_range(It, It);
33 namespace boost { namespace typeindex {
35 /// \class type_index_facade
37 /// This class takes care about the comparison operators, hash functions and
38 /// ostream operators. Use this class as a public base class for defining new
39 /// type_info-conforming classes.
43 /// class stl_type_index: public type_index_facade<stl_type_index, std::type_info>
46 /// typedef std::type_info type_info_t;
48 /// const type_info_t* data_;
51 /// stl_type_index(const type_info_t& data) noexcept
58 /// \tparam Derived Class derived from type_index_facade.
59 /// \tparam TypeInfo Class that will be used as a base type_info class.
60 /// \note Take a look at the protected methods. They are \b not \b defined in type_index_facade.
61 /// Protected member functions raw_name() \b must be defined in Derived class. All the other
62 /// methods are mandatory.
63 /// \see 'Making a custom type_index' section for more information about
64 /// creating your own type_index using type_index_facade.
65 template <class Derived, class TypeInfo>
66 class type_index_facade {
69 BOOST_CXX14_CONSTEXPR const Derived & derived() const BOOST_NOEXCEPT {
70 return *static_cast<Derived const*>(this);
74 typedef TypeInfo type_info_t;
76 /// \b Override: This function \b may be redefined in Derived class. Overrides \b must not throw.
77 /// \return Name of a type. By default returns Derived::raw_name().
78 inline const char* name() const BOOST_NOEXCEPT {
79 return derived().raw_name();
82 /// \b Override: This function \b may be redefined in Derived class. Overrides may throw.
83 /// \return Human readable type name. By default returns Derived::name().
84 inline std::string pretty_name() const {
85 return derived().name();
88 /// \b Override: This function \b may be redefined in Derived class. Overrides \b must not throw.
89 /// \return True if two types are equal. By default compares types by raw_name().
90 inline bool equal(const Derived& rhs) const BOOST_NOEXCEPT {
91 const char* const left = derived().raw_name();
92 const char* const right = rhs.raw_name();
93 return left == right || !std::strcmp(left, right);
96 /// \b Override: This function \b may be redefined in Derived class. Overrides \b must not throw.
97 /// \return True if rhs is greater than this. By default compares types by raw_name().
98 inline bool before(const Derived& rhs) const BOOST_NOEXCEPT {
99 const char* const left = derived().raw_name();
100 const char* const right = rhs.raw_name();
101 return left != right && std::strcmp(left, right) < 0;
104 /// \b Override: This function \b may be redefined in Derived class. Overrides \b must not throw.
105 /// \return Hash code of a type. By default hashes types by raw_name().
106 /// \note <boost/functional/hash.hpp> has to be included if this function is used.
107 inline std::size_t hash_code() const BOOST_NOEXCEPT {
108 const char* const name_raw = derived().raw_name();
109 return boost::hash_range(name_raw, name_raw + std::strlen(name_raw));
112 #if defined(BOOST_TYPE_INDEX_DOXYGEN_INVOKED)
114 /// \b Override: This function \b must be redefined in Derived class. Overrides \b must not throw.
115 /// \return Pointer to unredable/raw type name.
116 inline const char* raw_name() const BOOST_NOEXCEPT;
118 /// \b Override: This function \b may be redefined in Derived class. Overrides \b must not throw.
119 /// \return Const reference to underlying low level type_info_t.
120 inline const type_info_t& type_info() const BOOST_NOEXCEPT;
122 /// This is a factory method that is used to create instances of Derived classes.
123 /// boost::typeindex::type_id() will call this method, if Derived has same type as boost::typeindex::type_index.
125 /// \b Override: This function \b may be redefined and made public in Derived class. Overrides \b must not throw.
126 /// Overrides \b must remove const, volatile && and & modifiers from T.
127 /// \tparam T Type for which type_index must be created.
128 /// \return type_index for type T.
130 static Derived type_id() BOOST_NOEXCEPT;
132 /// This is a factory method that is used to create instances of Derived classes.
133 /// boost::typeindex::type_id_with_cvr() will call this method, if Derived has same type as boost::typeindex::type_index.
135 /// \b Override: This function \b may be redefined and made public in Derived class. Overrides \b must not throw.
136 /// Overrides \b must \b not remove const, volatile && and & modifiers from T.
137 /// \tparam T Type for which type_index must be created.
138 /// \return type_index for type T.
140 static Derived type_id_with_cvr() BOOST_NOEXCEPT;
142 /// This is a factory method that is used to create instances of Derived classes.
143 /// boost::typeindex::type_id_runtime(const T&) will call this method, if Derived has same type as boost::typeindex::type_index.
145 /// \b Override: This function \b may be redefined and made public in Derived class.
146 /// \param variable Variable which runtime type will be stored in type_index.
147 /// \return type_index with runtime type of variable.
149 static Derived type_id_runtime(const T& variable) BOOST_NOEXCEPT;
156 template <class Derived, class TypeInfo>
157 BOOST_CXX14_CONSTEXPR inline bool operator == (const type_index_facade<Derived, TypeInfo>& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
158 return static_cast<Derived const&>(lhs).equal(static_cast<Derived const&>(rhs));
161 template <class Derived, class TypeInfo>
162 BOOST_CXX14_CONSTEXPR inline bool operator < (const type_index_facade<Derived, TypeInfo>& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
163 return static_cast<Derived const&>(lhs).before(static_cast<Derived const&>(rhs));
168 template <class Derived, class TypeInfo>
169 BOOST_CXX14_CONSTEXPR inline bool operator > (const type_index_facade<Derived, TypeInfo>& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
173 template <class Derived, class TypeInfo>
174 BOOST_CXX14_CONSTEXPR inline bool operator <= (const type_index_facade<Derived, TypeInfo>& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
178 template <class Derived, class TypeInfo>
179 BOOST_CXX14_CONSTEXPR inline bool operator >= (const type_index_facade<Derived, TypeInfo>& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
183 template <class Derived, class TypeInfo>
184 BOOST_CXX14_CONSTEXPR inline bool operator != (const type_index_facade<Derived, TypeInfo>& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
185 return !(lhs == rhs);
188 // ######################### COMPARISONS with Derived ############################ //
189 template <class Derived, class TypeInfo>
190 inline bool operator == (const TypeInfo& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
191 return Derived(lhs) == rhs;
194 template <class Derived, class TypeInfo>
195 inline bool operator < (const TypeInfo& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
196 return Derived(lhs) < rhs;
199 template <class Derived, class TypeInfo>
200 inline bool operator > (const TypeInfo& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
201 return rhs < Derived(lhs);
204 template <class Derived, class TypeInfo>
205 inline bool operator <= (const TypeInfo& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
206 return !(Derived(lhs) > rhs);
209 template <class Derived, class TypeInfo>
210 inline bool operator >= (const TypeInfo& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
211 return !(Derived(lhs) < rhs);
214 template <class Derived, class TypeInfo>
215 inline bool operator != (const TypeInfo& lhs, const type_index_facade<Derived, TypeInfo>& rhs) BOOST_NOEXCEPT {
216 return !(Derived(lhs) == rhs);
220 template <class Derived, class TypeInfo>
221 inline bool operator == (const type_index_facade<Derived, TypeInfo>& lhs, const TypeInfo& rhs) BOOST_NOEXCEPT {
222 return lhs == Derived(rhs);
225 template <class Derived, class TypeInfo>
226 inline bool operator < (const type_index_facade<Derived, TypeInfo>& lhs, const TypeInfo& rhs) BOOST_NOEXCEPT {
227 return lhs < Derived(rhs);
230 template <class Derived, class TypeInfo>
231 inline bool operator > (const type_index_facade<Derived, TypeInfo>& lhs, const TypeInfo& rhs) BOOST_NOEXCEPT {
232 return Derived(rhs) < lhs;
235 template <class Derived, class TypeInfo>
236 inline bool operator <= (const type_index_facade<Derived, TypeInfo>& lhs, const TypeInfo& rhs) BOOST_NOEXCEPT {
237 return !(lhs > Derived(rhs));
240 template <class Derived, class TypeInfo>
241 inline bool operator >= (const type_index_facade<Derived, TypeInfo>& lhs, const TypeInfo& rhs) BOOST_NOEXCEPT {
242 return !(lhs < Derived(rhs));
245 template <class Derived, class TypeInfo>
246 inline bool operator != (const type_index_facade<Derived, TypeInfo>& lhs, const TypeInfo& rhs) BOOST_NOEXCEPT {
247 return !(lhs == Derived(rhs));
250 // ######################### COMPARISONS with Derived END ############################ //
254 #if defined(BOOST_TYPE_INDEX_DOXYGEN_INVOKED)
256 /// noexcept comparison operators for type_index_facade classes.
257 bool operator ==, !=, <, ... (const type_index_facade& lhs, const type_index_facade& rhs) noexcept;
259 /// noexcept comparison operators for type_index_facade and it's TypeInfo classes.
260 bool operator ==, !=, <, ... (const type_index_facade& lhs, const TypeInfo& rhs) noexcept;
262 /// noexcept comparison operators for type_index_facade's TypeInfo and type_index_facade classes.
263 bool operator ==, !=, <, ... (const TypeInfo& lhs, const type_index_facade& rhs) noexcept;
267 #ifndef BOOST_NO_IOSTREAM
268 #ifdef BOOST_NO_TEMPLATED_IOSTREAMS
270 /// Ostream operator that will output demangled name
271 template <class Derived, class TypeInfo>
272 inline std::ostream& operator<<(std::ostream& ostr, const type_index_facade<Derived, TypeInfo>& ind) {
273 ostr << static_cast<Derived const&>(ind).pretty_name();
278 /// Ostream operator that will output demangled name.
279 template <class CharT, class TriatT, class Derived, class TypeInfo>
280 inline std::basic_ostream<CharT, TriatT>& operator<<(
281 std::basic_ostream<CharT, TriatT>& ostr,
282 const type_index_facade<Derived, TypeInfo>& ind)
284 ostr << static_cast<Derived const&>(ind).pretty_name();
287 #endif // BOOST_NO_TEMPLATED_IOSTREAMS
288 #endif // BOOST_NO_IOSTREAM
290 /// This free function is used by Boost's unordered containers.
291 /// \note <boost/functional/hash.hpp> has to be included if this function is used.
292 template <class Derived, class TypeInfo>
293 inline std::size_t hash_value(const type_index_facade<Derived, TypeInfo>& lhs) BOOST_NOEXCEPT {
294 return static_cast<Derived const&>(lhs).hash_code();
297 }} // namespace boost::typeindex
299 #endif // BOOST_TYPE_INDEX_TYPE_INDEX_FACADE_HPP