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

celerity / celerity-runtime / 10325124557

09 Aug 2024 08:11PM UTC coverage: 95.087% (+0.02%) from 95.07%
10325124557

push

github

fknorr
Update benchmark results for Tracy integration

3014 of 3414 branches covered (88.28%)

Branch coverage included in aggregate %.

6624 of 6722 relevant lines covered (98.54%)

1474033.76 hits per line

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

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

3
#include <algorithm>
4
#include <cassert>
5
#include <cstdint>
6
#include <functional>
7
#include <string>
8
#include <string_view>
9
#include <tuple>
10
#include <type_traits>
11
#include <typeinfo>
12

13
#include <fmt/format.h>
14

15
#include "types.h"
16

17

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

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) {
743✔
27
        return std::move(from);
743✔
28
}
29

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

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

41
template <typename BitMaskT>
42
constexpr inline uint32_t popcount(const BitMaskT bit_mask) noexcept {
43
        static_assert(std::is_integral_v<BitMaskT> && std::is_unsigned_v<BitMaskT>, "popcount argument needs to be an unsigned integer type.");
44

45
        uint32_t counter = 0;
46
        for(auto b = bit_mask; b; b >>= 1) {
47
                counter += b & 1;
48
        }
49
        return counter;
50
}
51

52
// Implementation from Boost.ContainerHash, licensed under the Boost Software License, Version 1.0.
53
inline void hash_combine(std::size_t& seed, std::size_t value) { seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2); }
51,290✔
54

55
struct pair_hash {
56
        template <typename U, typename V>
57
        std::size_t operator()(const std::pair<U, V>& p) const {
10,038✔
58
                std::size_t seed = 0;
10,038✔
59
                hash_combine(seed, std::hash<U>{}(p.first));
10,038✔
60
                hash_combine(seed, std::hash<V>{}(p.second));
10,038✔
61
                return seed;
10,038✔
62
        }
63
};
64

65
} // namespace celerity::detail::utils
66

67
namespace celerity::detail::utils_detail {
68

69
template <typename... Without, typename... ToKeep, typename T, typename... Ts>
70
constexpr auto tuple_without_impl(const std::tuple<ToKeep...>& to_keep, const std::tuple<T, Ts...>& to_check) {
14✔
71
        if constexpr((std::is_same_v<T, Without> || ...)) {
72
                if constexpr(sizeof...(Ts) == 0) {
73
                        return to_keep;
6✔
74
                } else {
75
                        return tuple_without_impl<Without...>(to_keep, std::tuple{std::get<Ts>(to_check)...});
76
                }
77
        } else {
78
                if constexpr(sizeof...(Ts) == 0) {
79
                        return std::tuple_cat(to_keep, to_check);
5✔
80
                } else {
81
                        return tuple_without_impl<Without...>(std::tuple_cat(to_keep, std::tuple{std::get<T>(to_check)}), std::tuple{std::get<Ts>(to_check)...});
3✔
82
                }
83
        }
84
}
85

86
template <typename Container, typename Key, typename Enable = void>
87
struct has_member_find : std::false_type {};
88

89
template <typename Container, typename Key>
90
struct has_member_find<Container, Key, std::void_t<decltype(std::declval<const Container&>().find(std::declval<const Key&>()))>> : std::true_type {};
91

92
template <typename Container, typename Key>
93
inline constexpr bool has_member_find_v = has_member_find<Container, Key>::value;
94

95
} // namespace celerity::detail::utils_detail
96

97
namespace celerity::detail::utils {
98

99
/// See `utils::type_switch_t`.
100
template <typename Lookup, typename... KVs>
101
struct type_switch {};
102

103
/// `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), ...>`
104
template <typename Lookup, typename... KVs>
105
using type_switch_t = typename type_switch<Lookup, KVs...>::type;
106

107
template <typename MatchingKey, typename Value, typename... KVs>
108
struct type_switch<MatchingKey, MatchingKey(Value), KVs...> {
109
        using type = Value;
110
};
111

112
template <typename NonMatching, typename Key, typename Value, typename... KVs>
113
struct type_switch<NonMatching, Key(Value), KVs...> {
114
        using type = type_switch_t<NonMatching, KVs...>;
115
};
116

117
template <typename... Without, typename... Ts>
118
constexpr auto tuple_without(const std::tuple<Ts...>& tuple) {
18✔
119
        if constexpr(sizeof...(Ts) > 0) {
120
                return utils_detail::tuple_without_impl<Without...>({}, tuple);
11✔
121
        } else {
122
                return tuple;
7✔
123
        }
124
}
125

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

130
/// Fiddles out the base name of a (possibly templated) struct or class from a full (possibly mangled) type name.
131
template <typename Struct>
132
std::string get_simplified_type_name() {
1,697✔
133
        // Using a pointer will also make this function work types that have no definitions, which commonly happens for kernel name type.
134
        return get_simplified_type_name_from_pointer(typeid(Struct*));
1,697✔
135
}
136

137
/// Escapes "<", ">", and "&" with their corresponding HTML escape sequences
138
std::string escape_for_dot_label(std::string str);
139

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

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

145
[[noreturn]] void unreachable();
146

147
enum class panic_solution {
148
        log_and_abort,     ///< default
149
        throw_logic_error, ///< enabled in unit tests to detect and recover from panics
150
};
151

152
/// Globally and atomically sets the behavior of `utils::panic()`.
153
void set_panic_solution(panic_solution solution);
154

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

158
/// Either throws or aborts with a message, depending on the global `panic_solution` setting.
159
template <typename... FmtParams>
160
[[noreturn]] void panic(fmt::format_string<FmtParams...> fmt_string, FmtParams&&... fmt_args) {
×
161
        // TODO also receive a std::source_location with C++20.
162
        panic(fmt::format(fmt_string, std::forward<FmtParams>(fmt_args)...));
×
163
}
164

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

168
/// Ignores, logs, or panics on an error depending on the `error_policy`.
169
template <typename... FmtParams, std::enable_if_t<sizeof...(FmtParams) >= 1, int> = 0>
170
void report_error(const error_policy policy, const fmt::format_string<FmtParams...> fmt_string, FmtParams&&... fmt_args) {
32✔
171
        // TODO also receive a std::source_location with C++20.
172
        if(policy != error_policy::ignore) { report_error(policy, fmt::format(fmt_string, std::forward<FmtParams>(fmt_args)...)); }
81!
173
}
15✔
174

175
template <typename Container>
176
Container set_intersection(const Container& lhs, const Container& rhs) {
280✔
177
        using std::begin, std::end;
178
        assert(std::is_sorted(begin(lhs), end(lhs)));
280✔
179
        assert(std::is_sorted(begin(rhs), end(rhs)));
280✔
180
        Container intersection;
280✔
181
        std::set_intersection(begin(lhs), end(lhs), begin(rhs), end(rhs), std::back_inserter(intersection));
560✔
182
        return intersection;
280✔
183
}
×
184

185
template <typename Container, typename Key>
186
bool contains(const Container& container, const Key& key) {
1,284✔
187
        using std::begin, std::end;
188
        if constexpr(utils_detail::has_member_find_v<Container, Key>) {
189
                return container.find(key) != end(container);
790✔
190
        } else {
191
                return std::find(begin(container), end(container), key) != end(container);
2,667✔
192
        }
193
}
194

195
template <typename Container, typename Predicate>
196
void erase_if(Container& container, const Predicate& predicate) {
3,954,623✔
197
        using std::begin, std::end;
198
        container.erase(std::remove_if(begin(container), end(container), predicate), end(container));
11,863,869✔
199
}
3,954,623✔
200

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

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