HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
process.hpp
1
2#pragma once
3
4#include "fixed_string.hpp"
5#include <typeinfo>
6#include <string_view>
7
8namespace hi {
9inline namespace v1 {
10namespace detail {
11
13 virtual ~process_call_functor_base() = default;
14
15 virtual void operator()() const noexcept = 0;
16};
17
19
20template<typename Functor>
22 process_call_functor() noexcept
23 {
24 process_call_functors[name] = *this;
25 }
26
27 std::string_view name() const noexcept
28 {
29 return typeid(Functor).name();
30 }
31
32 void operator()() const noexcept override
33 {
34 return Functor{}();
35 }
36};
37
38template<typename Functor>
39inline global_process_call_functor = process_call_functor<Functor>{};
40
41void process_call_trampoline(std::string_view name, std::string_view data)
42{
43 auto it = process_call_functors.find(name);
44 if (it == process_call_functors.end()) {
45 throw key_error(std::format("Functor '{}' not registered", name));
46 }
47 return (*it)(data);
48}
49
50}
51
58template<typename Functor>
59void process_call(Functor &&functor, std::string_view data)
60{
61 static_assert(requires { Functor{}(data); }, "process_call() must be called with functor.");
62
63 auto &functor = global_process_call_functor<Functor>:
64
65 // Trampoline the call by executing the same executable in a new process.
66 auto args = std::vector<std::string>{};
67 args.push_back(process_path());
68 args.push_back(process_name());
69 args.push_back(std::format("--process-call={},{}", functor.name(), data);
70 auto pid = process_exec(std::move(args));
71 process_wait(pid);
72};
73
74
75}}
DOXYGEN BUG.
Definition algorithm.hpp:15
geometry/margins.hpp
Definition assert.hpp:18
void process_call(Functor &&functor, std::string_view data)
Call a function in a new process.
Definition process.hpp:59
Definition exception.hpp:106
Definition process.hpp:21
T move(T... args)
T push_back(T... args)