8 #ifndef META_OCEAN_BASE_SCOPED_SUBSCRIPTION_H
9 #define META_OCEAN_BASE_SCOPED_SUBSCRIPTION_H
26 template <
typename T,
typename TOwner>
72 explicit inline operator bool()
const;
165 template <
typename TCallbackFunction,
typename TOwner,
bool tThreadSafe>
185 using CallbackMap = std::unordered_map<unsigned int, TCallbackFunction>;
216 template <
class... TArgs>
237 template <
typename T,
typename TOwner>
240 *
this = std::move(scopedSubscription);
243 template <
typename T,
typename TOwner>
245 subscriptionId_(std::make_unique<T>(subscriptionId)),
246 releaseCallbackFunction_(std::move(releaseCallbackFunction))
251 template <
typename T,
typename TOwner>
257 template <
typename T,
typename TOwner>
262 ocean_assert(releaseCallbackFunction_);
263 releaseCallbackFunction_(*subscriptionId_);
265 releaseCallbackFunction_ =
nullptr;
266 subscriptionId_ =
nullptr;
270 template <
typename T,
typename TOwner>
273 return subscriptionId_ !=
nullptr;
276 template <
typename T,
typename TOwner>
282 template <
typename T,
typename TOwner>
285 if (
this != &scopedSubscription)
289 subscriptionId_ = std::move(scopedSubscription.subscriptionId_);
290 releaseCallbackFunction_ = std::move(scopedSubscription.releaseCallbackFunction_);
296 template <
typename T,
typename TOwner>
302 template <
typename T,
typename TOwner>
305 return !(*
this == scopedSubscription);
308 template <
typename T,
typename TOwner>
311 return std::hash<std::unique_ptr<T>>()(scopedSubscription.
subscriptionId_);
316 ocean_assert(releaseCallbackFunction);
320 template <
typename TCallbackFunction,
typename TOwner,
bool tThreadSafe>
325 const unsigned int subscriptionId = ++subscriptionIdCounter_;
327 ocean_assert(callbackMap_.find(subscriptionId) == callbackMap_.cend());
328 callbackMap_.emplace(subscriptionId, std::move(callbackFunction));
333 template <
typename TCallbackFunction,
typename TOwner,
bool tThreadSafe>
338 return callbackMap_.size();
341 template <
typename TCallbackFunction,
typename TOwner,
bool tThreadSafe>
346 return callbackMap_.empty();
349 template <
typename TCallbackFunction,
typename TOwner,
bool tThreadSafe>
350 template <
class... TArgs>
355 for (
typename CallbackMap::const_iterator iCallback = callbackMap_.cbegin(); iCallback != callbackMap_.cend(); ++iCallback)
357 const TCallbackFunction& callbackFunction = iCallback->second;
359 typename CallbackMap::const_iterator iNextCallback(iCallback);
361 if (++iNextCallback == callbackMap_.cend())
363 return callbackFunction(std::forward<TArgs>(args)...);
367 callbackFunction(std::forward<TArgs>(args)...);
372 template <
typename TCallbackFunction,
typename TOwner,
bool tThreadSafe>
377 ocean_assert(callbackMap_.find(subscriptionId) != callbackMap_.cend());
379 callbackMap_.erase(subscriptionId);
This class implements a handler for scoped subscriptions to callback functions.
Definition: ScopedSubscription.h:167
static constexpr bool isThreadSafe_
True, if this handler is thread-safe.
Definition: ScopedSubscription.h:178
friend TOwner
Definition: ScopedSubscription.h:168
unsigned int subscriptionIdCounter_
The counter for subscription ids.
Definition: ScopedSubscription.h:231
TCallbackFunction CallbackFunctionType
Definition of the data type of the callback function.
Definition: ScopedSubscription.h:175
void removeCallback(const unsigned int &subscriptionId)
Removes a callback function from the handler.
Definition: ScopedSubscription.h:373
ScopedSubscriptionType addCallback(TCallbackFunction callbackFunction)
Adds a new callback function to this handler.
Definition: ScopedSubscription.h:321
std::unordered_map< unsigned int, TCallbackFunction > CallbackMap
Definition of an unordered map mapping subscription ids to callback functions.
Definition: ScopedSubscription.h:185
size_t subscriptions() const
Returns the number of subscriptions.
Definition: ScopedSubscription.h:334
CallbackMap callbackMap_
The map mapping subscription ids to callback functions.
Definition: ScopedSubscription.h:228
TemplatedLock< tThreadSafe > lock_
The optional lock object.
Definition: ScopedSubscription.h:234
TCallbackFunction::result_type callCallbacks(TArgs &&... args)
Calls all callback functions of this handler.
Definition: ScopedSubscription.h:351
bool isEmpty() const
Returns whether no subscription exists.
Definition: ScopedSubscription.h:342
This class implements the base class for all subscription handlers.
Definition: ScopedSubscription.h:138
ScopedSubscriptionT< unsigned int, ScopedSubscriptionHandler > ScopedSubscriptionType
Definition of a scoped subscription object.
Definition: ScopedSubscription.h:144
ScopedSubscriptionType scopedSubscription(const unsigned int &subscriptionId, ScopedSubscriptionType::ReleaseCallbackFunction releaseCallbackFunction)
Returns the subscription object for a given subscription id.
Definition: ScopedSubscription.h:314
This class implements a subscription object which can be used unique subscriptions to e....
Definition: ScopedSubscription.h:28
bool operator==(const ScopedSubscriptionT< T, TOwner > &scopedSubscription) const
Returns whether two subscription objects are identical.
Definition: ScopedSubscription.h:297
friend TOwner
Definition: ScopedSubscription.h:29
ScopedSubscriptionT< T, TOwner > & operator=(const ScopedSubscriptionT< T, TOwner > &scopedSubscription)=delete
The disabled assign operator.
ScopedSubscriptionT(ScopedSubscriptionT< T, TOwner > &&scopedSubscription)
Move constructor.
Definition: ScopedSubscription.h:238
void release()
Explicitly releases the subscription before this object is disposes.
Definition: ScopedSubscription.h:258
std::unique_ptr< T > subscriptionId_
The subscription id, nullptr if invalid.
Definition: ScopedSubscription.h:127
std::function< void(const T &subscriptionId)> ReleaseCallbackFunction
Definition of a callback function for release requests.
Definition: ScopedSubscription.h:37
size_t operator()(const ScopedSubscriptionT< T, TOwner > &scopedSubscription) const
Hash function.
Definition: ScopedSubscription.h:309
ScopedSubscriptionT()=default
Creates an invalid (unsubscribed) subscription object.
ScopedSubscriptionT(const T &subscriptionId, ReleaseCallbackFunction releaseCallbackFunction)
Creates a new subscription object for a valid subscription id.
Definition: ScopedSubscription.h:244
bool operator!=(const ScopedSubscriptionT< T, TOwner > &scopedSubscription) const
Returns whether two subscription objects are not identical.
Definition: ScopedSubscription.h:303
bool isValid() const
Returns whether this object holds a valid subscription.
Definition: ScopedSubscription.h:271
ScopedSubscriptionT(const ScopedSubscriptionT< T, TOwner > &scopedSubscription)=delete
Disabled copy constructor.
~ScopedSubscriptionT()
Destructs the object and releases the subscription if any.
Definition: ScopedSubscription.h:252
ReleaseCallbackFunction releaseCallbackFunction_
The callback function which will be used when the subscription needs to be released.
Definition: ScopedSubscription.h:130
ScopedSubscriptionT< T, TOwner > & operator=(ScopedSubscriptionT< T, TOwner > &&scopedSubscription)
Move operator.
Definition: ScopedSubscription.h:283
This class implements a recursive scoped lock object that is activated by a boolean template paramete...
Definition: Lock.h:178
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15