added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / ObjectStore / src / impl / primitive_list_notifier.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2017 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/primitive_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 PrimitiveListNotifier::PrimitiveListNotifier(TableRef table, std::shared_ptr<Realm> realm)
29 : CollectionNotifier(std::move(realm))
30 , m_prev_size(table->size())
31 {
32     set_table(*table->get_parent_table());
33     m_table_handover = source_shared_group().export_table_for_handover(table);
34 }
35
36 void PrimitiveListNotifier::release_data() noexcept
37 {
38     m_table.reset();
39 }
40
41 void PrimitiveListNotifier::do_attach_to(SharedGroup& sg)
42 {
43     REALM_ASSERT(m_table_handover);
44     REALM_ASSERT(!m_table);
45     m_table = sg.import_table_from_handover(std::move(m_table_handover));
46 }
47
48 void PrimitiveListNotifier::do_detach_from(SharedGroup& sg)
49 {
50     REALM_ASSERT(!m_table_handover);
51     if (m_table) {
52         m_table_handover = sg.export_table_for_handover(m_table);
53         m_table = {};
54     }
55 }
56
57 bool PrimitiveListNotifier::do_add_required_change_info(TransactionChangeInfo& info)
58 {
59     REALM_ASSERT(!m_table_handover);
60     if (!m_table || !m_table->is_attached()) {
61         return false; // origin row was deleted after the notification was added
62     }
63
64     auto& table = *m_table->get_parent_table();
65     size_t row_ndx = m_table->get_parent_row_index();
66     size_t col_ndx = find_container_column(table, row_ndx, m_table, type_Table, &Table::get_subtable);
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 PrimitiveListNotifier::run()
74 {
75     if (!m_table || !m_table->is_attached()) {
76         // Table was deleted entirely, so report all of the rows being removed
77         // if this is 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     if (!m_change.deletions.empty() && m_change.deletions.begin()->second == std::numeric_limits<size_t>::max()) {
89         // Table was cleared, so set the deletions to the actual previous size
90         m_change.deletions.set(m_prev_size);
91     }
92
93     m_prev_size = m_table->size();
94 }
95
96 void PrimitiveListNotifier::do_prepare_handover(SharedGroup&)
97 {
98     add_changes(std::move(m_change));
99 }