CatapultServer  v0.5.0.1 (Elephant)
StrandOwnerLifetimeExtender.h
Go to the documentation of this file.
1 
21 #pragma once
23 #include <boost/asio.hpp>
24 
25 namespace catapult { namespace thread {
26 
28  template<typename TOwner>
30  public:
32  explicit StrandOwnerLifetimeExtender(boost::asio::io_context::strand& strand) : m_strand(strand)
33  {}
34 
35  public:
37  template<typename THandler>
38  auto wrap(const std::shared_ptr<TOwner>& pOwner, THandler handler) {
39  // when wrap is called the returned callback needs to extend the lifetime of pOwner
40  utils::WrappedWithOwnerDecorator<THandler> wrappedHandler(pOwner, std::move(handler));
41  return m_strand.wrap(wrappedHandler);
42  }
43 
44  public:
46  template<typename THandler>
47  void post(const std::shared_ptr<TOwner>& pOwner, THandler handler) {
48  // ensure all handlers extend the lifetime of pOwner and post to a strand
49  boost::asio::post(m_strand, [pOwner, handler]() {
50  handler(pOwner);
51  });
52  }
53 
54  private:
55  boost::asio::io_context::strand& m_strand;
56  };
57 }}
WrappedWithOwnerDecorator.h
catapult::thread::StrandOwnerLifetimeExtender
Wraps a strand and automatically augments handlers to extend the lifetime of an owning object.
Definition: StrandOwnerLifetimeExtender.h:29
catapult::thread::StrandOwnerLifetimeExtender::post
void post(const std::shared_ptr< TOwner > &pOwner, THandler handler)
Attaches pOwner to handler and posts it to the strand.
Definition: StrandOwnerLifetimeExtender.h:47
catapult::thread::StrandOwnerLifetimeExtender::wrap
auto wrap(const std::shared_ptr< TOwner > &pOwner, THandler handler)
Wraps handler and attaches pOwner to it.
Definition: StrandOwnerLifetimeExtender.h:38
catapult::thread::StrandOwnerLifetimeExtender::StrandOwnerLifetimeExtender
StrandOwnerLifetimeExtender(boost::asio::io_context::strand &strand)
Creates a strand owner lifetime extender around strand.
Definition: StrandOwnerLifetimeExtender.h:32
catapult::thread::StrandOwnerLifetimeExtender::m_strand
boost::asio::io_context::strand & m_strand
Definition: StrandOwnerLifetimeExtender.h:55
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::WrappedWithOwnerDecorator
Definition: WrappedWithOwnerDecorator.h:29