|
| 1 | +#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_ |
| 2 | +#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_ |
| 3 | + |
| 4 | +#include <cstdint> |
| 5 | + |
| 6 | +#include "app/src/callback.h" |
| 7 | +#include "firebase/firestore.h" |
| 8 | +#include "firebase/firestore/event_listener.h" |
| 9 | +#include "firebase/firestore/listener_registration.h" |
| 10 | +#include "firebase/firestore/firestore_errors.h" |
| 11 | + |
| 12 | +namespace firebase { |
| 13 | +namespace firestore { |
| 14 | +namespace csharp { |
| 15 | + |
| 16 | +// Add this to make this header compile when SWIG is not involved. |
| 17 | +#ifndef SWIGSTDCALL |
| 18 | +#if !defined(SWIG) && \ |
| 19 | + (defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)) |
| 20 | +#define SWIGSTDCALL __stdcall |
| 21 | +#else |
| 22 | +#define SWIGSTDCALL |
| 23 | +#endif |
| 24 | +#endif |
| 25 | + |
| 26 | +// The callbacks that are used by the listener, that need to reach back to C# |
| 27 | +// callbacks. |
| 28 | +typedef void(SWIGSTDCALL* SnapshotsInSyncCallback)(int callback_id); |
| 29 | + |
| 30 | +// Provides a C++ implementation of the EventListener for |
| 31 | +// ListenForSnapshotsInSync that can forward the calls back to the C# delegates. |
| 32 | +class SnapshotsInSyncListener : public EventListener<void> { |
| 33 | + public: |
| 34 | + SnapshotsInSyncListener(int32_t callback_id, |
| 35 | + SnapshotsInSyncCallback callback) |
| 36 | + : callback_id_(callback_id), callback_(callback) {} |
| 37 | + |
| 38 | + void OnEvent(Error error) override; |
| 39 | + |
| 40 | + // This method is a proxy to Firestore::AddSnapshotsInSyncListener() |
| 41 | + // that can be easily called from C#. It allows our C# wrapper to |
| 42 | + // track user callbacks in a dictionary keyed off of a unique int |
| 43 | + // for each user callback and then raise the correct one later. |
| 44 | + static ListenerRegistration AddListenerTo(Firestore* firestore, |
| 45 | + int32_t callback_id, |
| 46 | + SnapshotsInSyncCallback callback); |
| 47 | + |
| 48 | + private: |
| 49 | + int32_t callback_id_; |
| 50 | + SnapshotsInSyncCallback callback_; |
| 51 | +}; |
| 52 | + |
| 53 | +} // namespace csharp |
| 54 | +} // namespace firestore |
| 55 | +} // namespace firebase |
| 56 | + |
| 57 | +#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_SNAPSHOTS_IN_SYNC_LISTENER_H_ |
0 commit comments