ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
pendingReplies.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <daq/pendingReplies.hpp>
3 
4 
5 namespace daq {
6 
7 ReplyToken::ReplyToken(uint64_t token, std::weak_ptr<PendingReplies> ptr) noexcept
8  : m_token(token), m_pending_replies(std::move(ptr)) {
9 }
10 
12  Release();
13 }
14 
15 void
17  if (auto pending = m_pending_replies.lock(); pending) {
18  pending->Release(m_token);
19  }
20 }
21 
22 std::shared_ptr<PendingReplies> PendingReplies::Create() {
23  return std::shared_ptr<PendingReplies>(new PendingReplies());
24 }
25 
27  return !m_pending.empty();
28 }
29 
30 std::vector<PendingReplies::PendingReply> PendingReplies::GetPendingReplies() const {
31  std::vector<PendingReply> r;
32  r.reserve(m_pending.size());
33  std::transform(m_pending.begin(), m_pending.end(), std::back_inserter(r),
34  [](auto const& tup) -> PendingReply { return std::get<1>(tup); });
35  return r;
36 }
37 
38 ReplyToken PendingReplies::Acquire(std::string source_id, std::string request) {
39  auto token = m_next_token_id++;
40  m_pending.emplace_back(std::tuple<uint64_t, PendingReply>(
41  {token, PendingReply{std::move(source_id), std::move(request)}}));
42  return ReplyToken(token, shared_from_this());
43 }
44 
45 std::shared_ptr<ReplyToken> PendingReplies::AcquireShared(std::string source_id,
46  std::string request) {
47  auto token = m_next_token_id++;
48  m_pending.emplace_back(std::tuple<uint64_t, PendingReply>(
49  {token, PendingReply{std::move(source_id), std::move(request)}}));
50  return std::shared_ptr<ReplyToken>(new ReplyToken(token, shared_from_this()));
51 }
52 
53 void PendingReplies::Release(uint64_t token) {
54  m_pending.erase(
55  std::remove_if(m_pending.begin(), m_pending.end(),
56  [=](auto const& tup) -> bool {
57  return std::get<0>(tup) == token; }),
58  m_pending.end());
59 }
60 
61 } // namespace daq
static std::shared_ptr< PendingReplies > Create()
Create instance.
void Release(uint64_t token)
ReplyToken Acquire(std::string source_id, std::string request)
Acquire token.
bool HasPendingReplies() const
std::shared_ptr< ReplyToken > AcquireShared(std::string source_id, std::string request)
PendingReplies()=default
std::vector< PendingReply > GetPendingReplies() const
Token representing a not-yet-received reply.
void Release()
Release token explicitly.
ReplyToken(ReplyToken &&)=default
Contains declaration for classes related to pending replies.
auto const & transform