pars 0.2.1.99
Loading...
Searching...
No Matches
event.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 "common.h"
33
34#include <pars/pars.h>
35
36#include <format>
37#include <string_view>
38
40{
41
42// a client send this to request a fib_n computation
44{
45 std::size_t work_id = 0;
46 uint64_t n = 0;
47 bool use_fast_fib = false;
48
49 auto format_to(std::format_context& ctx) const -> decltype(ctx.out())
50 {
51 return std::format_to(ctx.out(), "fib_requested({},{},{})", work_id, n,
52 use_fast_fib ? "fast_fib" : "slow_fib");
53 }
54};
55
56// a server backend send this in response for a fib_n computation
58{
59 std::size_t work_id = 0;
60 uint64_t fib_n = 0;
61
62 auto format_to(std::format_context& ctx) const -> decltype(ctx.out())
63 {
64 return std::format_to(ctx.out(), "fib_computed({},{})", work_id, fib_n);
65 }
66};
67
69{
70 int pipe_id = 0;
71
72 auto format_to(std::format_context& ctx) const -> decltype(ctx.out())
73 {
74 return std::format_to(ctx.out(), "stop_compute({})", pipe_id);
75 }
76};
77
78} // namespace pars_example::event
79
80template<>
82 : base_klass<::pars_example::event::fib_requested>
83{
84 static constexpr std::string_view uuid =
85 "9e85e1e6-28b1-4e52-85d6-12ca0110049f";
86
87 template<typename Archive>
88 static void serialize(event_type& ev, Archive& ar)
89 {
90 ar(ev.work_id, ev.n, ev.use_fast_fib);
91 }
92
93 template<template<typename> typename kind_of>
94 requires kind_c<kind_of>
95 static constexpr executes exec_policy()
96 {
98 return executes::async;
99 else
101 }
102};
103
104template<>
106 : base_klass<::pars_example::event::fib_computed>
107{
108 static constexpr std::string_view uuid =
109 "3dc87ab0-bfe0-4eea-8f71-76a16bda62ff";
110
111 template<typename Archive>
112 static void serialize(event_type& ev, Archive& ar)
113 {
114 ar(ev.work_id, ev.fib_n);
115 }
116};
117
118template<>
120 : base_klass<::pars_example::event::stop_compute>
121{
122 static constexpr std::string_view uuid =
123 "8194470a-3ad8-4f37-a544-d44ff4e5bd29";
124
125 template<typename Archive>
126 static void serialize(event_type& ev, Archive& ar)
127 {
128 ar(ev.pipe_id);
129 }
130};
constexpr auto enable_compute_fib_async
Definition common.h:32
constexpr auto is_same_kind_v
Definition kind.h:56
executes
Definition event.h:41
static constexpr executes exec_policy()
an event_t executes synchronously in every possibile kind_of<event_t>
Definition klass.h:61
static void serialize(event_type &ev, Archive &ar)
Definition event.h:112
static constexpr std::string_view uuid
Definition event.h:108
static constexpr std::string_view uuid
Definition event.h:84
static void serialize(event_type &ev, Archive &ar)
Definition event.h:88
static constexpr std::string_view uuid
Definition event.h:122
static void serialize(event_type &ev, Archive &ar)
Definition event.h:126
::pars_example::event::fib_requested event_type
Definition klass.h:70
auto format_to(std::format_context &ctx) const -> decltype(ctx.out())
Definition event.h:62
auto format_to(std::format_context &ctx) const -> decltype(ctx.out())
Definition event.h:49
auto format_to(std::format_context &ctx) const -> decltype(ctx.out())
Definition event.h:72