Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
run_context.hpp
Go to the documentation of this file.
1
5#pragma once
6#include "metadata.hpp"
7#include "program.hpp"
8#include <functional>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <type_traits>
13#include <utility>
14#include <vector>
15
16namespace uni20
17{
20{
21 long double monotonic_seconds;
22 std::optional<long double> process_cpu_seconds = std::nullopt;
23};
24
26[[nodiscard]] std::string utc_now();
28[[nodiscard]] std::string metadata_line(std::string_view text);
29
30enum class run_outcome
31{
32 success,
33 partial,
34 failed,
36};
37[[nodiscard]] std::string_view to_string(run_outcome value);
38
40{
42 std::function<std::string()> utc = utc_now;
44 std::vector<std::string> invocation = {};
45};
46
51 public:
53 run_context(run_context const&) = delete;
55
56 [[nodiscard]] metadata_document& metadata();
57 [[nodiscard]] metadata_document snapshot() const { return metadata_; }
58 [[nodiscard]] std::vector<std::string> const& invocation() const { return options_.invocation; }
59 [[nodiscard]] presentation::program_info const& program() const { return program_; }
60
62 public:
63 explicit computation_scope(run_context& owner);
66 ~computation_scope() noexcept;
68 void finish();
69
70 private:
71 run_context* owner_;
72 };
73 [[nodiscard]] computation_scope computation() { return computation_scope(*this); }
74 template <typename F> decltype(auto) measure(F&& function)
75 {
76 auto scope = this->computation();
77 if constexpr (std::is_void_v<std::invoke_result_t<F>>)
78 {
79 std::invoke(std::forward<F>(function));
80 scope.finish();
81 }
82 else
83 {
84 decltype(auto) result = std::invoke(std::forward<F>(function));
85 scope.finish();
86 return result;
87 }
88 }
89
92 [[nodiscard]] metadata_document const& finish(run_outcome outcome, metadata_document summary = {});
93 [[nodiscard]] bool finished() const { return final_.has_value(); }
94
95 private:
96 void begin_computation();
97 void end_computation();
99 run_context_options options_;
100 metadata_document metadata_;
101 run_clock_sample start_;
102 std::optional<run_clock_sample> computation_start_;
103 std::optional<long double> computation_cpu_ = 0;
104 std::optional<metadata_document> final_;
105};
106
108[[nodiscard]] presentation::report_builder metadata_report(metadata_document const& document, std::string title = {},
109 bool detail = false);
110} // namespace uni20
Ordered, uniquely identified metadata. Copies are independent snapshots.
Definition metadata.hpp:306
Builder for simple terminal reports with headings, status lines, fields, and tables.
Definition presentation.hpp:602
Definition run_context.hpp:61
void finish()
End the interval explicitly, reporting clock errors to the caller.
Definition run_context.cpp:143
computation_scope(computation_scope const &)=delete
~computation_scope() noexcept
Definition run_context.cpp:134
computation_scope & operator=(computation_scope const &)=delete
Parser-independent run state.
Definition run_context.hpp:50
run_context(run_context const &)=delete
decltype(auto) measure(F &&function)
Definition run_context.hpp:74
run_context & operator=(run_context const &)=delete
std::vector< std::string > const & invocation() const
Definition run_context.hpp:58
metadata_document snapshot() const
Definition run_context.hpp:57
metadata_document & metadata()
Definition run_context.cpp:102
computation_scope computation()
Definition run_context.hpp:73
presentation::program_info const & program() const
Definition run_context.hpp:59
bool finished() const
Definition run_context.hpp:93
locale_t value
Definition data_table.cpp:49
presentation::styled_text text
Definition display.cpp:141
Owned native scalar metadata, independent of parsers and output streams.
Definition async/async.hpp:26
presentation::report_builder metadata_report(metadata_document const &document, std::string title, bool detail)
Render ordered groups without using display strings as the machine metadata source.
Definition run_context.cpp:165
run_outcome
Definition run_context.hpp:31
run_clock_sample system_run_clock()
Definition run_context.cpp:24
std::string utc_now()
Definition run_context.cpp:35
auto to_string(basic_half_int< T > value) -> std::string
Format a half-integer as an integer or decimal .5.
Definition half_int.hpp:477
std::string metadata_line(std::string_view text)
Escape control bytes for a single human-readable metadata line.
Definition run_context.cpp:46
Parser-independent application identity and help documents.
Application identity and explanatory content, independent of any option parser.
Definition program.hpp:32
One clock sample; unavailable process CPU time is distinct from zero.
Definition run_context.hpp:20
long double monotonic_seconds
Definition run_context.hpp:21
std::optional< long double > process_cpu_seconds
Definition run_context.hpp:22
Definition run_context.hpp:40
std::vector< std::string > invocation
Empty omits invocation capture; supply owned argv tokens explicitly to opt in.
Definition run_context.hpp:44
std::function< run_clock_sample()> clock
Definition run_context.hpp:41
std::function< std::string()> utc
Definition run_context.hpp:42