RLMClassInfo.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 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. #import <Foundation/Foundation.h>
  19. #import <unordered_map>
  20. #import <vector>
  21. namespace realm {
  22. class ObjectSchema;
  23. class Schema;
  24. class Table;
  25. struct Property;
  26. }
  27. class RLMObservationInfo;
  28. @class RLMRealm, RLMSchema, RLMObjectSchema, RLMProperty;
  29. NS_ASSUME_NONNULL_BEGIN
  30. namespace std {
  31. // Add specializations so that NSString can be used as the key for hash containers
  32. template<> struct hash<NSString *> {
  33. size_t operator()(__unsafe_unretained NSString *const str) const {
  34. return [str hash];
  35. }
  36. };
  37. template<> struct equal_to<NSString *> {
  38. bool operator()(__unsafe_unretained NSString * lhs, __unsafe_unretained NSString *rhs) const {
  39. return [lhs isEqualToString:rhs];
  40. }
  41. };
  42. }
  43. // The per-RLMRealm object schema information which stores the cached table
  44. // reference, handles table column lookups, and tracks observed objects
  45. class RLMClassInfo {
  46. public:
  47. RLMClassInfo(RLMRealm *, RLMObjectSchema *, const realm::ObjectSchema *);
  48. __unsafe_unretained RLMRealm *const realm;
  49. __unsafe_unretained RLMObjectSchema *const rlmObjectSchema;
  50. const realm::ObjectSchema *const objectSchema;
  51. // Storage for the functionality in RLMObservation for handling indirect
  52. // changes to KVO-observed things
  53. std::vector<RLMObservationInfo *> observedObjects;
  54. // Get the table for this object type. Will return nullptr only if it's a
  55. // read-only Realm that is missing the table entirely.
  56. realm::Table *_Nullable table() const;
  57. // Get the RLMProperty for a given table column, or `nil` if it is a column
  58. // not used by the current schema
  59. RLMProperty *_Nullable propertyForTableColumn(NSUInteger) const noexcept;
  60. // Get the RLMProperty that's used as the primary key, or `nil` if there is
  61. // no primary key for the current schema
  62. RLMProperty *_Nullable propertyForPrimaryKey() const noexcept;
  63. // Get the table column for the given property. The property must be a valid
  64. // persisted property.
  65. NSUInteger tableColumn(NSString *propertyName) const;
  66. NSUInteger tableColumn(RLMProperty *property) const;
  67. // Get the info for the target of the link at the given property index.
  68. RLMClassInfo &linkTargetType(size_t propertyIndex);
  69. // Get the info for the target of the given property
  70. RLMClassInfo &linkTargetType(realm::Property const& property);
  71. void releaseTable() { m_table = nullptr; }
  72. private:
  73. mutable realm::Table *_Nullable m_table = nullptr;
  74. std::vector<RLMClassInfo *> m_linkTargets;
  75. };
  76. // A per-RLMRealm object schema map which stores RLMClassInfo keyed on the name
  77. class RLMSchemaInfo {
  78. using impl = std::unordered_map<NSString *, RLMClassInfo>;
  79. public:
  80. RLMSchemaInfo() = default;
  81. RLMSchemaInfo(RLMRealm *realm);
  82. RLMSchemaInfo clone(realm::Schema const& source_schema, RLMRealm *target_realm);
  83. // Look up by name, throwing if it's not present
  84. RLMClassInfo& operator[](NSString *name);
  85. impl::iterator begin() noexcept;
  86. impl::iterator end() noexcept;
  87. impl::const_iterator begin() const noexcept;
  88. impl::const_iterator end() const noexcept;
  89. private:
  90. std::unordered_map<NSString *, RLMClassInfo> m_objects;
  91. };
  92. NS_ASSUME_NONNULL_END