1 /*************************************************************************
3 * Copyright 2017 Realm Inc.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 **************************************************************************/
19 #ifndef REALM_SYNC_IMPL_INSTRUCTION_APPLIER_HPP
20 #define REALM_SYNC_IMPL_INSTRUCTION_APPLIER_HPP
22 #include <realm/sync/instructions.hpp>
23 #include <realm/sync/changeset.hpp>
24 #include <realm/util/logger.hpp>
31 struct InstructionApplier {
32 explicit InstructionApplier(Group& group) noexcept;
34 void apply(const Changeset& log, util::Logger* logger);
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
44 template<class A> static void apply(A& applier, const Changeset& log, util::Logger* logger);
47 const Changeset* m_log = nullptr;
48 util::Logger* m_logger = nullptr;
50 TableRef m_selected_table;
51 TableRef m_selected_array;
52 LinkViewRef m_selected_link_list;
53 TableRef m_link_target_table;
55 template <class... Args>
56 void log(const char* fmt, Args&&... args)
59 m_logger->trace(fmt, std::forward<Args>(args)...); // Throws
63 void bad_transaction_log(const char*) const; // Throws
65 TableRef table_for_class_name(StringData) const; // Throws
73 inline InstructionApplier::InstructionApplier(Group& group) noexcept:
79 inline void InstructionApplier::apply(A& applier, const Changeset& log, util::Logger* logger)
82 applier.m_logger = logger;
83 for (auto instr: log) {
86 instr->visit(applier); // Throws
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{};
95 inline void InstructionApplier::apply(const Changeset& log, util::Logger* logger)
97 apply(*this, log, logger); // Throws
103 #endif // REALM_SYNC_IMPL_INSTRUCTION_APPLIER_HPP