@@ -203,6 +203,36 @@ namespace NActors {
203
203
}
204
204
};
205
205
206
+ /* *
207
+ * Allows customizing behavior based on the event type
208
+ */
209
+ template <class TEvType >
210
+ struct TTestEventObserverTraits {
211
+ static bool Match (IEventHandle::TPtr& ev) noexcept {
212
+ return ev->GetTypeRewrite () == TEvType::EventType;
213
+ }
214
+
215
+ static typename TEvType::TPtr& Convert (IEventHandle::TPtr& ev) noexcept {
216
+ return reinterpret_cast <typename TEvType::TPtr&>(ev);
217
+ }
218
+ };
219
+
220
+ template <>
221
+ struct TTestEventObserverTraits <IEventHandle> {
222
+ static constexpr bool Match (IEventHandle::TPtr&) noexcept {
223
+ return true ;
224
+ }
225
+
226
+ static constexpr IEventHandle::TPtr& Convert (IEventHandle::TPtr& ev) noexcept {
227
+ return ev;
228
+ }
229
+ };
230
+
231
+ template <class TEvType >
232
+ struct TTestEventObserverTraits <TEventHandle<TEvType>>
233
+ : public TTestEventObserverTraits<TEvType>
234
+ {};
235
+
206
236
class TTestActorRuntimeBase : public TNonCopyable {
207
237
public:
208
238
class TEdgeActor ;
@@ -375,24 +405,19 @@ namespace NActors {
375
405
observerHolder.Remove();
376
406
*/
377
407
378
- template <typename TEvType>
408
+ template <typename TEvType = IEventHandle >
379
409
TEventObserverHolder AddObserver (std::function<void (typename TEvType::TPtr&)> observerFunc)
380
410
{
381
- auto baseFunc = [observerFunc](TAutoPtr<IEventHandle>& event) {
382
- if (event && event->GetTypeRewrite () == TEvType::EventType)
383
- observerFunc (*(reinterpret_cast <typename TEvType::TPtr*>(&event)));
411
+ auto baseFunc = [observerFunc](IEventHandle::TPtr& event) {
412
+ if (event && TTestEventObserverTraits<TEvType>::Match (event)) {
413
+ observerFunc (TTestEventObserverTraits<TEvType>::Convert (event));
414
+ }
384
415
};
385
416
386
417
auto iter = ObserverFuncs.insert (ObserverFuncs.end (), baseFunc);
387
418
return TEventObserverHolder (&ObserverFuncs, std::move (iter));
388
419
}
389
420
390
- TEventObserverHolder AddObserver (std::function<void (TAutoPtr<IEventHandle>&)> observerFunc)
391
- {
392
- auto iter = ObserverFuncs.insert (ObserverFuncs.end (), observerFunc);
393
- return TEventObserverHolder (&ObserverFuncs, std::move (iter));
394
- }
395
-
396
421
template <typename T>
397
422
void AppendToLogSettings (NLog::EComponent minVal, NLog::EComponent maxVal, T func) {
398
423
Y_ABORT_UNLESS (!IsInitialized);
@@ -445,15 +470,14 @@ namespace NActors {
445
470
TDuration simTimeout = TDuration::Max())
446
471
{
447
472
typename TEvent::TPtr handle;
448
- const ui32 eventType = TEvent::EventType;
449
473
WaitForEdgeEvents ([&](TTestActorRuntimeBase& runtime, TAutoPtr<IEventHandle>& event) {
450
474
Y_UNUSED (runtime);
451
- if (event-> GetTypeRewrite () != eventType )
475
+ if (!TTestEventObserverTraits<TEvent>:: Match (event) )
452
476
return false ;
453
477
454
- typename TEvent::TPtr* typedEvent = reinterpret_cast < typename TEvent::TPtr*>(& event);
455
- if (predicate (* typedEvent)) {
456
- handle = * typedEvent;
478
+ typename TEvent::TPtr& typedEvent = TTestEventObserverTraits< TEvent>:: Convert ( event);
479
+ if (predicate (typedEvent)) {
480
+ handle = std::move ( typedEvent) ;
457
481
return true ;
458
482
}
459
483
@@ -809,8 +833,8 @@ namespace NActors {
809
833
const std::function<bool (const typename TEvent::TPtr&)>& predicate) {
810
834
ev.Destroy ();
811
835
for (auto & event : events) {
812
- if (event && event-> GetTypeRewrite () == TEvent::EventType ) {
813
- if (predicate (reinterpret_cast < const typename TEvent::TPtr&> (event))) {
836
+ if (event && TTestEventObserverTraits< TEvent>:: Match (event) ) {
837
+ if (predicate (TTestEventObserverTraits< TEvent>:: Convert (event))) {
814
838
ev = event;
815
839
return ev->CastAsLocal <TEvent>();
816
840
}
0 commit comments