pars 0.2.1
Loading...
Searching...
No Matches
make_hf.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/event.h"
33#include "pars/concept/kind.h"
34
35#include <functional>
36#include <ratio>
37
38namespace pars::ev
39{
40
41template<typename callable_t>
42struct hf_traits;
43
44template<template<typename> typename kind_of, event_c event_t>
45 requires kind_c<kind_of>
46using hf_arg = kind_of<event_t>;
47
48template<template<typename> typename kind_of, event_c event_t>
49 requires kind_c<kind_of>
50using handler_f = std::move_only_function<void(hf_arg<kind_of, event_t>)>;
51
52template<
53 typename mem_fn_t,
54 template<typename> typename kind_of = hf_traits<mem_fn_t>::template kind_type,
55 typename event_t = hf_traits<mem_fn_t>::event_type,
56 typename class_t = hf_traits<mem_fn_t>::class_type>
58handler_f<kind_of, event_t> make_hf(mem_fn_t& mem_fn, class_t* self)
59{
60 using namespace std::placeholders;
61
62 static constexpr std::size_t arity = hf_traits<mem_fn_t>::arity;
63
64 if constexpr (std::is_same_v<class_t, void>)
65 {
66 return handler_f<kind_of, event_t>(std::bind(mem_fn, _1));
67 }
68 else if constexpr (std::is_same_v<std::integral_constant<std::size_t, arity>,
69 std::integral_constant<std::size_t, 0>>)
70 {
72 std::bind(mem_fn, static_cast<class_t*>(self)));
73 }
74 else if constexpr (std::is_same_v<std::integral_constant<std::size_t, arity>,
75 std::integral_constant<std::size_t, 1>>)
76 {
78 std::bind(mem_fn, static_cast<class_t*>(self), _1));
79 }
80 else if constexpr (std::is_same_v<std::integral_constant<std::size_t, arity>,
81 std::integral_constant<std::size_t, 2>>)
82 {
84 std::bind(mem_fn, static_cast<class_t*>(self), _1, _2));
85 }
86
87 static_assert(std::ratio_less_equal_v<std::ratio<arity>, std::ratio<2>>,
88 "Only fn with arity <= 2 are supported");
89}
90
91template<typename return_t, typename class_t,
92 template<typename> typename kind_of, event_c event_t>
93struct hf_traits<return_t (class_t::*)(hf_arg<kind_of, event_t>)>
94{
95 using return_type = return_t;
96
97 using class_type = class_t;
98
99 template<typename event2_t>
100 using kind_type = kind_of<event2_t>;
101
102 using event_type = event_t;
103
104 static constexpr std::size_t arity = 1;
105};
106
107} // namespace pars::ev
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