Skip to content

Commit b385716

Browse files
committed
chore: use core::typeinfo
1 parent b77b328 commit b385716

3 files changed

Lines changed: 84 additions & 109 deletions

File tree

include/boost/rts/detail/type_info.hpp

Lines changed: 0 additions & 77 deletions
This file was deleted.

include/boost/rts/polystore.hpp

Lines changed: 82 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,88 @@
1313
#include <boost/rts/detail/config.hpp>
1414
#include <boost/rts/detail/call_traits.hpp>
1515
#include <boost/rts/detail/except.hpp>
16-
#include <boost/rts/detail/type_info.hpp>
1716
#include <boost/rts/detail/type_traits.hpp>
17+
#include <boost/core/typeinfo.hpp>
1818
#include <boost/core/detail/static_assert.hpp>
19+
#include <cstring>
1920
#include <memory>
2021
#include <type_traits>
2122
#include <unordered_map>
2223
#include <vector>
2324

25+
#if ! defined( BOOST_NO_TYPEID )
26+
#include <typeindex>
27+
#endif
28+
2429
namespace boost {
2530
namespace rts {
2631

32+
namespace detail {
33+
34+
#if defined( BOOST_NO_TYPEID )
35+
36+
struct typeindex
37+
{
38+
typeindex(
39+
core::typeinfo const& ti) noexcept
40+
: n_(std::strlen(ti.name()))
41+
, ti_(&ti)
42+
{
43+
}
44+
45+
std::size_t hash_code() const noexcept
46+
{
47+
constexpr std::size_t offset_basis =
48+
(sizeof(std::size_t) == 8)
49+
? 1469598103934665603ull
50+
: 2166136261u;
51+
constexpr std::size_t prime =
52+
(sizeof(std::size_t) == 8)
53+
? 1099511628211ull
54+
: 16777619u;
55+
auto const s = ti_->name();
56+
std::size_t h = offset_basis;
57+
for(std::size_t i = 0; i < n_; ++i)
58+
h = (h ^ static_cast<unsigned char>(s[i])) * prime;
59+
return h;
60+
}
61+
62+
bool operator==(typeindex const& other) const noexcept
63+
{
64+
return n_ == other.n_ && *ti_ == *other.ti_;
65+
}
66+
67+
private:
68+
std::size_t n_;
69+
core::typeinfo const* ti_;
70+
};
71+
72+
} // detail
73+
} // rts
74+
} // boost
75+
namespace std {
76+
template<>
77+
struct hash< boost::rts::detail::typeindex >
78+
{
79+
std::size_t operator()(
80+
boost::rts::detail::typeindex const& t) const noexcept
81+
{
82+
return t.hash_code();
83+
}
84+
};
85+
} // std
86+
namespace boost {
87+
namespace rts {
88+
namespace detail {
89+
90+
#else
91+
92+
using typeindex = std::type_index;
93+
94+
#endif
95+
96+
} // detail
97+
2798
/** A container of type-erased objects
2899
29100
Objects are stored and retrieved by their type.
@@ -135,8 +206,7 @@ class polystore
135206
template<class T>
136207
T* find() const noexcept
137208
{
138-
return static_cast<T*>(find(
139-
detail::get_type_info<T>()));
209+
return static_cast<T*>(find(BOOST_CORE_TYPEID(T)));
140210
}
141211

142212
/** Return a reference to the object associated with type T
@@ -457,8 +527,11 @@ class polystore
457527

458528
struct key
459529
{
460-
detail::type_info const* ti;
530+
detail::typeindex ti;
461531
void* p;
532+
533+
key(detail::typeindex const& ti_,
534+
void* p_) noexcept : ti(ti_) , p(p_) {}
462535
};
463536

464537
template<class T, class... Key>
@@ -471,7 +544,7 @@ class polystore
471544
key kn[1];
472545

473546
explicit keyset(T& t) noexcept
474-
: kn{{ &detail::get_type_info<T>(), &t }}
547+
: kn{{ detail::typeindex(BOOST_CORE_TYPEID(T)), &t }}
475548
{
476549
}
477550
};
@@ -484,9 +557,9 @@ class polystore
484557

485558
explicit keyset(T& t) noexcept
486559
: kn{
487-
{ &detail::get_type_info<T>(),
560+
{ detail::typeindex(BOOST_CORE_TYPEID(T)),
488561
std::addressof(t) },
489-
{ &detail::get_type_info<Keys>(),
562+
{ detail::typeindex(BOOST_CORE_TYPEID(Keys)),
490563
&static_cast<Keys&>(t) }...,
491564
}
492565
{
@@ -509,34 +582,13 @@ class polystore
509582
void destroy() noexcept;
510583
BOOST_RTS_DECL any& get(std::size_t i);
511584
BOOST_RTS_DECL void* find(
512-
detail::type_info const& ti) const noexcept;
585+
core::typeinfo const& ti) const noexcept;
513586
BOOST_RTS_DECL void* insert_impl(any_ptr,
514587
key const* = nullptr, std::size_t = 0);
515588

516-
struct hash
517-
{
518-
std::size_t operator()(
519-
detail::type_info const* p) const noexcept
520-
{
521-
return p->hash_code();
522-
}
523-
};
524-
525-
struct eq
526-
{
527-
bool operator()(
528-
detail::type_info const* p0,
529-
detail::type_info const* p1) const noexcept
530-
{
531-
return *p0 == *p1;
532-
}
533-
};
534-
535589
std::vector<any_ptr> v_;
536590
std::unordered_map<
537-
detail::type_info const*,
538-
void*, hash, eq> m_;
539-
591+
detail::typeindex, void*> m_;
540592
};
541593

542594
//------------------------------------------------

src/polystore.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ get(std::size_t i) -> any&
8282
void*
8383
polystore::
8484
find(
85-
detail::type_info const& ti) const noexcept
85+
core::typeinfo const& ti) const noexcept
8686
{
87-
auto const it = m_.find(&ti);
87+
auto const it = m_.find(ti);
8888
if(it == m_.end())
8989
return nullptr;
9090
return it->second;

0 commit comments

Comments
 (0)