Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #pragma once
18 : :
19 : : #include <cassert>
20 : : #include <cstdint>
21 : :
22 : : namespace wirestead::wrapper {
23 : :
24 : : // Immediate send rejection only. Loss after acceptance belongs in statistics.
25 : : // The enum may grow; consumers should provide a default case when switching.
26 : : enum class SendRejection : std::uint8_t {
27 : : NotStarted,
28 : : Stopping,
29 : : NotReady,
30 : : WouldBlock,
31 : : QueueFull,
32 : : TooLarge,
33 : : InvalidArgument,
34 : : CancelledWhileWaiting
35 : : };
36 : :
37 : : // An admission outcome, never a delivery receipt. Construct an explicit outcome
38 : : // through accept()/reject(); there is no default or implicit bool constructor.
39 : : class [[nodiscard]] SendResult {
40 : : public:
41 : 857228 : static constexpr SendResult accept() noexcept { return SendResult(true, SendRejection::NotStarted); }
42 : 17948 : static constexpr SendResult reject(SendRejection reason) noexcept { return SendResult(false, reason); }
43 : :
44 : 888355 : constexpr bool accepted() const noexcept { return accepted_; }
45 : 7594 : constexpr explicit operator bool() const noexcept { return accepted(); }
46 : :
47 : : // Precondition: !accepted(). The stored reason has no meaning on success.
48 : 5267 : constexpr SendRejection reason() const noexcept {
49 [ - + ]: 5267 : assert(!accepted_ && "SendResult::reason() requires a rejected result");
50 : 5267 : return reason_;
51 : : }
52 : :
53 : : private:
54 : 875176 : constexpr SendResult(bool accepted, SendRejection reason) noexcept : accepted_(accepted), reason_(reason) {}
55 : : bool accepted_;
56 : : SendRejection reason_;
57 : : };
58 : :
59 : : } // namespace wirestead::wrapper
|