Blame view

Pods/Realm/include/RLMClassInfo.hpp 5.53 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  ////////////////////////////////////////////////////////////////////////////
  //
  // Copyright 2016 Realm Inc.
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  // http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  //
  ////////////////////////////////////////////////////////////////////////////
  
  #import <Foundation/Foundation.h>
  
  #import <realm/table_ref.hpp>
  #import <realm/util/optional.hpp>
  
  #import <unordered_map>
  #import <vector>
  
  namespace realm {
      class ObjectSchema;
      class Schema;
      struct Property;
      struct ColKey;
      struct TableKey;
  }
  
  class RLMObservationInfo;
  @class RLMRealm, RLMSchema, RLMObjectSchema, RLMProperty;
  
  NS_ASSUME_NONNULL_BEGIN
  
  namespace std {
  // Add specializations so that NSString can be used as the key for hash containers
  template<> struct hash<NSString *> {
      size_t operator()(__unsafe_unretained NSString *const str) const {
          return [str hash];
      }
  };
  template<> struct equal_to<NSString *> {
      bool operator()(__unsafe_unretained NSString * lhs, __unsafe_unretained NSString *rhs) const {
          return [lhs isEqualToString:rhs];
      }
  };
  }
  
  // The per-RLMRealm object schema information which stores the cached table
  // reference, handles table column lookups, and tracks observed objects
  class RLMClassInfo {
  public:
      RLMClassInfo(RLMRealm *, RLMObjectSchema *, const realm::ObjectSchema *);
  
      RLMClassInfo(RLMRealm *realm, RLMObjectSchema *rlmObjectSchema,
                   std::unique_ptr<realm::ObjectSchema> objectSchema);
  
      __unsafe_unretained RLMRealm *const realm;
      __unsafe_unretained RLMObjectSchema *const rlmObjectSchema;
      const realm::ObjectSchema *const objectSchema;
  
      // Storage for the functionality in RLMObservation for handling indirect
      // changes to KVO-observed things
      std::vector<RLMObservationInfo *> observedObjects;
  
      // Get the table for this object type. Will return nullptr only if it's a
      // read-only Realm that is missing the table entirely.
      realm::TableRef table() const;
  
      // Get the RLMProperty for a given table column, or `nil` if it is a column
      // not used by the current schema
      RLMProperty *_Nullable propertyForTableColumn(realm::ColKey) const noexcept;
  
      // Get the RLMProperty that's used as the primary key, or `nil` if there is
      // no primary key for the current schema
      RLMProperty *_Nullable propertyForPrimaryKey() const noexcept;
  
      // Get the table column for the given property. The property must be a valid
      // persisted property.
      realm::ColKey tableColumn(NSString *propertyName) const;
      realm::ColKey tableColumn(RLMProperty *property) const;
      // Get the table column key for the given computed property. The property
      // must be a valid computed property.
      // Subscripting a `realm::ObjectSchema->computed_properties[property.index]`
      // does not return a valid colKey, unlike subscripting persisted_properties.
      // This method retrieves a valid column key for computed properties by
      // getting the opposite table column of the origin's "forward" link.
      realm::ColKey computedTableColumn(RLMProperty *property) const;
  
      // Get the info for the target of the link at the given property index.
      RLMClassInfo &linkTargetType(size_t propertyIndex);
  
      // Get the info for the target of the given property
      RLMClassInfo &linkTargetType(realm::Property const& property);
  
      // Get the corresponding ClassInfo for the given Realm
      RLMClassInfo &resolve(RLMRealm *);
  
      // Return true if the RLMObjectSchema is for a Swift class
      bool isSwiftClass() const noexcept;
  
      // Returns true if this was a dynamically added type
      bool isDynamic() const noexcept;
  
  private:
      // If the ObjectSchema is not owned by the realm instance
      // we need to manually manage the ownership of the object.
      std::unique_ptr<realm::ObjectSchema> dynamicObjectSchema;
      [[maybe_unused]] RLMObjectSchema *_Nullable dynamicRLMObjectSchema;
  };
  
  // A per-RLMRealm object schema map which stores RLMClassInfo keyed on the name
  class RLMSchemaInfo {
      using impl = std::unordered_map<NSString *, RLMClassInfo>;
  
  public:
      RLMSchemaInfo() = default;
      RLMSchemaInfo(RLMRealm *realm);
  
      RLMSchemaInfo clone(realm::Schema const& source_schema, RLMRealm *target_realm);
  
      // Look up by name, throwing if it's not present
      RLMClassInfo& operator[](NSString *name);
      // Look up by table key, return none if its not present.
      RLMClassInfo* operator[](realm::TableKey const& tableKey);
  
      // Emplaces a locally derived object schema into RLMSchemaInfo. This is used
      // when creating objects dynamically that are not registered in the Cocoa schema.
      // Note: `RLMClassInfo` assumes ownership of `schema`.
      void appendDynamicObjectSchema(std::unique_ptr<realm::ObjectSchema> schema,
                                     RLMObjectSchema *objectSchema,
                                     RLMRealm *const target_realm);
  
      impl::iterator begin() noexcept;
      impl::iterator end() noexcept;
      impl::const_iterator begin() const noexcept;
      impl::const_iterator end() const noexcept;
  
  private:
      std::unordered_map<NSString *, RLMClassInfo> m_objects;
  };
  
  NS_ASSUME_NONNULL_END