IEventDispatcher.h#
Fully qualified name: carb/eventdispatcher/IEventDispatcher.h
File members: carb/eventdispatcher/IEventDispatcher.h
// Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#pragma once
#include "../Interface.h"
#include "EventDispatcherTypes.h"
#define carb_eventdispatcher_IEventDispatcher_latest CARB_HEXVERSION(1, 1)
#ifndef carb_eventdispatcher_IEventDispatcher
# define carb_eventdispatcher_IEventDispatcher CARB_HEXVERSION(0, 1)
#endif
namespace carb
{
namespace eventdispatcher
{
class ObserverGuard;
struct IEventDispatcher
{
// 0.1 - Initial version
// 1.0 - (no changes)
// 1.1 - Added ability to change and query order
CARB_PLUGIN_INTERFACE_EX("carb::eventdispatcher::IEventDispatcher",
carb_eventdispatcher_IEventDispatcher_latest,
carb_eventdispatcher_IEventDispatcher)
Observer(CARB_ABI* internalObserveEvent)(int order,
RString eventName,
size_t numVariants,
NamedVariant const* variants,
ObserverFn fn,
CleanupFn cleanup,
void* ud);
bool(CARB_ABI* stopObserving)(Observer ob);
bool(CARB_ABI* internalHasObservers)(RString eventName, size_t numVariants, NamedVariant const* variants);
size_t(CARB_ABI* internalDispatch)(const EventData&);
bool(CARB_ABI* isDispatching)(bool currentThread);
#if CARB_VERSION_ATLEAST(carb_eventdispatcher_IEventDispatcher, 1, 1)
Observer(CARB_ABI* internalObserveEvent2)(const ObserveEventDesc& desc);
bool(CARB_ABI* getObserverName)(Observer ob, RStringKey& nameOut);
bool(CARB_ABI* getObserverOrder)(Observer ob, int& orderOut);
bool(CARB_ABI* setObserverOrder)(Observer ob, int newOrder);
bool(CARB_ABI* getObserverEnabled)(Observer ob, bool& enabledOut);
bool(CARB_ABI* setObserverEnabled)(Observer ob, bool enabled);
#endif
// Interface functions above
// Inline helper functions below
#if CARB_VERSION_ATLEAST(carb_eventdispatcher_IEventDispatcher, 1, 1)
template <class Invocable, class... Args>
CARB_NODISCARD ObserverGuard
observeEvent(RStringKey observerName, int order, RString eventName, Invocable&& invocable, Args&&... filterArgs);
template <class Invocable, class InIter>
CARB_NODISCARD ObserverGuard observeEventIter(
RStringKey observerName, int order, RString eventName, Invocable&& invocable, InIter begin, InIter end);
template <class R,
class Invocable CARB_NO_DOC(
,
std::enable_if_t<
std::is_same<void, cpp::void_t<decltype(std::begin(std::declval<R>()), std::end(std::declval<R>()))>>::value,
bool> = false)>
CARB_NODISCARD ObserverGuard
observeEventRange(RStringKey observerName, int order, RString eventName, Invocable&& invocable, R&& range)
{
return observeEventIter(
observerName, order, eventName, std::forward<Invocable>(invocable), range.begin(), range.end());
}
# ifndef DOXYGEN_BUILD
// Enable implicit initializer_list since {} cannot be deduced to a type
template <class Invocable, class T>
CARB_NODISCARD ObserverGuard observeEventRange(RStringKey observerName,
int order,
RString eventName,
Invocable&& invocable,
std::initializer_list<std::pair<RStringKey, T>> range)
{
return observeEventIter(
observerName, order, eventName, std::forward<Invocable>(invocable), range.begin(), range.end());
}
# endif
#else
template <class Invocable, class... Args>
CARB_NODISCARD ObserverGuard observeEvent(int order, RString eventName, Invocable&& invocable, Args&&... filterArgs);
template <class Invocable, class InIter>
CARB_NODISCARD ObserverGuard
observeEventIter(int order, RString eventName, Invocable&& invocable, InIter begin, InIter end);
template <class R,
class Invocable CARB_NO_DOC(
,
std::enable_if_t<
std::is_same<void, cpp::void_t<decltype(std::begin(std::declval<R>()), std::end(std::declval<R>()))>>::value,
bool> = false)>
CARB_NODISCARD ObserverGuard observeEventRange(int order, RString eventName, Invocable&& invocable, R&& range)
{
return observeEventIter(order, eventName, std::forward<Invocable>(invocable), range.begin(), range.end());
}
template <class Invocable, class T>
CARB_NODISCARD ObserverGuard observeEventRange(int order,
RString eventName,
Invocable&& invocable,
std::initializer_list<std::pair<RStringKey, T>> range)
{
return observeEventIter(order, eventName, std::forward<Invocable>(invocable), range.begin(), range.end());
}
#endif
template <class... Args>
bool hasObservers(RString eventName, Args&&... filterArgs);
template <class InIter>
bool hasObserversIter(RString eventName, InIter begin, InIter end);
template <class R CARB_NO_DOC(
,
std::enable_if_t<std::is_same<void, cpp::void_t<decltype(std::begin(std::declval<R>()), std::end(std::declval<R>()))>>::value,
bool> = false)>
bool hasObserversRange(RString eventName, R&& range)
{
// Implementation needs to be inline because certain compilers get very confused about the SFINAE
std::vector<NamedVariant> variants;
for (auto& r : range)
variants.emplace_back(detail::translate(r));
std::sort(variants.begin(), variants.end(), detail::NamedVariantLess{});
CARB_ASSERT(std::adjacent_find(variants.begin(), variants.end(), detail::NamedVariantEqual{}) == variants.end(),
"At least one non-unique key specified");
return internalHasObservers(eventName, variants.size(), variants.data());
}
#ifndef DOXYGEN_BUILD
// Enable implicit initializer_list since {} cannot be deduced to a type
template <class T>
bool hasObserversRange(RString eventName, std::initializer_list<std::pair<RStringKey, T>> range)
{
return hasObserversRange<std::initializer_list<std::pair<RStringKey, T>>>(eventName, std::move(range));
}
#endif
size_t dispatchEvent(const Event& event, RString* newEventName = nullptr);
template <class... Args>
size_t dispatchEvent(RString eventName, Args&&... payload);
template <class InIter>
size_t dispatchEventIter(RString eventName, InIter begin, InIter end);
template <class R>
size_t dispatchEventRange(RString eventName, R&& range)
{
// Implementation needs to be inline because certain compilers get very confused about the SFINAE
std::vector<NamedVariant> variants;
for (auto& r : range)
variants.emplace_back(detail::translate(r));
std::sort(variants.begin(), variants.end(), detail::NamedVariantLess{});
CARB_ASSERT(std::adjacent_find(variants.begin(), variants.end(), detail::NamedVariantEqual{}) == variants.end(),
"Event has duplicate keys");
return internalDispatch({ eventName, variants.size(), variants.data() });
}
#ifndef DOXYGEN_BUILD
// Enable implicit initializer_list since {} cannot be deduced to a type
template <class T>
size_t dispatchEventRange(RString eventName, std::initializer_list<std::pair<RStringKey, T>> range)
{
return dispatchEventRange<std::initializer_list<std::pair<RStringKey, T>>>(eventName, std::move(range));
}
#endif
};
} // namespace eventdispatcher
} // namespace carb
#include "IEventDispatcher.inl"