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

YACLib / YACLib / 27313595153

10 Jun 2026 11:41PM UTC coverage: 99.439% (+0.005%) from 99.434%
27313595153

Pull #277

github

web-flow
Merge 18e1cf397 into ca2c3f1c2
Pull Request #277: Shared core callbacks own their references

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

1 existing line in 1 file now uncovered.

2126 of 2138 relevant lines covered (99.44%)

1796277.33 hits per line

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

98.11
/include/yaclib/async/shared_future.hpp
1
#pragma once
2

3
#include <yaclib/algo/detail/core.hpp>
4
#include <yaclib/algo/detail/shared_core.hpp>
5
#include <yaclib/async/wait.hpp>
6
#include <yaclib/exe/executor.hpp>
7
#include <yaclib/fwd.hpp>
8
#include <yaclib/util/helper.hpp>
9
#include <yaclib/util/type_traits.hpp>
10

11
namespace yaclib {
12

13
template <typename V, typename T>
14
class SharedFutureBase {
15
  using CoreType = detail::CoreType;
16

17
 public:
18
  static_assert(Check<V>(), "V should be valid");
19
  static_assert(!std::is_same_v<V, typename T::Error>,
20
                "V cannot be the same as the trait Error type, because callback dispatch would be ambiguous");
21
  static_assert(std::is_copy_constructible_v<wrap_void_t<V>>, "Result should be copyable");
22

23
  using Result = typename T::template Result<V>;
24

25
  SharedFutureBase() = default;
177✔
26

27
  [[nodiscard]] bool Valid() const noexcept {
15✔
28
    return _core != nullptr;
15✔
29
  }
30

31
  [[nodiscard]] bool Ready() const noexcept {
54✔
32
    YACLIB_ASSERT(Valid());
33
    return _core->Ready();
54✔
34
  }
35

36
  [[nodiscard]] Result Get() && noexcept {
3,129✔
37
    YACLIB_ASSERT(Valid());
38
    Wait(*this);
3,129✔
39
    if (_core->GetRef() == 1) {
3,129✔
40
      return std::move(_core->Get());
1,828✔
41
    } else {
42
      return _core->Get();
1,301✔
43
    }
44
  }
45

46
  void Get() const&& = delete;
47

48
  [[nodiscard]] const Result& Get() const& noexcept {
168✔
49
    YACLIB_ASSERT(Valid());
50
    Wait(*this);
168✔
51
    return _core->Get();
168✔
52
  }
53

54
  [[nodiscard]] Result Touch() && noexcept {
36✔
55
    YACLIB_ASSERT(Valid());
56
    YACLIB_ASSERT(Ready());
57
    if (_core->GetRef() == 1) {
36✔
58
      return std::move(_core->Get());
36✔
59
    } else {
UNCOV
60
      return _core->Get();
×
61
    }
62
  }
63

64
  void Touch() const&& = delete;
65

66
  [[nodiscard]] const Result& Touch() const& noexcept {
89✔
67
    YACLIB_ASSERT(Valid());
68
    YACLIB_ASSERT(Ready());
69
    return _core->Get();
89✔
70
  }
71

72
  template <typename Func>
73
  [[nodiscard]] /*FutureOn*/ auto Then(IExecutor& e, Func&& f) const {
18✔
74
    YACLIB_WARN(e.Tag() == IExecutor::Type::Inline,
75
                "better way is use ThenInline(...) instead of Then(MakeInline(), ...)");
76
    static constexpr auto CoreT = CoreType::ToUnique | CoreType::Call;
77
    return detail::SetCallback<CoreT, true>(_core, &e, std::forward<Func>(f));
18✔
78
  }
79

80
  void Detach() && noexcept {
3,015✔
81
    _core = nullptr;
3,015✔
82
  }
3,015✔
83

84
  template <typename Func>
85
  void SubscribeInline(Func&& f) const {
6✔
86
    static constexpr auto CoreT = CoreType::Detach;
87
    detail::SetCallback<CoreT, false>(_core, nullptr, std::forward<Func>(f));
6✔
88
  }
6✔
89

90
  template <typename Func>
91
  void Subscribe(IExecutor& e, Func&& f) const {
3✔
92
    YACLIB_WARN(e.Tag() == IExecutor::Type::Inline,
93
                "better way is use SubscribeInline(...) instead of Subscribe(MakeInline(), ...)");
94
    static constexpr auto CoreT = CoreType::Detach | CoreType::Call;
95
    detail::SetCallback<CoreT, true>(_core, &e, std::forward<Func>(f));
3✔
96
  }
3✔
97

98
  [[nodiscard]] detail::SharedCorePtr<V, T>& GetCore() noexcept {
777✔
99
    return _core;
777✔
100
  }
101

102
  [[nodiscard]] const detail::SharedCorePtr<V, T>& GetCore() const noexcept {
522✔
103
    return _core;
522✔
104
  }
105

106
  using Handle = detail::SharedHandle;
107
  using Core = detail::SharedCore<V, T>;
108

109
  [[nodiscard]] detail::SharedHandle GetHandle() const noexcept {
3,600✔
110
    return detail::SharedHandle{*_core};
3,600✔
111
  }
112

113
 protected:
114
  explicit SharedFutureBase(detail::SharedCorePtr<V, T> core) noexcept : _core{std::move(core)} {
4,845✔
115
  }
4,845✔
116

117
  detail::SharedCorePtr<V, T> _core;
118
};
119

120
extern template class SharedFutureBase<void, DefaultTrait>;
121

122
template <typename V, typename T>
123
class SharedFuture final : public SharedFutureBase<V, T> {
124
  using CoreType = detail::CoreType;
125
  using Base = SharedFutureBase<V, T>;
126

127
 public:
128
  using Base::Base;
129

130
  SharedFuture(detail::SharedCorePtr<V, T> core) noexcept : Base{std::move(core)} {
4,053✔
131
  }
4,053✔
132

133
  template <typename Func>
134
  [[nodiscard]] /*Future*/ auto ThenInline(Func&& f) const {
18✔
135
    static constexpr auto CoreT = CoreType::ToUnique;
136
    return detail::SetCallback<CoreT, false>(this->_core, nullptr, std::forward<Func>(f));
18✔
137
  }
138
};
139

140
extern template class SharedFuture<>;
141

142
template <typename V, typename T>
143
class SharedFutureOn final : public SharedFutureBase<V, T> {
144
  using CoreType = detail::CoreType;
145
  using Base = SharedFutureBase<V, T>;
146

147
 public:
148
  using Base::Base;
149
  using Base::Detach;
150
  using Base::Then;
151

152
  SharedFutureOn(detail::SharedCorePtr<V, T> core) noexcept : Base{std::move(core)} {
792✔
153
  }
792✔
154

155
  [[nodiscard]] SharedFuture<V, T> On(std::nullptr_t) && noexcept {
348✔
156
    return {std::move(this->_core)};
348✔
157
  }
158

159
  template <typename Func>
160
  [[nodiscard]] /*FutureOn*/ auto ThenInline(Func&& f) const {
12✔
161
    static constexpr auto CoreT = CoreType::ToUnique;
162
    return detail::SetCallback<CoreT, true>(this->_core, nullptr, std::forward<Func>(f));
12✔
163
  }
164

165
  template <typename Func>
166
  [[nodiscard]] /*FutureOn*/ auto Then(Func&& f) const {
54✔
167
    static constexpr auto CoreT = CoreType::ToUnique | CoreType::Call;
168
    return detail::SetCallback<CoreT, true>(this->_core, nullptr, std::forward<Func>(f));
54✔
169
  }
170

171
  template <typename Func>
172
  void Subscribe(Func&& f) const {
3✔
173
    static constexpr auto CoreT = CoreType::Detach | CoreType::Call;
174
    detail::SetCallback<CoreT, false>(this->_core, nullptr, std::forward<Func>(f));
3✔
175
  }
3✔
176
};
177

178
extern template class SharedFutureOn<>;
179

180
}  // namespace yaclib
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