HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
trace.hpp
1// Copyright Take Vos 2019, 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "fixed_string.hpp"
8#include "time_stamp_count.hpp"
9#include "counters.hpp"
10#include "datum.hpp"
11#include <array>
12#include <tuple>
13
14namespace hi::inline v1 {
15
17public:
18 trace_base(trace_base const &) = delete;
19 trace_base(trace_base &&) = delete;
20 trace_base &operator=(trace_base const &) = delete;
21 trace_base &operator=(trace_base &&) = delete;
22
23 trace_base() noexcept : _time_stamp(time_stamp_count::inplace{}), _next(std::exchange(_top, this)) {}
24
25 virtual ~trace_base()
26 {
27 _top = _next;
28 }
29
30 virtual void log() const noexcept = 0;
31
32protected:
33 inline static thread_local trace_base *_top = nullptr;
34
35 time_stamp_count _time_stamp;
36 trace_base *_next = nullptr;
37};
38
39template<fixed_string Tag, int NumItems = 0>
40class trace : public trace_base {
41public:
42 trace() noexcept : trace_base(), items(), size(0) {}
43
44 virtual ~trace() noexcept
45 {
46 if (std::uncaught_exceptions()) {
47 log();
48 }
49
50 hilet current_time_stamp = time_stamp_count{time_stamp_count::inplace{}};
51 global_counter<Tag>.add_duration(current_time_stamp.count() - _time_stamp.count());
52 }
53
54 void log() const noexcept override
55 {
56 if (_next) {
57 _next->log();
58 }
59 }
60
61 template<typename T>
62 void set(char const *key, T &&value) noexcept
63 {
64 // XXX Use type erase to store the type of the value and a pointer
65 // to the location of the value on the stack.
66
67 hi_axiom(size < NumItems);
68 items[size++] = {key, datum{std::forward<T>(value)}};
69 }
70
71private:
73 std::size_t size = 0;
74};
75
76template<fixed_string Tag>
77class trace<Tag, 0> : public trace_base {
78public:
79 trace() noexcept : trace_base() {}
80
81 virtual ~trace() noexcept
82 {
83 if (std::uncaught_exceptions()) {
84 log();
85 }
86
87 hilet current_time_stamp = time_stamp_count{time_stamp_count::inplace{}};
88 global_counter<Tag>.add_duration(current_time_stamp.count() - _time_stamp.count());
89 }
90
91 void log() const noexcept override
92 {
93 if (_next) {
94 _next->log();
95 }
96 }
97};
98
99} // namespace hi::inline v1
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
A dynamic data type.
Definition datum.hpp:224
Since Window's 10 QueryPerformanceCounter() counts at only 10MHz which is too low to measure performa...
Definition time_stamp_count.hpp:29
Definition time_stamp_count.hpp:31
Definition trace.hpp:16
Definition trace.hpp:40