EventsUtils.h#

Fully qualified name: carb/events/EventsUtils.h

File members: carb/events/EventsUtils.h

// SPDX-FileCopyrightText: Copyright (c) 2019-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
//
// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
// property and proprietary rights in and to this material, related
// documentation and any modifications thereto. Any use, reproduction,
// disclosure or distribution of this material and related documentation
// without an express license agreement from NVIDIA CORPORATION or
// its affiliates is strictly prohibited.

#pragma once

#include "../Defines.h"
#include "../InterfaceUtils.h"
#include "../ObjectUtils.h"
#include "IEvents.h"

#include <functional>
#include <utility>

namespace carb::events
{

inline IEvents* getCachedEventsInterface()
{
    return getCachedInterface<IEvents>();
}

class LambdaEventListener : public IEventListener
{
public:
    LambdaEventListener(std::function<void(IEvent*)> fn) : m_fn(std::move(fn))
    {
    }

    void onEvent(IEvent* e) override
    {
        if (m_fn)
            m_fn(e);
    }

private:
    std::function<void(IEvent*)> m_fn;

    CARB_IOBJECT_IMPL
};

[[nodiscard]] inline std::vector<ObjectPtr<ISubscription>> getSubscriptionsToPop(IEventStream* stream)
{
    std::vector<ObjectPtr<ISubscription>> subscriptions;
    subscriptions.reserve(stream->getSubscriptionToPopCount());
    stream->walkSubscriptionsToPop(
        [](ISubscription* s, void* userData) {
            auto& subs = *static_cast<decltype(subscriptions)*>(userData);
            subs.emplace_back(carb::borrowObject(s));
            return true;
        },
        &subscriptions);
    return subscriptions;
}

[[nodiscard]] inline std::vector<ObjectPtr<ISubscription>> getSubscriptionsToPush(IEventStream* stream)
{
    std::vector<ObjectPtr<ISubscription>> subscriptions;
    subscriptions.reserve(stream->getSubscriptionToPushCount());
    stream->walkSubscriptionsToPush(
        [](ISubscription* s, void* userData) {
            auto& subs = *static_cast<decltype(subscriptions)*>(userData);
            subs.emplace_back(carb::borrowObject(s));
            return true;
        },
        &subscriptions);
    return subscriptions;
}

#if CARB_VERSION_ATLEAST(carb_events_IEvents, 1, 5)
[[nodiscard]] inline ObjectPtr<ISubscription> createSubscriptionToPop(IEventStream* stream,
                                                                      std::function<void(IEvent*)> onEventFn,
                                                                      Order order = kDefaultOrder,
                                                                      cpp::string_view subscriptionName = {})
{
    return stream->createSubscriptionToPop(
        carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
}

[[nodiscard]] inline ObjectPtr<ISubscription> createSubscriptionToPopByType(IEventStream* stream,
                                                                            EventType eventType,
                                                                            std::function<void(IEvent*)> onEventFn,
                                                                            Order order = kDefaultOrder,
                                                                            cpp::string_view subscriptionName = {})
{
    return stream->createSubscriptionToPopByType(
        eventType, carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
}

[[nodiscard]] inline ObjectPtr<ISubscription> createSubscriptionToPush(IEventStream* stream,
                                                                       std::function<void(IEvent*)> onEventFn,
                                                                       Order order = kDefaultOrder,
                                                                       cpp::string_view subscriptionName = {})
{
    return stream->createSubscriptionToPush(
        carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
}

[[nodiscard]] inline ObjectPtr<ISubscription> createSubscriptionToPushByType(IEventStream* stream,
                                                                             EventType eventType,
                                                                             std::function<void(IEvent*)> onEventFn,
                                                                             Order order = kDefaultOrder,
                                                                             cpp::string_view subscriptionName = {})
{
    return stream->createSubscriptionToPushByType(
        eventType, carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
}

[[nodiscard]] CARB_EVENTS_DEPRECATED_1_5("use createSubscriptionToPop with string_view") inline ObjectPtr<
    ISubscription> createSubscriptionToPop(IEventStream1_0* stream,
                                           std::function<void(IEvent*)> onEventFn,
                                           Order order,
                                           cpp::unbounded_string subscriptionName)
{
    CARB_IGNORE_DEPRECATION_BEGIN
    return stream->createSubscriptionToPop(
        carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
    CARB_IGNORE_DEPRECATION_END
}

[[nodiscard]] CARB_EVENTS_DEPRECATED_1_5("use createSubscriptionToPopByType with string_view") inline ObjectPtr<
    ISubscription> createSubscriptionToPopByType(IEventStream1_0* stream,
                                                 EventType eventType,
                                                 std::function<void(IEvent*)> onEventFn,
                                                 Order order,
                                                 cpp::unbounded_string subscriptionName)
{
    CARB_IGNORE_DEPRECATION_BEGIN
    return stream->createSubscriptionToPopByType(
        eventType, carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
    CARB_IGNORE_DEPRECATION_END
}

[[nodiscard]] CARB_EVENTS_DEPRECATED_1_5("use createSubscriptionToPush with string_view") inline ObjectPtr<
    ISubscription> createSubscriptionToPush(IEventStream1_0* stream,
                                            std::function<void(IEvent*)> onEventFn,
                                            Order order,
                                            cpp::unbounded_string subscriptionName)
{
    CARB_IGNORE_DEPRECATION_BEGIN
    return stream->createSubscriptionToPush(
        carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
    CARB_IGNORE_DEPRECATION_END
}

[[nodiscard]] CARB_EVENTS_DEPRECATED_1_5("use createSubscriptionToPushByType with string_view") inline ObjectPtr<
    ISubscription> createSubscriptionToPushByType(IEventStream1_0* stream,
                                                  EventType eventType,
                                                  std::function<void(IEvent*)> onEventFn,
                                                  Order order,
                                                  cpp::unbounded_string subscriptionName)
{
    CARB_IGNORE_DEPRECATION_BEGIN
    return stream->createSubscriptionToPushByType(
        eventType, carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
    CARB_IGNORE_DEPRECATION_END
}

#else
inline ObjectPtr<ISubscription> createSubscriptionToPop(IEventStream* stream,
                                                        std::function<void(IEvent*)> onEventFn,
                                                        Order order = kDefaultOrder,
                                                        const char* subscriptionName = nullptr)
{
    return stream->createSubscriptionToPop(
        carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
}

inline ObjectPtr<ISubscription> createSubscriptionToPopByType(IEventStream* stream,
                                                              EventType eventType,
                                                              std::function<void(IEvent*)> onEventFn,
                                                              Order order = kDefaultOrder,
                                                              const char* subscriptionName = nullptr)
{
    return stream->createSubscriptionToPopByType(
        eventType, carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
}

inline ObjectPtr<ISubscription> createSubscriptionToPush(IEventStream* stream,
                                                         std::function<void(IEvent*)> onEventFn,
                                                         Order order = kDefaultOrder,
                                                         const char* subscriptionName = nullptr)
{
    return stream->createSubscriptionToPush(
        carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
}

inline ObjectPtr<ISubscription> createSubscriptionToPushByType(IEventStream* stream,
                                                               EventType eventType,
                                                               std::function<void(IEvent*)> onEventFn,
                                                               Order order = kDefaultOrder,
                                                               const char* subscriptionName = nullptr)
{
    return stream->createSubscriptionToPushByType(
        eventType, carb::stealObject(new LambdaEventListener(std::move(onEventFn))).get(), order, subscriptionName);
}
#endif

} // namespace carb::events