Skip to content

Commit

Permalink
Merge pull request #61 from realm/tg/notifications
Browse files Browse the repository at this point in the history
Add fine-grained notification stuff
  • Loading branch information
tgoyne committed May 20, 2016
2 parents deea1e8 + db55770 commit 1d34e78
Show file tree
Hide file tree
Showing 55 changed files with 7,793 additions and 1,016 deletions.
17 changes: 14 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
set(SOURCES
collection_notifications.cpp
index_set.cpp
list.cpp
object_schema.cpp
object_store.cpp
results.cpp
schema.cpp
shared_realm.cpp
impl/async_query.cpp
impl/collection_change_builder.cpp
impl/collection_notifier.cpp
impl/list_notifier.cpp
impl/realm_coordinator.cpp
impl/results_notifier.cpp
impl/transact_log_handler.cpp
parser/parser.cpp
parser/query_builder.cpp)

set(HEADERS
collection_notifications.hpp
index_set.hpp
list.hpp
object_schema.hpp
object_store.hpp
property.hpp
results.hpp
schema.hpp
shared_realm.hpp
impl/weak_realm_notifier.hpp
impl/weak_realm_notifier_base.hpp
impl/collection_change_builder.hpp
impl/collection_notifier.hpp
impl/external_commit_helper.hpp
impl/list_notifier.hpp
impl/realm_coordinator.hpp
impl/results_notifier.hpp
impl/transact_log_handler.hpp
impl/weak_realm_notifier.hpp
impl/weak_realm_notifier_base.hpp
parser/parser.hpp
parser/query_builder.hpp
util/atomic_shared_ptr.hpp)
Expand Down
56 changes: 56 additions & 0 deletions src/collection_notifications.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

#include "collection_notifications.hpp"

#include "impl/collection_notifier.hpp"

using namespace realm;
using namespace realm::_impl;

NotificationToken::NotificationToken(std::shared_ptr<_impl::CollectionNotifier> notifier, size_t token)
: m_notifier(std::move(notifier)), m_token(token)
{
}

NotificationToken::~NotificationToken()
{
// m_notifier itself (and not just the pointed-to thing) needs to be accessed
// atomically to ensure that there are no data races when the token is
// destroyed after being modified on a different thread.
// This is needed despite the token not being thread-safe in general as
// users find it very surprising for obj-c objects to care about what
// thread they are deallocated on.
if (auto notifier = m_notifier.exchange({})) {
notifier->remove_callback(m_token);
}
}

NotificationToken::NotificationToken(NotificationToken&& rgt) = default;

NotificationToken& NotificationToken::operator=(realm::NotificationToken&& rgt)
{
if (this != &rgt) {
if (auto notifier = m_notifier.exchange({})) {
notifier->remove_callback(m_token);
}
m_notifier = std::move(rgt.m_notifier);
m_token = rgt.m_token;
}
return *this;
}
71 changes: 71 additions & 0 deletions src/collection_notifications.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

#ifndef REALM_COLLECTION_NOTIFICATIONS_HPP
#define REALM_COLLECTION_NOTIFICATIONS_HPP

#include "index_set.hpp"
#include "util/atomic_shared_ptr.hpp"

#include <exception>
#include <functional>
#include <memory>
#include <vector>

namespace realm {
namespace _impl {
class CollectionNotifier;
}

// A token which keeps an asynchronous query alive
struct NotificationToken {
NotificationToken() = default;
NotificationToken(std::shared_ptr<_impl::CollectionNotifier> notifier, size_t token);
~NotificationToken();

NotificationToken(NotificationToken&&);
NotificationToken& operator=(NotificationToken&&);

NotificationToken(NotificationToken const&) = delete;
NotificationToken& operator=(NotificationToken const&) = delete;

private:
util::AtomicSharedPtr<_impl::CollectionNotifier> m_notifier;
size_t m_token;
};

struct CollectionChangeSet {
struct Move {
size_t from;
size_t to;

bool operator==(Move m) const { return from == m.from && to == m.to; }
};

IndexSet deletions;
IndexSet insertions;
IndexSet modifications;
std::vector<Move> moves;

bool empty() const { return deletions.empty() && insertions.empty() && modifications.empty() && moves.empty(); }
};

using CollectionChangeCallback = std::function<void (CollectionChangeSet, std::exception_ptr)>;
} // namespace realm

#endif // REALM_COLLECTION_NOTIFICATIONS_HPP
Loading

0 comments on commit 1d34e78

Please sign in to comment.