Stream Chat Unreal SDK
Loading...
Searching...
No Matches
JsonEventSubscription.h
1// Copyright 2022 Stream.IO, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Dom/JsonObject.h"
7#include "Launch/Resources/Version.h"
8#include "StreamJson.h"
9
10template <class TEvent>
11using TEventMulticastDelegate = TMulticastDelegate<void(const TEvent& Event)>;
12template <class TEvent>
13using TEventDelegate = typename TEventMulticastDelegate<TEvent>::FDelegate;
14#if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION <= 27
15template <class TEvent, class UserClass>
16using TEventDelegateUObjectMethodPtr = typename TEventDelegate<TEvent>::template TUObjectMethodDelegate<UserClass>::FMethodPtr;
17template <class TEvent, class UserClass>
18using TEventDelegateSpMethodPtr = typename TEventDelegate<TEvent>::template TSPMethodDelegate<UserClass>::FMethodPtr;
19#else
20template <class TEvent, class UserClass>
21using TEventDelegateUObjectMethodPtr = typename TEventDelegate<TEvent>::template TMethodPtr<UserClass>;
22template <class TEvent, class UserClass>
23using TEventDelegateSpMethodPtr = typename TEventDelegate<TEvent>::template TMethodPtr<UserClass>;
24#endif
25
26class IJsonEventSubscription
27{
28public:
29 virtual ~IJsonEventSubscription() = 0;
30 virtual bool OnMessage(const TSharedRef<FJsonObject>&) = 0;
31 virtual int32 Unsubscribe(UObject* Object) = 0;
32};
33
34inline IJsonEventSubscription::~IJsonEventSubscription() = default;
35
36template <class T>
37class TJsonEventSubscription final : public IJsonEventSubscription
38{
39public:
40 virtual bool OnMessage(const TSharedRef<FJsonObject>&) override;
41 virtual int32 Unsubscribe(UObject* Object) override;
42
43 TEventMulticastDelegate<T> Delegate;
44};
45
46using FEventSubscriptionPtr = TSharedPtr<IJsonEventSubscription>;
47
48template <class T>
49bool TJsonEventSubscription<T>::OnMessage(const TSharedRef<FJsonObject>& JsonObject)
50{
51 T OutStruct;
52 if (!JsonObjectDeserialization::JsonObjectToUStruct<T>(JsonObject, &OutStruct))
53 {
54 return false;
55 }
56 check(OutStruct.Type == T::StaticType());
57
58 Delegate.Broadcast(OutStruct);
59 return true;
60}
61
62template <class T>
63int32 TJsonEventSubscription<T>::Unsubscribe(UObject* Object)
64{
65 return Delegate.RemoveAll(Object);
66}
Conversion to and from dynamic JSON objects.
Definition: StreamJson.h:16