• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

celerity / celerity-runtime / 12052892324

27 Nov 2024 02:58PM UTC coverage: 95.004% (+0.05%) from 94.956%
12052892324

Pull #314

github

web-flow
Merge 372a0f7bf into f2a0a76b4
Pull Request #314: Exclude src/testspy from coverage

3180 of 3611 branches covered (88.06%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 2 files covered. (100.0%)

4 existing lines in 2 files now uncovered.

7070 of 7178 relevant lines covered (98.5%)

1279329.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.84
/include/utils.h
1
#pragma once
2

3
#include "types.h"
4

5
#include <algorithm>
6
#include <cassert>
7
#include <concepts>
8
#include <cstddef>
9
#include <functional>
10
#include <string>
11
#include <string_view>
12
#include <type_traits>
13
#include <typeinfo>
14
#include <utility>
15

16
#include <fmt/format.h>
17

18

19
#define CELERITY_DETAIL_UTILS_CAT_2(a, b) a##b
20
#define CELERITY_DETAIL_UTILS_CAT(a, b) CELERITY_DETAIL_UTILS_CAT_2(a, b)
21

22
namespace celerity::detail::utils {
23

24
/// Like std::move, but move-constructs the result so it does not reference the argument after returning.
25
template <typename T>
26
T take(T& from) {
752✔
27
        return std::move(from);
752✔
28
}
29

30
template <typename T, typename P>
31
bool isa(const P* p) {
85,835✔
32
        return dynamic_cast<const T*>(p) != nullptr;
85,835!
33
}
34

35
template <typename T, typename P>
36
auto as(P* p) {
15,987✔
37
        assert(isa<T>(p));
15,987✔
38
        return static_cast<std::conditional_t<std::is_const_v<P>, const T*, T*>>(p);
15,987✔
39
}
40

41
// Implementation from Boost.ContainerHash, licensed under the Boost Software License, Version 1.0.
42
inline void hash_combine(std::size_t& seed, std::size_t value) { seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2); }
67,798✔
43

44
struct pair_hash {
45
        template <typename U, typename V>
46
        std::size_t operator()(const std::pair<U, V>& p) const {
18,323✔
47
                std::size_t seed = 0;
18,323✔
48
                hash_combine(seed, std::hash<U>{}(p.first));
18,323✔
49
                hash_combine(seed, std::hash<V>{}(p.second));
18,323✔
50
                return seed;
18,323✔
51
        }
52
};
53

54

55
/// See `utils::type_switch_t`.
56
template <typename Lookup, typename... KVs>
57
struct type_switch {};
58

59
/// `switch` equivalent of `std::conditional_t`. Use as `utils::type_switch_t<lookup-type, key-type-1(result-type-1), key-type-2(result-type-2), ...>`
60
template <typename Lookup, typename... KVs>
61
using type_switch_t = typename type_switch<Lookup, KVs...>::type;
62

63
template <typename MatchingKey, typename Value, typename... KVs>
64
struct type_switch<MatchingKey, MatchingKey(Value), KVs...> {
65
        using type = Value;
66
};
67

68
template <typename NonMatching, typename Key, typename Value, typename... KVs>
69
struct type_switch<NonMatching, Key(Value), KVs...> {
70
        using type = type_switch_t<NonMatching, KVs...>;
71
};
72

73

74
/// Fiddles out the base name of a (possibly templated) struct or class from a full (possibly mangled) type name.
75
/// The input parameter should be `typeid(Struct*)`, i.e. a _pointer_ to the desired struct type.
76
std::string get_simplified_type_name_from_pointer(const std::type_info& pointer_type_info);
77

78
/// Fiddles out the base name of a (possibly templated) struct or class from a full (possibly mangled) type name.
79
template <typename Struct>
80
std::string get_simplified_type_name() {
2,433✔
81
        // Using a pointer will also make this function work types that have no definitions, which commonly happens for kernel name type.
82
        return get_simplified_type_name_from_pointer(typeid(Struct*));
2,433✔
83
}
84

85
/// Escapes "<", ">", and "&" with their corresponding HTML escape sequences
86
std::string escape_for_dot_label(std::string str);
87

88
/// Print the buffer id as either 'B1' or 'B1 "name"' (if `name` is non-empty)
89
std::string make_buffer_debug_label(const buffer_id bid, const std::string& name = "");
90

91
std::string make_task_debug_label(const task_type tt, const task_id tid, const std::string& debug_name, bool title_case = false);
92

93

94
[[noreturn]] void unreachable();
95

96
enum class panic_solution {
97
        log_and_abort,     ///< default
98
        throw_logic_error, ///< enabled in unit tests to detect and recover from panics
99
};
100

101
/// Globally and atomically sets the behavior of `utils::panic()`.
102
void set_panic_solution(panic_solution solution);
103

104
/// Either throws or aborts with a message, depending on the global `panic_solution` setting.
105
[[noreturn]] void panic(const std::string& msg);
106

107
/// Either throws or aborts with a message, depending on the global `panic_solution` setting.
108
template <typename... FmtParams>
UNCOV
109
[[noreturn]] void panic(fmt::format_string<FmtParams...> fmt_string, FmtParams&&... fmt_args) {
×
110
        // TODO also receive a std::source_location with C++20.
UNCOV
111
        panic(fmt::format(fmt_string, std::forward<FmtParams>(fmt_args)...));
×
112
}
113

114
/// Ignores, logs, or panics on an error depending on the `error_policy`.
115
void report_error(const error_policy policy, const std::string& msg);
116

117
/// Ignores, logs, or panics on an error depending on the `error_policy`.
118
template <typename... FmtParams, std::enable_if_t<sizeof...(FmtParams) >= 1, int> = 0>
119
void report_error(const error_policy policy, const fmt::format_string<FmtParams...> fmt_string, FmtParams&&... fmt_args) {
34✔
120
        // TODO also receive a std::source_location with C++20.
121
        if(policy != error_policy::ignore) { report_error(policy, fmt::format(fmt_string, std::forward<FmtParams>(fmt_args)...)); }
53!
122
}
15✔
123

124

125
template <typename Container>
126
Container set_intersection(const Container& lhs, const Container& rhs) {
292✔
127
        assert(std::ranges::is_sorted(lhs));
292✔
128
        assert(std::ranges::is_sorted(rhs));
292✔
129
        Container intersection;
292✔
130
        std::ranges::set_intersection(lhs, rhs, std::back_inserter(intersection));
292✔
131
        return intersection;
292✔
UNCOV
132
}
×
133

134
template <typename Container, typename Key>
135
bool contains(const Container& container, const Key& key) {
1,319✔
136
        if constexpr(requires(Container c) { c.find(key); }) {
137
                return container.find(key) != std::end(container);
804✔
138
        } else {
139
                return std::ranges::find(container, key) != std::end(container);
1,834✔
140
        }
141
}
142

143
/// Replaces all occurrences of `pattern` in `in` with `with`. If `pattern` is empty, returns the input string unchanged.
144
std::string replace_all(const std::string_view& input, const std::string_view& pattern, const std::string_view& replacement);
145

146
template <std::integral Integral>
147
[[nodiscard]] constexpr Integral ceil(const Integral quantity, const Integral granularity) {
498✔
148
        return (quantity + granularity - 1) / granularity * granularity;
498✔
149
}
150

151
template <typename Void>
152
    requires(std::is_void_v<Void>)
153
[[nodiscard]] constexpr Void* offset(Void* const ptr, const size_t offset_bytes) {
2,023✔
154
        using byte_type = std::conditional_t<std::is_const_v<Void>, const std::byte, std::byte>;
155
        return static_cast<byte_type*>(ptr) + offset_bytes;
2,023✔
156
}
157

158
} // namespace celerity::detail::utils
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc