tess 1.0.0
Performance-first tile and path simulation substrate
Loading...
Searching...
No Matches
capacity.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <limits>
6#include <utility>
7
8// TESS_INTERNAL_CAPACITY_TESTING is an internal test hook, not a supported
9// consumer switch. It changes the definition of the inline
10// `detail::effective_capacity_limit` below, so a program that defines it for
11// some translation units and not others violates the one-definition rule with
12// no diagnostic on any compiler. Define it for a whole binary or not at all.
13// The pragma below gives MSVC a link-time check; GCC and Clang have no
14// equivalent mechanism, so consistency there is the build system's job.
15#if defined(_MSC_VER)
16#if defined(TESS_INTERNAL_CAPACITY_TESTING)
17#pragma detect_mismatch("tess_capacity_testing", "enabled")
18#else
19#pragma detect_mismatch("tess_capacity_testing", "disabled")
20#endif
21#endif
22
23namespace tess {
24
26enum class ReserveStatus : std::uint8_t {
27 Reserved,
28 CapacityExceeded,
29};
30
31namespace detail {
32
33#if defined(TESS_INTERNAL_CAPACITY_TESTING)
34inline thread_local std::size_t capacity_limit_for_testing =
35 std::numeric_limits<std::size_t>::max();
36
37class ScopedCapacityLimitForTesting {
38 public:
39 explicit ScopedCapacityLimitForTesting(std::size_t limit) noexcept
40 : previous_(std::exchange(capacity_limit_for_testing, limit)) {}
41
42 ScopedCapacityLimitForTesting(const ScopedCapacityLimitForTesting&) = delete;
43 auto operator=(const ScopedCapacityLimitForTesting&)
44 -> ScopedCapacityLimitForTesting& = delete;
45
46 ~ScopedCapacityLimitForTesting() { capacity_limit_for_testing = previous_; }
47
48 private:
49 std::size_t previous_;
50};
51#endif
52
53[[nodiscard]] inline auto effective_capacity_limit(
54 std::size_t container_limit) noexcept -> std::size_t {
55#if defined(TESS_INTERNAL_CAPACITY_TESTING)
56 return container_limit < capacity_limit_for_testing
57 ? container_limit
58 : capacity_limit_for_testing;
59#else
60 return container_limit;
61#endif
62}
63
64} // namespace detail
65
66} // namespace tess