pars 0.2.1.99
Loading...
Searching...
No Matches
job.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 "nngxx/msg.h"
33
34#include "pars/ev/kind.h"
35#include "pars/ev/metadata.h"
36#include "pars/ev/serializer.h"
37#include "pars/ev/spec.h"
38
39#include <any>
40#include <format>
41#include <type_traits>
42
43namespace pars::ev
44{
45
46class job
47{
48public:
49 job(std::size_t j_id, int s_id, std::size_t h, std::any ke)
50 : id_m{j_id}
51 , socket_id_m{s_id}
52 , spec_hash_m{h}
53 , event_kind_m{std::move(ke)}
54 {
55 }
56
57 job(const job&) = delete;
58
59 job(job&&) = default;
60
61 job& operator=(const job&) = delete;
62
63 job& operator=(job&& j) = default;
64
65 template<template<typename> typename kind_of, event_c event_t>
67 std::is_same_v<event_t, nngxx::msg>)
68 kind_of<event_t> event()
69 {
70 return std::any_cast<kind_of<event_t>>(std::move(event_kind_m));
71 }
72
73 template<template<typename> typename kind_of, event_c event_t>
75 !std::is_same_v<event_t, nngxx::msg>)
77 {
78 // get the received<nngxx::msg>
80
81 auto& md = r.md();
82
83 // return a received<event_t>
84 return received{
86 metadata<received, event_t>{md.socket_id(), md.tool(), md.pipe()}};
87 }
88
89 std::size_t id() const { return id_m; }
90
91 int socket_id() const { return socket_id_m; }
92
93 std::size_t spec_hash() const { return spec_hash_m; }
94
95 auto format_to(std::format_context& ctx) const -> decltype(ctx.out())
96 {
97 return std::format_to(ctx.out(), "spec:0x{:X}", spec_hash());
98 }
99
100 void set_id(std::size_t id) { id_m = id; }
101
102private:
103 std::size_t id_m;
104 int socket_id_m;
105 std::size_t spec_hash_m;
106 std::any event_kind_m;
107};
108
109template<template<typename> typename kind_of, event_c event_t>
110 requires kind_c<kind_of>
111static std::size_t compute_spec_hash(const kind_of<event_t>& ke)
112{
113 if constexpr (std::is_same_v<kind_of<event_t>, received<nngxx::msg>>)
114 return ke.msg_hash();
115 else
116 return spec<kind_of<event_t>>::hash;
117}
118
119template<template<typename> typename kind_of, event_c event_t>
120 requires kind_c<kind_of>
121static job make_job(std::size_t j_id, kind_of<event_t> ke)
122{
123 auto h = compute_spec_hash(ke);
124
125 return job(j_id, ke.md().socket_id(), h,
126 std::make_any<kind_of<event_t>>(std::move(ke)));
127}
128
129} // namespace pars::ev
received< event_t > event()
Definition job.h:76
std::size_t id() const
Definition job.h:89
job & operator=(job &&j)=default
auto format_to(std::format_context &ctx) const -> decltype(ctx.out())
Definition job.h:95
job & operator=(const job &)=delete
job(const job &)=delete
int socket_id() const
Definition job.h:91
job(job &&)=default
job(std::size_t j_id, int s_id, std::size_t h, std::any ke)
Definition job.h:49
kind_of< event_t > event()
Definition job.h:68
void set_id(std::size_t id)
Definition job.h:100
std::size_t spec_hash() const
Definition job.h:93
constexpr auto is_same_kind_v
Definition kind.h:56
static event_t to_event(const nngxx::msg &m)
Definition serializer.h:83