34
34
#include < limits>
35
35
#include < functional>
36
36
#include < unordered_map>
37
- #include < variant>
38
- #endif
37
+
38
+ namespace zmq
39
+ {
40
+ // socket ref or native file descriptor for poller
41
+ class poller_ref_t
42
+ {
43
+ public:
44
+ enum RefType
45
+ {
46
+ RT_SOCKET,
47
+ RT_FD
48
+ };
49
+
50
+ poller_ref_t () : poller_ref_t (socket_ref{})
51
+ {}
52
+
53
+ poller_ref_t (const zmq::socket_ref& socket) : data{RT_SOCKET, socket, {}}
54
+ {}
55
+
56
+ poller_ref_t (zmq::fd_t fd) : data{RT_FD, {}, fd}
57
+ {}
58
+
59
+ size_t hash () const ZMQ_NOTHROW
60
+ {
61
+ std::size_t h = 0 ;
62
+ hash_combine (h, std::get<0 >(data));
63
+ hash_combine (h, std::get<1 >(data));
64
+ hash_combine (h, std::get<2 >(data));
65
+ return h;
66
+ }
67
+
68
+ bool operator == (const poller_ref_t & o) const ZMQ_NOTHROW
69
+ {
70
+ return data == o.data ;
71
+ }
72
+
73
+ private:
74
+ template <class T >
75
+ static void hash_combine (std::size_t & seed, const T& v) ZMQ_NOTHROW
76
+ {
77
+ std::hash<T> hasher;
78
+ seed ^= hasher (v) + 0x9e3779b9 + (seed<<6 ) + (seed>>2 );
79
+ }
80
+
81
+ std::tuple<int , zmq::socket_ref, zmq::fd_t > data;
82
+
83
+ }; // class poller_ref_t
84
+
85
+ } // namespace zmq
86
+
87
+ // std::hash<> specialization for std::unordered_map
88
+ template <> struct std ::hash<zmq::poller_ref_t >
89
+ {
90
+ size_t operator ()(const zmq::poller_ref_t & ref) const ZMQ_NOTHROW
91
+ {
92
+ return ref.hash ();
93
+ }
94
+ };
95
+ #endif // ZMQ_CPP11
39
96
40
97
namespace zmq
41
98
{
@@ -684,7 +741,7 @@ class active_poller_t
684
741
685
742
void add (zmq::socket_ref socket, event_flags events, handler_type handler)
686
743
{
687
- const Ref ref{socket};
744
+ const poller_ref_t ref{socket};
688
745
689
746
if (!handler)
690
747
throw std::invalid_argument (" null handler in active_poller_t::add (socket)" );
@@ -705,7 +762,7 @@ class active_poller_t
705
762
706
763
void add (fd_t fd, event_flags events, handler_type handler)
707
764
{
708
- const Ref ref{fd};
765
+ const poller_ref_t ref{fd};
709
766
710
767
if (!handler)
711
768
throw std::invalid_argument (" null handler in active_poller_t::add (fd)" );
@@ -778,8 +835,7 @@ class active_poller_t
778
835
779
836
poller_t <handler_type> base_poller{};
780
837
781
- using Ref = std::variant<socket_ref, fd_t >;
782
- std::unordered_map<Ref, std::shared_ptr<handler_type>> handlers{};
838
+ std::unordered_map<zmq::poller_ref_t , std::shared_ptr<handler_type>> handlers{};
783
839
784
840
std::vector<decltype (base_poller)::event_type> poller_events{};
785
841
std::vector<std::shared_ptr<handler_type>> poller_handlers{};
0 commit comments