added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / ObjectStore / src / impl / object_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/object_notifier.hpp"
20
21 #include "shared_realm.hpp"
22
23 using namespace realm;
24 using namespace realm::_impl;
25
26 ObjectNotifier::ObjectNotifier(Row const& row, std::shared_ptr<Realm> realm)
27 : CollectionNotifier(std::move(realm))
28 {
29     REALM_ASSERT(row.get_table());
30     set_table(*row.get_table());
31
32     m_handover = source_shared_group().export_for_handover(row);
33 }
34
35 void ObjectNotifier::release_data() noexcept
36 {
37     m_row = nullptr;
38 }
39
40 void ObjectNotifier::do_attach_to(SharedGroup& sg)
41 {
42     REALM_ASSERT(m_handover);
43     REALM_ASSERT(!m_row);
44     m_row = sg.import_from_handover(std::move(m_handover));
45 }
46
47 void ObjectNotifier::do_detach_from(SharedGroup& sg)
48 {
49     REALM_ASSERT(!m_handover);
50     if (m_row) {
51         m_handover = sg.export_for_handover(*m_row);
52         m_row = nullptr;
53     }
54 }
55
56 bool ObjectNotifier::do_add_required_change_info(TransactionChangeInfo& info)
57 {
58     REALM_ASSERT(!m_handover);
59     m_info = &info;
60     if (m_row && m_row->is_attached()) {
61         size_t table_ndx = m_row->get_table()->get_index_in_group();
62         if (table_ndx >= info.table_modifications_needed.size())
63             info.table_modifications_needed.resize(table_ndx + 1);
64         info.table_modifications_needed[table_ndx] = true;
65     }
66     return false;
67 }
68
69 void ObjectNotifier::run()
70 {
71     if (!m_row)
72         return;
73     if (!m_row->is_attached()) {
74         m_change.deletions.add(0);
75         m_row = nullptr;
76         return;
77     }
78
79     size_t table_ndx = m_row->get_table()->get_index_in_group();
80     if (table_ndx >= m_info->tables.size())
81         return;
82     auto& change = m_info->tables[table_ndx];
83     if (!change.modifications.contains(m_row->get_index()))
84         return;
85     m_change.modifications.add(0);
86     m_change.columns.reserve(change.columns.size());
87     for (auto& col : change.columns) {
88         m_change.columns.emplace_back();
89         if (col.contains(m_row->get_index()))
90             m_change.columns.back().add(0);
91     }
92 }
93
94 void ObjectNotifier::do_prepare_handover(SharedGroup&)
95 {
96     add_changes(std::move(m_change));
97 }