| #ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HFT_CORE_MANAGER_H_ |
| #define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HFT_CORE_MANAGER_H_ |
| |
| #include <bit> |
| #include <cstdint> |
| #include <memory> |
| |
| #include "absl/functional/any_invocable.h" |
| #include "absl/status/status.h" |
| #include "absl/status/statusor.h" |
| #include "payload.pb.h" |
| #include "subscription_params.pb.h" |
| |
| namespace milotic_hft { |
| |
| // The largest sensor sampling interval (ms) accepted; the largest power-of-two |
| // int32 (~12 days). |
| inline constexpr int32_t kMaxSensorSamplingIntervalMs = 1 << 30; |
| |
| // Whether `sampling_interval_ms` is a valid sensor sampling interval: a power |
| // of two in [1, kMaxSensorSamplingIntervalMs]. Sensor subscriptions must |
| // request a power-of-two interval (AddSubscription rejects others) so that the |
| // lowest requested period divides every other, resampling reduces to |
| // power-of-two decimation, and a coarser subscriber's samples are a strict |
| // subset of a finer one's. |
| inline bool IsValidSensorSamplingIntervalMs(int32_t sampling_interval_ms) { |
| return sampling_interval_ms >= 1 && |
| sampling_interval_ms <= kMaxSensorSamplingIntervalMs && |
| std::has_single_bit(static_cast<uint32_t>(sampling_interval_ms)); |
| } |
| |
| class SubscriptionManager { |
| public: |
| // Opaque subscription handle. The subscription is kept alive as long as |
| // any strong reference to this handle exists; callers unwind the |
| // subscription by calling Unsubscribe or by dropping the last reference. |
| class Subscription { |
| public: |
| virtual ~Subscription() = default; |
| }; |
| |
| virtual ~SubscriptionManager() = default; |
| |
| // Adds a new subscription to the manager. The returned handle is the |
| // subscription's lifetime anchor: the subscription persists until |
| // Unsubscribe is called on it (or the handle is destroyed). |
| // `on_data_callback` will be called when new data is available to be |
| // exported to the subscriber. |
| virtual absl::StatusOr<std::shared_ptr<Subscription>> AddSubscription( |
| const SubscriptionParams& params, |
| absl::AnyInvocable<void(Payload&&)> on_data_callback) = 0; |
| virtual absl::Status Unsubscribe( |
| const std::shared_ptr<Subscription>& subscription) = 0; |
| }; |
| |
| } // namespace milotic_hft |
| |
| #endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HFT_CORE_MANAGER_H_ |