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