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 #import <Foundation/Foundation.h>
20 #import <unordered_map>
30 class RLMObservationInfo;
31 @class RLMRealm, RLMSchema, RLMObjectSchema, RLMProperty;
33 NS_ASSUME_NONNULL_BEGIN
36 // Add specializations so that NSString can be used as the key for hash containers
37 template<> struct hash<NSString *> {
38 size_t operator()(__unsafe_unretained NSString *const str) const {
42 template<> struct equal_to<NSString *> {
43 bool operator()(__unsafe_unretained NSString * lhs, __unsafe_unretained NSString *rhs) const {
44 return [lhs isEqualToString:rhs];
49 // The per-RLMRealm object schema information which stores the cached table
50 // reference, handles table column lookups, and tracks observed objects
53 RLMClassInfo(RLMRealm *, RLMObjectSchema *, const realm::ObjectSchema *);
55 __unsafe_unretained RLMRealm *const realm;
56 __unsafe_unretained RLMObjectSchema *const rlmObjectSchema;
57 const realm::ObjectSchema *const objectSchema;
59 // Storage for the functionality in RLMObservation for handling indirect
60 // changes to KVO-observed things
61 std::vector<RLMObservationInfo *> observedObjects;
63 // Get the table for this object type. Will return nullptr only if it's a
64 // read-only Realm that is missing the table entirely.
65 realm::Table *_Nullable table() const;
67 // Get the RLMProperty for a given table column, or `nil` if it is a column
68 // not used by the current schema
69 RLMProperty *_Nullable propertyForTableColumn(NSUInteger) const noexcept;
71 // Get the RLMProperty that's used as the primary key, or `nil` if there is
72 // no primary key for the current schema
73 RLMProperty *_Nullable propertyForPrimaryKey() const noexcept;
75 // Get the table column for the given property. The property must be a valid
76 // persisted property.
77 NSUInteger tableColumn(NSString *propertyName) const;
78 NSUInteger tableColumn(RLMProperty *property) const;
80 // Get the info for the target of the link at the given property index.
81 RLMClassInfo &linkTargetType(size_t propertyIndex);
83 // Get the info for the target of the given property
84 RLMClassInfo &linkTargetType(realm::Property const& property);
86 void releaseTable() { m_table = nullptr; }
89 mutable realm::Table *_Nullable m_table = nullptr;
90 std::vector<RLMClassInfo *> m_linkTargets;
93 // A per-RLMRealm object schema map which stores RLMClassInfo keyed on the name
95 using impl = std::unordered_map<NSString *, RLMClassInfo>;
97 RLMSchemaInfo() = default;
98 RLMSchemaInfo(RLMRealm *realm);
100 RLMSchemaInfo clone(realm::Schema const& source_schema, RLMRealm *target_realm);
102 // Look up by name, throwing if it's not present
103 RLMClassInfo& operator[](NSString *name);
105 impl::iterator begin() noexcept;
106 impl::iterator end() noexcept;
107 impl::const_iterator begin() const noexcept;
108 impl::const_iterator end() const noexcept;
110 std::unordered_map<NSString *, RLMClassInfo> m_objects;
113 NS_ASSUME_NONNULL_END