TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_daemon.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-25 14:49:21 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
26#pragma once
27
28#include "tec/tec_def.hpp" // IWYU pragma: keep
29#include "tec/tec_status.hpp"
30#include "tec/tec_signal.hpp"
31#include "tec/tec_message.hpp"
32
33
34namespace tec {
35
47class Daemon {
48public:
54 Daemon() = default;
55
60 virtual ~Daemon() = default;
61
65 Daemon(const Daemon&) = delete;
69 Daemon(Daemon&&) = delete;
70
77 virtual Status run() = 0;
78
86 virtual Status terminate() = 0;
87
94 virtual void send(Message&& msg) = 0;
95
102 virtual const Signal& sig_terminated() const = 0;
103
115 virtual Status make_request(Request&&, Reply&&) = 0;
116
131 template <typename TRequest, typename TReply>
132 Status request(const TRequest* req, TReply* rep) {
133 return make_request({req}, {rep});
134 }
135
140 template <typename TRequest>
141 Status request(const TRequest* req) {
142 return make_request({req}, {});
143 }
144
145public:
146
154 template <typename Derived>
155 struct Builder {
163 std::unique_ptr<Daemon> operator()(typename Derived::Params const& params) {
164 static_assert(std::is_base_of<Daemon, Derived>::value,
165 "Derived type must inherit from tec::Daemon");
166 return std::make_unique<Derived>(params);
167 }
168 };
169
170}; // class Daemon
171
172} // namespace tec
Abstract interface for a daemon that runs in a separate thread.
Definition tec_daemon.hpp:47
Daemon(Daemon &&)=delete
Daemons are non-movable to ensure unique ownership.
virtual Status make_request(Request &&, Reply &&)=0
Sends a request and waits for a reply in a daemon process.
virtual void send(Message &&msg)=0
Sends a control message to the daemon.
Daemon(const Daemon &)=delete
Daemons are non-copyable to ensure unique ownership.
virtual ~Daemon()=default
Virtual destructor for safe polymorphic deletion.
Status request(const TRequest *req)
Helper: Sends a notification request – no reply required.
Definition tec_daemon.hpp:141
Daemon()=default
Default constructor.
virtual Status run()=0
Starts the daemon's operation.
virtual Status terminate()=0
Terminates the daemon's operation.
Status request(const TRequest *req, TReply *rep)
Helper: Sends a request and waits for a reply in a daemon process.
Definition tec_daemon.hpp:132
virtual const Signal & sig_terminated() const =0
Retrieves the signal indicating the daemon is terminated.
A thread-safe signal mechanism for inter-thread synchronization.
Definition tec_signal.hpp:44
Factory for creating daemon instances.
Definition tec_daemon.hpp:155
std::unique_ptr< Daemon > operator()(typename Derived::Params const &params)
Creates a unique pointer to a derived daemon instance.
Definition tec_daemon.hpp:163
Common definitions and utilities for the tec namespace.
Defines a flexible message type and helper functions for the tec namespace.
std::any Reply
Type alias for a reply object that can hold any object.
Definition tec_message.hpp:55
std::any Message
Type alias for a message that can hold any object.
Definition tec_message.hpp:43
std::any Request
Type alias for a request object that can hold any object.
Definition tec_message.hpp:49
Defines a thread-safe signal implementation using mutex and condition variable.
Defines error handling types and utilities for the tec namespace.