1 #ifndef USD_NOTICE_FRAMEWORK_BROKER_H
2 #define USD_NOTICE_FRAMEWORK_BROKER_H
10 #include <pxr/base/plug/plugin.h>
11 #include <pxr/base/plug/registry.h>
12 #include <pxr/base/tf/refBase.h>
13 #include <pxr/base/tf/refPtr.h>
14 #include <pxr/base/tf/weakBase.h>
15 #include <pxr/base/tf/weakPtr.h>
17 #include <pxr/usd/usd/common.h>
18 #include <pxr/usd/usd/stage.h>
24 #include <unordered_map>
55 UNF_API
virtual ~
Broker() =
default;
133 template <
class UnfNotice,
class... Args>
134 void Send(Args&&... args);
167 static void _CleanCache();
170 void _DiscoverDispatchers();
181 template <
class OutputPtr,
class OutputFactory>
184 struct UsdStageWeakPtrHasher {
187 return hash_value(ptr);
192 static std::unordered_map<
196 class _NoticeMerger {
201 void Join(_NoticeMerger&);
207 using _NoticePtrList = std::vector<UnfNotice::StageNoticeRefPtr>;
208 using _NoticePtrMap = std::unordered_map<std::string, _NoticePtrList>;
210 _NoticePtrMap _noticeMap;
211 CapturePredicate _predicate;
218 std::vector<_NoticeMerger> _mergers;
221 std::unordered_map<std::string, DispatcherPtr> _dispatcherMap;
224 template <
class UnfNotice,
class... Args>
228 UnfNotice::Create(std::forward<Args>(args)...);
237 std::is_base_of<Dispatcher, T>::value,
238 "Expecting a type derived from unf::Dispatcher.");
240 auto self = PXR_NS::TfCreateWeakPtr(
this);
241 DispatcherPtr dispatcher = PXR_NS::TfCreateRefPtr(
new T(
self));
249 const auto& dispatcher = _AddDispatcher<T>();
250 dispatcher->Register();
253 template <
class OutputPtr,
class OutputFactory>
256 PXR_NAMESPACE_USING_DIRECTIVE
258 const PXR_NS::PlugPluginPtr plugin =
259 PXR_NS::PlugRegistry::GetInstance().GetPluginForType(type);
265 if (!plugin->Load()) {
267 "Failed to load plugin %s for %s",
268 plugin->GetName().c_str(),
269 type.GetTypeName().c_str());
274 OutputFactory* factory = type.GetFactory<OutputFactory>();
277 output = factory->New(TfCreateWeakPtr(
this));
282 "Failed to manufacture %s from plugin %s",
283 type.GetTypeName().c_str(),
284 plugin->GetName().c_str());
Intermediate object between the Usd Stage and any clients that needs asynchronous handling and upstre...
Definition: broker.h:46
static UNF_API void ResetAll()
Un-register all brokers.
UNF_API void EndTransaction()
Stop a notice transaction.
UNF_API const PXR_NS::UsdStageWeakPtr GetStage() const
Return Usd Stage associated with the broker.
Definition: broker.h:64
void AddDispatcher()
Create and register a new dispatcher.
Definition: broker.h:247
static UNF_API BrokerPtr Create(const PXR_NS::UsdStageWeakPtr &stage)
Create a broker from a Usd Stage.
UNF_API Broker(const Broker &)=delete
Remove default copy constructor.
UNF_API void Reset()
Un-register broker.
UNF_API Broker & operator=(const Broker &)=delete
Remove default assignment operator.
UNF_API void BeginTransaction(CapturePredicate predicate=CapturePredicate::Default())
Start a notice transaction.
UNF_API bool IsInTransaction()
Indicate whether a notice transaction has been started.
UNF_API void BeginTransaction(const CapturePredicateFunc &)
Start a notice transaction with a capture predicate function.
void Send(Args &&... args)
Create and send a UnfNotice::StageNotice notice via the broker.
Definition: broker.h:225
UNF_API DispatcherPtr & GetDispatcher(std::string identifier)
Return dispatcher reference associated with identifier.
UNF_API void Send(const UnfNotice::StageNoticeRefPtr &)
Send a UnfNotice::StageNotice notice via the broker.
Predicate functor which indicates whether a notice can be captured during a transaction.
Definition: capturePredicate.h:29
static UNF_API CapturePredicate Default()
Create a predicate which return true for each notice type.
PXR_NS::TfRefPtr< StageNotice > StageNoticeRefPtr
Convenient alias for StageNotice reference pointer.
Definition: notice.h:124
PXR_NS::TfRefPtr< Broker > BrokerPtr
Convenient alias for Broker reference pointer.
Definition: broker.h:33
std::function< bool(const UnfNotice::StageNotice &)> CapturePredicateFunc
Convenient alias for function defining whether notice can be captured.
Definition: capturePredicate.h:16