CatapultServer  v0.5.0.1 (Elephant)
WrappedWithOwnerDecorator.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include <memory>
23 
24 namespace catapult { namespace utils {
25 
28  template<typename THandler>
30  public:
32  template<typename TOwner>
33  WrappedWithOwnerDecorator(const std::shared_ptr<TOwner>& pOwner, THandler handler)
34  : m_pOwner(pOwner)
35  , m_handler(std::move(handler))
36  {}
37 
38  public:
40  template<typename... TArgs>
41  auto operator()(TArgs&& ...args) const {
42  return m_handler(std::forward<TArgs>(args)...);
43  }
44 
46  const std::shared_ptr<const void>& owner() const {
47  return m_pOwner;
48  }
49 
50  protected:
51  std::shared_ptr<const void> m_pOwner; // owner must be destroyed last in case handler holds raw reference(s) to it
52  THandler m_handler;
53  };
54 
57  template<typename THandler>
59  public:
61  template<typename TOwner>
62  ResettableWrappedWithOwnerDecorator(const std::shared_ptr<TOwner>& pOwner, THandler handler)
63  : WrappedWithOwnerDecorator<THandler>(pOwner, std::move(handler))
64  {}
65 
66  public:
68  void reset() {
69  using BaseType = WrappedWithOwnerDecorator<THandler>;
70  auto empty = decltype(BaseType::m_handler)();
71  BaseType::m_handler.swap(empty);
72  BaseType::m_pOwner.reset();
73  }
74  };
75 }}
m_handler
THandler m_handler
Definition: BufferedPacketIo.cpp:111
catapult::utils::ResettableWrappedWithOwnerDecorator::ResettableWrappedWithOwnerDecorator
ResettableWrappedWithOwnerDecorator(const std::shared_ptr< TOwner > &pOwner, THandler handler)
Wraps handler and keeps pOwner alive.
Definition: WrappedWithOwnerDecorator.h:62
catapult::utils::ResettableWrappedWithOwnerDecorator::reset
void reset()
Releases all resources associated with this object.
Definition: WrappedWithOwnerDecorator.h:68
catapult::utils::ResettableWrappedWithOwnerDecorator
Definition: WrappedWithOwnerDecorator.h:58
m_pOwner
std::shared_ptr< const void > m_pOwner
Definition: ParallelValidationPolicy.cpp:138
catapult::utils::WrappedWithOwnerDecorator::m_handler
THandler m_handler
Definition: WrappedWithOwnerDecorator.h:52
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::WrappedWithOwnerDecorator::operator()
auto operator()(TArgs &&...args) const
Forwards args to the wrapped handler.
Definition: WrappedWithOwnerDecorator.h:41
catapult::utils::WrappedWithOwnerDecorator
Definition: WrappedWithOwnerDecorator.h:29
catapult::utils::WrappedWithOwnerDecorator::owner
const std::shared_ptr< const void > & owner() const
A reference to the owning pointer.
Definition: WrappedWithOwnerDecorator.h:46
catapult::utils::WrappedWithOwnerDecorator::m_pOwner
std::shared_ptr< const void > m_pOwner
Definition: WrappedWithOwnerDecorator.h:51
catapult::utils::WrappedWithOwnerDecorator::WrappedWithOwnerDecorator
WrappedWithOwnerDecorator(const std::shared_ptr< TOwner > &pOwner, THandler handler)
Wraps handler and keeps pOwner alive.
Definition: WrappedWithOwnerDecorator.h:33