• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

celerity / celerity-runtime / 15258506201

26 May 2025 04:31PM UTC coverage: 95.061% (+0.01%) from 95.05%
15258506201

Pull #323

github

web-flow
Merge aa6c8b8ce into 09f5469c8
Pull Request #323: Change split functions to work on box instead of chunk

3247 of 3682 branches covered (88.19%)

Branch coverage included in aggregate %.

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

19 existing lines in 7 files now uncovered.

7146 of 7251 relevant lines covered (98.55%)

1878177.37 hits per line

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

86.11
/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
template <typename T, typename P>
25
bool isa(const P* p) {
81,254✔
26
        return dynamic_cast<const T*>(p) != nullptr;
81,254!
27
}
28

29
template <typename T, typename P>
30
auto as(P* p) {
14,718✔
31
        assert(isa<T>(p));
14,718✔
32
        return static_cast<std::conditional_t<std::is_const_v<P>, const T*, T*>>(p);
14,718✔
33
}
34

35
// Implementation from Boost.ContainerHash, licensed under the Boost Software License, Version 1.0.
36
inline void hash_combine(std::size_t& seed, std::size_t value) { seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2); }
75,236,562✔
37

38
struct pair_hash {
39
        template <typename U, typename V>
40
        std::size_t operator()(const std::pair<U, V>& p) const {
19,110✔
41
                std::size_t seed = 0;
19,110✔
42
                hash_combine(seed, std::hash<U>{}(p.first));
19,110✔
43
                hash_combine(seed, std::hash<V>{}(p.second));
19,110✔
44
                return seed;
19,110✔
45
        }
46
};
47

48

49
/// See `utils::type_switch_t`.
50
template <typename Lookup, typename... KVs>
51
struct type_switch {};
52

53
/// `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), ...>`
54
template <typename Lookup, typename... KVs>
55
using type_switch_t = typename type_switch<Lookup, KVs...>::type;
56

57
template <typename MatchingKey, typename Value, typename... KVs>
58
struct type_switch<MatchingKey, MatchingKey(Value), KVs...> {
59
        using type = Value;
60
};
61

62
template <typename NonMatching, typename Key, typename Value, typename... KVs>
63
struct type_switch<NonMatching, Key(Value), KVs...> {
64
        using type = type_switch_t<NonMatching, KVs...>;
65
};
66

67

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

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

79
/// Escapes "<", ">", and "&" with their corresponding HTML escape sequences
80
std::string escape_for_dot_label(std::string str);
81

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

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

87

88
[[noreturn]] void unreachable();
89

90
enum class panic_solution {
91
        log_and_abort,     ///< default
92
        throw_logic_error, ///< enabled in unit tests to detect and recover from panics
93
};
94

95
/// Globally and atomically sets the behavior of `utils::panic()`.
96
void set_panic_solution(panic_solution solution);
97

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

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

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

111
/// Ignores, logs, or panics on an error depending on the `error_policy`.
112
template <typename... FmtParams, std::enable_if_t<sizeof...(FmtParams) >= 1, int> = 0>
113
void report_error(const error_policy policy, const fmt::format_string<FmtParams...> fmt_string, FmtParams&&... fmt_args) {
36✔
114
        // TODO also receive a std::source_location with C++20.
115
        if(policy != error_policy::ignore) { report_error(policy, fmt::format(fmt_string, std::forward<FmtParams>(fmt_args)...)); }
55!
116
}
17✔
117

118

119
template <typename Container>
120
Container set_intersection(const Container& lhs, const Container& rhs) {
304✔
121
        assert(std::ranges::is_sorted(lhs));
304✔
122
        assert(std::ranges::is_sorted(rhs));
304✔
123
        Container intersection;
304✔
124
        std::ranges::set_intersection(lhs, rhs, std::back_inserter(intersection));
304✔
125
        return intersection;
304✔
UNCOV
126
}
×
127

128
template <typename Container, typename Key>
129
bool contains(const Container& container, const Key& key) {
1,346✔
130
        if constexpr(requires(Container c) { c.find(key); }) {
131
                return container.find(key) != std::end(container);
818✔
132
        } else {
133
                return std::ranges::find(container, key) != std::end(container);
1,874✔
134
        }
135
}
136

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

140
template <std::integral Integral>
141
[[nodiscard]] constexpr Integral ceil(const Integral quantity, const Integral granularity) {
498✔
142
        return (quantity + granularity - 1) / granularity * granularity;
498✔
143
}
144

145
template <typename Void>
146
    requires(std::is_void_v<Void>)
147
[[nodiscard]] constexpr Void* offset(Void* const ptr, const size_t offset_bytes) {
2,023✔
148
        using byte_type = std::conditional_t<std::is_const_v<Void>, const std::byte, std::byte>;
149
        return static_cast<byte_type*>(ptr) + offset_bytes;
2,023✔
150
}
151

152
} // 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