added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / sync / instruction_applier.hpp
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 #ifndef REALM_SYNC_IMPL_INSTRUCTION_APPLIER_HPP
20 #define REALM_SYNC_IMPL_INSTRUCTION_APPLIER_HPP
21
22 #include <realm/sync/instructions.hpp>
23 #include <realm/sync/changeset.hpp>
24 #include <realm/util/logger.hpp>
25
26 namespace realm {
27 namespace sync {
28
29 struct Changeset;
30
31 struct InstructionApplier {
32     explicit InstructionApplier(Group& group) noexcept;
33
34     void apply(const Changeset& log, util::Logger* logger);
35
36 protected:
37     StringData get_string(InternString) const;
38     StringData get_string(StringBufferRange) const;
39 #define REALM_DECLARE_INSTRUCTION_HANDLER(X) void operator()(const Instruction::X&);
40     REALM_FOR_EACH_INSTRUCTION_TYPE(REALM_DECLARE_INSTRUCTION_HANDLER)
41 #undef REALM_DECLARE_INSTRUCTION_HANDLER
42     friend struct Instruction; // to allow visitor
43
44     template<class A> static void apply(A& applier, const Changeset& log, util::Logger* logger);
45
46 private:
47     const Changeset* m_log = nullptr;
48     util::Logger* m_logger = nullptr;
49     Group& m_group;
50     TableRef m_selected_table;
51     TableRef m_selected_array;
52     LinkViewRef m_selected_link_list;
53     TableRef m_link_target_table;
54
55     template <class... Args>
56     void log(const char* fmt, Args&&... args)
57     {
58         if (m_logger) {
59             m_logger->trace(fmt, std::forward<Args>(args)...); // Throws
60         }
61     }
62
63     void bad_transaction_log(const char*) const; // Throws
64
65     TableRef table_for_class_name(StringData) const; // Throws
66 };
67
68
69
70
71 // Implementation
72
73 inline InstructionApplier::InstructionApplier(Group& group) noexcept:
74     m_group(group)
75 {
76 }
77
78 template<class A>
79 inline void InstructionApplier::apply(A& applier, const Changeset& log, util::Logger* logger)
80 {
81     applier.m_log = &log;
82     applier.m_logger = logger;
83     for (auto instr: log) {
84         if (!instr)
85             continue;
86         instr->visit(applier); // Throws
87     }
88     applier.m_log = nullptr;
89     applier.m_logger = nullptr;
90     applier.m_selected_table = TableRef{};
91     applier.m_selected_link_list = LinkViewRef{};
92     applier.m_link_target_table = TableRef{};
93 }
94
95 inline void InstructionApplier::apply(const Changeset& log, util::Logger* logger)
96 {
97     apply(*this, log, logger); // Throws
98 }
99
100 } // namespace sync
101 } // namespace realm
102
103 #endif // REALM_SYNC_IMPL_INSTRUCTION_APPLIER_HPP