Sen API
Sen Libraries
Loading...
Searching...
No Matches
object_ref.h
Go to the documentation of this file.
1// === object_ref.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_OBJECT_REF_H
9#define SEN_CORE_OBJ_OBJECT_REF_H
10
11// sen
14
15namespace sen
16{
17
20
26template <typename T>
28{
29public: // types
30 using Callback = std::function<void()>;
31
32public:
33 SEN_NOCOPY_NOMOVE(ObjectRef)
34
35public: // special members
36 ObjectRef() = default;
37 ~ObjectRef() override = default;
38
39public:
41 [[nodiscard]] bool valid() const noexcept { return ptr_.load() != nullptr; }
42
44 [[nodiscard]] operator bool() const noexcept { return valid(); } // NOLINT(hicpp-explicit-conversions)
45
47 [[nodiscard]] T* operator->() noexcept { return ptr_.load(); }
48
50 [[nodiscard]] const T* operator->() const noexcept { return ptr_.load(); }
51
53 [[nodiscard]] T& get() noexcept { return *ptr_.load(); }
54
56 [[nodiscard]] const T& get() const noexcept { return *ptr_.load(); }
57
60 Callback onAdded(Callback&& function) noexcept { return std::exchange(onAdded_, std::move(function)); }
61
64 Callback onRemoved(Callback&& function) noexcept { return std::exchange(onRemoved_, std::move(function)); }
65
66protected: // implements ObjectProviderListener
67 void onObjectsAdded(const ObjectAdditionList& additions) override;
68 void onObjectsRemoved(const ObjectRemovalList& removals) override;
69
70private:
71 [[nodiscard]] T* getCastedObject(Object* object);
72
73private:
74 std::atomic<T*> ptr_ = nullptr;
75 Callback onAdded_ = nullptr;
76 Callback onRemoved_ = nullptr;
77};
78
80
81//----------------------------------------------------------------------------------------------------------------------
82// Inline implementation
83//----------------------------------------------------------------------------------------------------------------------
84
85template <typename T>
86inline T* ObjectRef<T>::getCastedObject(Object* object)
87{
88 if constexpr (!std::is_same_v<T, Object>)
89 {
90 auto castedObject = dynamic_cast<T*>(object);
91 if (!castedObject)
92 {
93 throwRuntimeError("invalid cast");
94 }
95 return castedObject;
96 }
97 else
98 {
99 return object;
100 }
101}
102
103template <typename T>
105{
106 for (const auto& addition: additions)
107 {
108 // only react to discovered instances
109 auto* instance = getObjectInstance(addition);
110 if (instance == nullptr)
111 {
112 continue;
113 }
114
115 // that are of our type
116 if (auto* castedObject = getCastedObject(instance); castedObject)
117 {
118 ptr_.store(castedObject);
119
120 if (onAdded_)
121 {
122 onAdded_();
123 }
124 return;
125 }
126 }
127}
128
129template <typename T>
131{
132 if (ptr_.load() != nullptr)
133 {
134 const auto* instance = ptr_.load()->asObject();
135 for (const auto& removal: removals)
136 {
137 if (instance->getId() == removal.objectid)
138 {
139 ptr_.store(nullptr);
140
141 if (onRemoved_)
142 {
143 onRemoved_();
144 }
145 return;
146 }
147 }
148 }
149}
150
151} // namespace sen
152
153#endif // SEN_CORE_OBJ_OBJECT_REF_H
Base class for event or method callbacks. It stores the queue where to push the response....
Definition callback.h:146
A sen object.
Definition object.h:76
const T & get() const noexcept
Access the internal instance.
Definition object_ref.h:56
Callback onAdded(Callback &&function) noexcept
Installs a function to be called when objects are added / discovered. Replaces any previously-install...
Definition object_ref.h:60
void onObjectsAdded(const ObjectAdditionList &additions) override
Called when objects are been added to a source.
Definition object_ref.h:104
const T * operator->() const noexcept
Access the internal instance.
Definition object_ref.h:50
T & get() noexcept
Access the internal instance.
Definition object_ref.h:53
ObjectRef()=default
Callback onRemoved(Callback &&function) noexcept
Installs a function to be called when objects are added / discovered. Replaces any previously-install...
Definition object_ref.h:64
void onObjectsRemoved(const ObjectRemovalList &removals) override
Called when objects will be removed from a source.
Definition object_ref.h:130
bool valid() const noexcept
True if the reference holds a valid pointer to an instance.
Definition object_ref.h:41
~ObjectRef() override=default
std::function< void()> Callback
Definition object_ref.h:30
T * operator->() noexcept
Access the internal instance.
Definition object_ref.h:47
void throwRuntimeError(const std::string &err)
Throws std::exception that attempts to collect the stack trace. We also wrap it to avoid including st...
Object * getObjectInstance(const ObjectAddition &discovery)
Definition object_provider.h:97
std::vector< ObjectAddition > ObjectAdditionList
Sequence of object additions.
Definition object_provider.h:71
std::vector< ObjectRemoval > ObjectRemovalList
Sequence of object removals.
Definition object_provider.h:74
Definition assert.h:17