CppWAMP
C++11 client library for the WAMP protocol
subscription.hpp
Go to the documentation of this file.
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2022.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_SUBSCRIPTION_HPP
8 #define CPPWAMP_SUBSCRIPTION_HPP
9 
10 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
14 
15 #include <memory>
16 #include <string>
17 #include "api.hpp"
18 #include "erroror.hpp"
19 #include "wampdefs.hpp"
20 #include "./internal/passkey.hpp"
21 
22 namespace wamp
23 {
24 
25 // Forward declaration
26 namespace internal { class Subscriber; }
27 
28 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 class CPPWAMP_API Subscription
42 {
43 public:
45  Subscription();
46 
48  Subscription(const Subscription& other);
49 
51  Subscription(Subscription&& other) noexcept;
52 
54  explicit operator bool() const;
55 
57  SubscriptionId id() const;
58 
60  Subscription& operator=(const Subscription& other);
61 
63  Subscription& operator=(Subscription&& other) noexcept;
64 
66  void unsubscribe() const;
67 
68 private:
69  using SubscriberPtr = std::weak_ptr<internal::Subscriber>;
70  using SlotId = uint64_t;
71 
72  static constexpr SubscriptionId invalidId_ = -1;
73 
74  SubscriberPtr subscriber_;
75  SubscriptionId subId_ = invalidId_;
76  SlotId slotId_ = invalidId_;
77 
78 public:
79  // Internal use only
80  Subscription(SubscriberPtr subscriber, SubscriptionId subId, SlotId slotId,
81  internal::PassKey);
82  SlotId slotId(internal::PassKey) const;
83 };
84 
85 
86 //------------------------------------------------------------------------------
91 //------------------------------------------------------------------------------
92 class CPPWAMP_API ScopedSubscription : public Subscription
93 {
94 // This class is modeled after boost::signals2::scoped_connection.
95 public:
98 
100  ScopedSubscription(ScopedSubscription&& other) noexcept;
101 
103  ScopedSubscription(Subscription subscription);
104 
107 
109  ScopedSubscription& operator=(ScopedSubscription&& other) noexcept;
110 
113  ScopedSubscription& operator=(Subscription subscription);
114 
117  void release();
118 
120  ScopedSubscription(const ScopedSubscription&) = delete;
121 
123  ScopedSubscription& operator=(const ScopedSubscription&) = delete;
124 
125 private:
126  using Base = Subscription;
127 };
128 
129 
130 } // namespace wamp
131 
132 #ifndef CPPWAMP_COMPILED_LIB
133 #include "internal/subscription.ipp"
134 #endif
135 
136 #endif // CPPWAMP_SUBSCRIPTION_HPP
wamp::ScopedSubscription
Limits a Subscription's lifetime to a particular scope.
Definition: subscription.hpp:92
erroror.hpp
Contains the ErrorOr template class.
api.hpp
Defines macros related to exporting/importing APIs.
wamp::SubscriptionId
int64_t SubscriptionId
Ephemeral ID associated with an topic subscription.
Definition: wampdefs.hpp:24
wamp
Definition: anyhandler.hpp:36
wamp::Subscription
Represents a pub/sub event subscription.
Definition: subscription.hpp:41
wampdefs.hpp
Contains type definitions related to WAMP IDs and sessions.