1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 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_THREAD_SAFE_REFERENCE_HPP
20 #define REALM_THREAD_SAFE_REFERENCE_HPP
23 #include <realm/group_shared.hpp>
33 template<typename T> class BasicRow;
34 typedef BasicRow<Table> Row;
36 // Opaque type representing an object for handover
37 class ThreadSafeReferenceBase {
39 ThreadSafeReferenceBase(const ThreadSafeReferenceBase&) = delete;
40 ThreadSafeReferenceBase& operator=(const ThreadSafeReferenceBase&) = delete;
41 ThreadSafeReferenceBase(ThreadSafeReferenceBase&&) = default;
42 ThreadSafeReferenceBase& operator=(ThreadSafeReferenceBase&&) = default;
43 ThreadSafeReferenceBase();
44 virtual ~ThreadSafeReferenceBase();
46 bool is_invalidated() const { return m_source_realm == nullptr; };
49 // Precondition: The associated Realm is for the current thread and is not in a write transaction;.
50 ThreadSafeReferenceBase(std::shared_ptr<Realm> source_realm);
52 SharedGroup& get_source_shared_group() const;
54 template <typename V, typename T>
55 V invalidate_after_import(Realm& destination_realm, T construct_with_shared_group);
60 VersionID m_version_id;
61 std::shared_ptr<Realm> m_source_realm; // Strong reference keeps alive so version stays pinned! Don't touch!!
63 bool has_same_config(Realm& realm) const;
68 class ThreadSafeReference;
71 class ThreadSafeReference<List>: public ThreadSafeReferenceBase {
74 std::unique_ptr<SharedGroup::Handover<LinkView>> m_link_view;
75 std::unique_ptr<SharedGroup::Handover<Table>> m_table;
77 // Precondition: The associated Realm is for the current thread and is not in a write transaction;.
78 ThreadSafeReference(List const& value);
80 // Precondition: Realm and handover are on same version.
81 List import_into_realm(std::shared_ptr<Realm> realm) &&;
85 class ThreadSafeReference<Object>: public ThreadSafeReferenceBase {
88 std::unique_ptr<SharedGroup::Handover<Row>> m_row;
89 std::string m_object_schema_name;
91 // Precondition: The associated Realm is for the current thread and is not in a write transaction;.
92 ThreadSafeReference(Object const& value);
94 // Precondition: Realm and handover are on same version.
95 Object import_into_realm(std::shared_ptr<Realm> realm) &&;
99 class ThreadSafeReference<Results>: public ThreadSafeReferenceBase {
102 std::unique_ptr<SharedGroup::Handover<Query>> m_query;
103 DescriptorOrdering::HandoverPatch m_ordering_patch;
105 // Precondition: The associated Realm is for the current thread and is not in a write transaction;.
106 ThreadSafeReference(Results const& value);
108 // Precondition: Realm and handover are on same version.
109 Results import_into_realm(std::shared_ptr<Realm> realm) &&;
113 #endif /* REALM_THREAD_SAFE_REFERENCE_HPP */