added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / ObjectStore / src / impl / list_notifier.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #include "impl/list_notifier.hpp"
20
21 #include "shared_realm.hpp"
22
23 #include <realm/link_view.hpp>
24
25 using namespace realm;
26 using namespace realm::_impl;
27
28 ListNotifier::ListNotifier(LinkViewRef lv, std::shared_ptr<Realm> realm)
29 : CollectionNotifier(std::move(realm))
30 , m_prev_size(lv->size())
31 {
32     set_table(lv->get_target_table());
33     m_lv_handover = source_shared_group().export_linkview_for_handover(lv);
34 }
35
36 void ListNotifier::release_data() noexcept
37 {
38     m_lv.reset();
39 }
40
41 void ListNotifier::do_attach_to(SharedGroup& sg)
42 {
43     REALM_ASSERT(m_lv_handover);
44     REALM_ASSERT(!m_lv);
45     m_lv = sg.import_linkview_from_handover(std::move(m_lv_handover));
46 }
47
48 void ListNotifier::do_detach_from(SharedGroup& sg)
49 {
50     REALM_ASSERT(!m_lv_handover);
51     if (m_lv) {
52         m_lv_handover = sg.export_linkview_for_handover(m_lv);
53         m_lv = {};
54     }
55 }
56
57 bool ListNotifier::do_add_required_change_info(TransactionChangeInfo& info)
58 {
59     REALM_ASSERT(!m_lv_handover);
60     if (!m_lv || !m_lv->is_attached()) {
61         return false; // origin row was deleted after the notification was added
62     }
63
64     auto& table = m_lv->get_origin_table();
65     size_t row_ndx = m_lv->get_origin_row_index();
66     size_t col_ndx = find_container_column(table, row_ndx, m_lv, type_LinkList, &Table::get_linklist);
67     info.lists.push_back({table.get_index_in_group(), row_ndx, col_ndx, &m_change});
68
69     m_info = &info;
70     return true;
71 }
72
73 void ListNotifier::run()
74 {
75     if (!m_lv || !m_lv->is_attached()) {
76         // LV was deleted, so report all of the rows being removed if this is
77         // the first run after that
78         if (m_prev_size) {
79             m_change.deletions.set(m_prev_size);
80             m_prev_size = 0;
81         }
82         else {
83             m_change = {};
84         }
85         return;
86     }
87
88     auto row_did_change = get_modification_checker(*m_info, m_lv->get_target_table());
89     for (size_t i = 0; i < m_lv->size(); ++i) {
90         if (m_change.modifications.contains(i))
91             continue;
92         if (row_did_change(m_lv->get(i).get_index()))
93             m_change.modifications.add(i);
94     }
95
96     for (auto const& move : m_change.moves) {
97         if (m_change.modifications.contains(move.to))
98             continue;
99         if (row_did_change(m_lv->get(move.to).get_index()))
100             m_change.modifications.add(move.to);
101     }
102
103     m_prev_size = m_lv->size();
104 }
105
106 void ListNotifier::do_prepare_handover(SharedGroup&)
107 {
108     add_changes(std::move(m_change));
109 }