Skip to content

Commit 4475863

Browse files
committed
Make BindingCallbackThreadObserver a pure virtual interface
It was both virtual and stored callbacks, which was redundant. Subclasses can store their own callbacks if desired (as the C API implementation already did).
1 parent 62ca644 commit 4475863

File tree

2 files changed

+22
-72
lines changed

2 files changed

+22
-72
lines changed

src/realm/object-store/c_api/types.hpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ struct realm_sync_socket_callback : realm::c_api::WrapC,
870870
}
871871
};
872872

873-
struct CBindingThreadObserver : public realm::BindingCallbackThreadObserver {
873+
struct CBindingThreadObserver final : public realm::BindingCallbackThreadObserver {
874874
public:
875875
CBindingThreadObserver(realm_on_object_store_thread_callback_t on_thread_create,
876876
realm_on_object_store_thread_callback_t on_thread_destroy,
@@ -882,14 +882,11 @@ struct CBindingThreadObserver : public realm::BindingCallbackThreadObserver {
882882
, m_user_data{userdata, [&free_userdata] {
883883
if (free_userdata)
884884
return free_userdata;
885-
else
886-
return CBindingThreadObserver::m_default_free_userdata;
885+
return CBindingThreadObserver::m_default_free_userdata;
887886
}()}
888887
{
889888
}
890889

891-
virtual ~CBindingThreadObserver() = default;
892-
893890
void did_create_thread() override
894891
{
895892
if (m_create_callback_func)
@@ -918,25 +915,25 @@ struct CBindingThreadObserver : public realm::BindingCallbackThreadObserver {
918915
/// {@
919916
/// For testing: Return the values in this CBindingThreadObserver for comparing if two objects
920917
/// have the same callback functions and userdata ptr values.
921-
inline realm_on_object_store_thread_callback_t test_get_create_callback_func() const noexcept
918+
realm_on_object_store_thread_callback_t test_get_create_callback_func() const noexcept
922919
{
923920
return m_create_callback_func;
924921
}
925-
inline realm_on_object_store_thread_callback_t test_get_destroy_callback_func() const noexcept
922+
realm_on_object_store_thread_callback_t test_get_destroy_callback_func() const noexcept
926923
{
927924
return m_destroy_callback_func;
928925
}
929-
inline realm_on_object_store_error_callback_t test_get_error_callback_func() const noexcept
926+
realm_on_object_store_error_callback_t test_get_error_callback_func() const noexcept
930927
{
931928
return m_error_callback_func;
932929
}
933-
inline realm_userdata_t test_get_userdata_ptr() const noexcept
930+
realm_userdata_t test_get_userdata_ptr() const noexcept
934931
{
935932
return m_user_data.get();
936933
}
937934
/// @}
938935

939-
protected:
936+
private:
940937
CBindingThreadObserver() = default;
941938

942939
static constexpr realm_free_userdata_func_t m_default_free_userdata = [](realm_userdata_t) {};

src/realm/sync/binding_callback_thread_observer.hpp

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,78 +20,31 @@
2020
#define REALM_OS_BINDING_CALLBACK_THREAD_OBSERVER_HPP
2121

2222
#include <exception>
23-
#include <functional>
24-
#include <optional>
25-
2623

2724
namespace realm {
28-
// Interface for bindings interested in registering callbacks before/after the ObjectStore thread runs.
29-
// This is for example helpful to attach/detach the pthread to the JavaVM in order to be able to perform JNI calls.
25+
// Interface for observing the lifecycle of the worker thread used by
26+
// DefaultSocketProvider. This is required to be able to attach/detach the thread
27+
// to the JVM to be able to perform JNI calls.
3028
struct BindingCallbackThreadObserver {
31-
using NotificationCallback = std::function<void()>;
32-
using ErrorCallback = std::function<bool(const std::exception&)>;
33-
34-
// Create a BindingCallbackThreadObserver that can be used in SyncClientConfig
35-
BindingCallbackThreadObserver(std::optional<NotificationCallback>&& did_create_thread,
36-
std::optional<NotificationCallback>&& will_destroy_thread,
37-
std::optional<ErrorCallback>&& error_handler)
38-
: m_create_thread_callback{std::move(did_create_thread)}
39-
, m_destroy_thread_callback{std::move(will_destroy_thread)}
40-
, m_handle_error_callback{std::move(error_handler)}
41-
{
42-
}
43-
4429
virtual ~BindingCallbackThreadObserver() = default;
4530

46-
///
47-
/// Execution Functions - check for a valid instance and if the function was set
48-
///
49-
50-
// Call the stored create thread callback function with the id of this thread
51-
// Can be overridden to provide a custom implementation
52-
virtual void did_create_thread()
53-
{
54-
if (m_create_thread_callback) {
55-
(*m_create_thread_callback)();
56-
}
57-
}
58-
59-
// Call the stored destroy thread callback function with the id of this thread
60-
// Can be overridden to provide a custom implementation
61-
virtual void will_destroy_thread()
62-
{
63-
if (m_destroy_thread_callback) {
64-
(*m_destroy_thread_callback)();
65-
}
66-
}
67-
68-
// Call the stored handle error callback function with the id of this thread
69-
// IMPORTANT: If a function is supplied that handles the exception, it must
70-
// call abort() or cause the application to crash since the SyncClient will
71-
// be in a bad state if this occurs and will not be able to shut down properly.
72-
// Can be overridden to provide a custom implementation
73-
// Return true if the exception was handled by this function, otherwise false
74-
virtual bool handle_error(const std::exception& e)
31+
// Called on the thread shortly after it is created. This is guaranteed to
32+
// be called before any other callbacks to the SDK are made.
33+
virtual void did_create_thread() {}
34+
// Called on the thread shortly before it is destroyed. No further callbacks
35+
// to the SDK on the thread will be made after this is called.
36+
virtual void will_destroy_thread() {}
37+
// If has_handle_error() returns true, any uncaught exceptions from the
38+
// event loop are passed to this. If this returns true, the thread exits
39+
// cleanly, while if it returns false the exception is rethrown.
40+
virtual bool handle_error(const std::exception&)
7541
{
76-
if (!m_handle_error_callback)
77-
return false;
78-
79-
return (*m_handle_error_callback)(e);
42+
return false;
8043
}
81-
82-
// Return true if this event loop observer has a handle error callback defined
8344
virtual bool has_handle_error()
8445
{
85-
return bool(m_handle_error_callback);
46+
return false;
8647
}
87-
88-
protected:
89-
// Default constructor
90-
BindingCallbackThreadObserver() = default;
91-
92-
std::optional<NotificationCallback> m_create_thread_callback;
93-
std::optional<NotificationCallback> m_destroy_thread_callback;
94-
std::optional<ErrorCallback> m_handle_error_callback;
9548
};
9649

9750
} // namespace realm

0 commit comments

Comments
 (0)