Sen API
Sen Libraries
Loading...
Searching...
No Matches
subscription.h
Go to the documentation of this file.
1// === subscription.h ==================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_OBJ_SUBSCRIPTION_H
9#define SEN_CORE_OBJ_SUBSCRIPTION_H
10
11// sen
15
16// std
17#include <memory>
18#include <utility>
19
20namespace sen
21{
22
25
27template <typename T>
29{
30public:
31 SEN_COPY_CONSTRUCT(Subscription) = delete;
32 SEN_COPY_ASSIGN(Subscription) = delete;
33
34public: // special members
35 Subscription(Subscription&& other) noexcept;
37 Subscription() = default;
39
40public:
43 void attachTo(std::shared_ptr<ObjectSource> src, std::shared_ptr<Interest> interest, bool notifyAboutExisting);
44
48 void release(bool notifyAboutExisting = true);
49
51 [[nodiscard]] std::shared_ptr<ObjectSource> getSource() const { return source_; }
52
53public:
54 ObjectList<T> list; // NOLINT(misc-non-private-member-variables-in-classes)
55
56private:
57 std::shared_ptr<ObjectSource> source_;
58};
59
60//-------------------------------------------------------------------------------------------------------------------
61// Inline implementation
62//-------------------------------------------------------------------------------------------------------------------
63
64template <typename T>
66 : list(std::move(other.list)), source_(std::move(other.source_))
67{
68}
69
70template <typename T>
72{
73 if (this != &other)
74 {
75 release(true);
76 list = std::move(other.list);
77 source_ = std::move(other.source_);
78 }
79
80 return *this;
81}
82
83template <typename T>
85{
86 release(true);
87}
88
89template <typename T>
90inline void Subscription<T>::attachTo(std::shared_ptr<ObjectSource> src,
91 std::shared_ptr<Interest> interest,
92 bool notifyAboutExisting)
93{
94 release();
95
96 source_ = std::move(src);
97 source_->addSubscriber(std::move(interest), &list, notifyAboutExisting);
98}
99
100template <typename T>
101inline void Subscription<T>::release(bool notifyAboutExisting)
102{
103 if (source_)
104 {
105 source_->removeSubscriber(&list, notifyAboutExisting);
106 source_.reset();
107 }
108}
109
111
112} // namespace sen
113
114#endif // SEN_CORE_OBJ_SUBSCRIPTION_H
A list of objects that is managed by some source based on user-expressed interests.
Definition object_list.h:30
void attachTo(std::shared_ptr< ObjectSource > src, std::shared_ptr< Interest > interest, bool notifyAboutExisting)
Attaches this subscription to a source and registers the given interest. Objects matching the interes...
Definition subscription.h:90
Subscription & operator=(Subscription &&other) noexcept
Definition subscription.h:71
void release(bool notifyAboutExisting=true)
Detaches from the source, unregistering the list as a subscriber. Called automatically on destruction...
Definition subscription.h:101
~Subscription()
Definition subscription.h:84
Subscription(Subscription &&other) noexcept
Definition subscription.h:65
Definition assert.h:17
Subscription()=default
ObjectList< T > list
Definition subscription.h:54
std::shared_ptr< ObjectSource > getSource() const
Returns the source currently attached to this subscription, or nullptr if none.
Definition subscription.h:51