From 409ef553fa30d9aae8c906108711866edb58d7fa Mon Sep 17 00:00:00 2001 From: Petr Pokorny Date: Mon, 22 Feb 2021 16:00:37 +0100 Subject: [PATCH] MGLSDKMetricsManager.h imported by accident. Added observable.hpp header. --- platform/ios/platform/ios/src/Mapbox.h | 1 - src/mbgl/util/observable.hpp | 67 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/mbgl/util/observable.hpp diff --git a/platform/ios/platform/ios/src/Mapbox.h b/platform/ios/platform/ios/src/Mapbox.h index 88e0486a8..333190cf6 100644 --- a/platform/ios/platform/ios/src/Mapbox.h +++ b/platform/ios/platform/ios/src/Mapbox.h @@ -56,7 +56,6 @@ FOUNDATION_EXPORT MGL_EXPORT const unsigned char MapboxVersionString[]; #import "MGLRasterDEMSource.h" #import "MGLRasterStyleLayer.h" #import "MGLRasterTileSource.h" -#import "MGLSDKMetricsManager.h" #import "MGLShape.h" #import "MGLShapeCollection.h" #import "MGLShapeOfflineRegion.h" diff --git a/src/mbgl/util/observable.hpp b/src/mbgl/util/observable.hpp new file mode 100644 index 000000000..15af10003 --- /dev/null +++ b/src/mbgl/util/observable.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace mbgl { + +class Observer; + +struct ObservableEvent { + explicit ObservableEvent(std::string type); + std::string type; + TimePoint begin; + TimePoint end; + mapbox::base::Value data; +}; + +class Observer { +public: + virtual ~Observer() = default; + + // Notifies Observer about an event + virtual void notify(const ObservableEvent&) = 0; + + // Id of an observer, must be unique. + // Default implementation uses `this` address of an instance. + virtual std::size_t id() const; +}; + +// Observable class implements basic Publish&Subscribe functionality +class Observable { +public: + Observable(); + virtual ~Observable(); + + // Subscribes `Observer` to recieve notifications of the event types provided in `eventTypes` + // `Observer::id()` would be used as an `id` for subscription. + virtual void subscribe(const std::shared_ptr&, const std::vector& eventTypes); + + // Unsubscribes perviosly subscribed `Observer` with an `id`, that is provided by `Observer::id()` + // Whenever an empty list of events is provided, observer would be unsubscribed from all events. + virtual void unsubscribeWithId(std::size_t id, const std::vector& eventTypes); + + // Must return true if `Observable` supports an `eventType`. + virtual bool supportsEventType(const std::string& eventType) const; + + // Shorthand for virtual method accepting `Observer` id. + void unsubscribe(const std::shared_ptr&, const std::vector& eventTypes); + void unsubscribe(const std::shared_ptr&); + +protected: + void dispatchEvent(const ObservableEvent&); + bool hasSubscribers() const; + +private: + struct Subscriber; + std::map> subscribers; + mutable std::mutex mutex; +}; + +} // namespace mbgl