ifw-daq  2.1.0-pre1
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
pendingReplies.hpp
Contains declaration for classes related to pending replies.
daq::PendingReplies::Release
void Release(uint64_t token)
Definition: pendingReplies.cpp:53
daq::PendingReplies::GetPendingReplies
std::vector< PendingReply > GetPendingReplies() const
Definition: pendingReplies.cpp:30
daq::PendingReplies::HasPendingReplies
bool HasPendingReplies() const
Definition: pendingReplies.cpp:26
daq::PendingReplies::AcquireShared
std::shared_ptr< ReplyToken > AcquireShared(std::string source_id, std::string request)
Definition: pendingReplies.cpp:45
daq::PendingReplies::ReplyToken
friend ReplyToken
Definition: pendingReplies.hpp:82
daq::ReplyToken
Token representing a not-yet-received reply.
Definition: pendingReplies.hpp:26
daq
Definition: asyncProcess.cpp:15
daq::PendingReplies::PendingReplies
PendingReplies()=default
daq::PendingReplies::Create
static std::shared_ptr< PendingReplies > Create()
Create instance.
Definition: pendingReplies.cpp:22
daq::ReplyToken::ReplyToken
ReplyToken(ReplyToken &&)=default
daq::ReplyToken::~ReplyToken
~ReplyToken()
Definition: pendingReplies.cpp:11
daq::PendingReplies::Acquire
ReplyToken Acquire(std::string source_id, std::string request)
Acquire token.
Definition: pendingReplies.cpp:38
daq::ReplyToken::Release
void Release()
Release token explicitly.
Definition: pendingReplies.cpp:16
daq::PendingReplies::PendingReply
Definition: pendingReplies.hpp:60