pars 0.2.1
Loading...
Searching...
No Matches
hf_registry.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "pars/concept/kind.h"
33#include "pars/ev/job.h"
34#include "pars/ev/make_hf.h"
35#include "pars/ev/spec.h"
37#include "pars/log.h"
38
39#include <format>
40#include <mutex>
41#include <unordered_map>
42
43namespace pars::ev
44{
45
46class runner;
47
48} // namespace pars::ev
49
50namespace pars::net
51{
52
53class rep;
54class req;
55
56} // namespace pars::net
57
58namespace pars::ev
59{
60
61using job_handler_f = std::function<void(job)>;
62
64{
65public:
66 hf_registry(runner& r)
67 : runner_m{r}
68 {
69 }
70
71 template<template<typename> typename kind_of, ev::event_c event_t,
72 typename class_t>
73 requires ev::kind_c<kind_of>
74 void on(void (class_t::*mem_fn)(hf_arg<kind_of, event_t>), class_t* self)
75 {
76 insert<kind_of, event_t>(make_hf(mem_fn, self));
77 }
78
79private:
80 friend net::rep;
81 friend net::req;
82 friend runner;
83
84 template<template<typename> typename kind_of, event_c event_t>
85 requires kind_c<kind_of>
86 void insert(handler_f<kind_of, event_t> hf)
87 {
88 insert(0, std::move(hf));
89 }
90
92 template<template<typename> typename kind_of, event_c event_t>
93 requires kind_c<kind_of>
94 void insert(int s_id, handler_f<kind_of, event_t> hf);
95
96 auto lock() { return std::unique_lock{mtx_m}; }
97
98 bool has_handler_for(int s_id, std::size_t spec_hash)
99 {
100 return handlers_m[s_id].contains(spec_hash);
101 }
102
103 const std::type_info* const type_for(std::size_t spec_hash)
104 {
105 return types_m[spec_hash];
106 }
107
108 const job_handler_f& handler_for(int s_id, std::size_t spec_hash)
109 {
110 return handlers_m[s_id][spec_hash];
111 }
112
113 template<template<typename> typename kind_of, event_c event_t>
114 requires kind_c<kind_of>
115 auto insert_jhf(int s_id, job_handler_f hf)
116 {
117 auto spec_hash = spec<kind_of<event_t>>::hash;
118
119 if (!handlers_m[s_id].try_emplace(spec_hash, std::move(hf)).second)
120 throw std::runtime_error(std::format(
121 "Unable to emplace the handler_f for Socket #{} and Spec {:X}", s_id,
122 spec_hash));
123
124 // register the type for logging purpose
125 types_m[spec_hash] = &typeid(kind_of<event_t>);
126
127 pars::debug(SL, lf::event, "Socket {}: Registered {}!", s_id,
128 spec<kind_of<event_t>>{});
129 }
130
131 std::mutex mtx_m;
132 std::unordered_map<int, std::unordered_map<std::size_t, job_handler_f>>
133 handlers_m;
134 std::unordered_map<std::size_t, const std::type_info*>
135 types_m;
136
137 runner& runner_m;
138};
139
140} // namespace pars::ev
Represents an nng_rep protocol.
Definition rep.h:45
Represents an nng_req protocol.
Definition req.h:45
#define SL
Definition log.h:58
std::function< void(job)> job_handler_f
Definition hf_registry.h:61
handler_f< kind_of, event_t > make_hf(mem_fn_t &mem_fn, class_t *self)
Definition make_hf.h:58
std::move_only_function< void(hf_arg< kind_of, event_t >)> handler_f
Definition make_hf.h:50
kind_of< event_t > hf_arg
Definition make_hf.h:46
@ event
Definition flags.h:43
void debug(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:129
void on(void(class_t::*mem_fn)(hf_arg< kind_of, event_t >), class_t *self)
Definition hf_registry.h:74