关联对象

2021-03-10  本文已影响0人  晨阳Xia

关联对象的方式

image.png

关联对象源码基本思路

关联对象的结构:
AssociationsHashManager // AssociationsHashMap
AssociationsHashMap // 类似于字典 <object, ObjectAssociationMap>
ObjectAssociationMap // 类似于字典 <key, ObjectAssociation>
ObjectAssociation // policy value
关联的对象存储在AssociationsHashManager中,AssociationsHashManager包含了AssociationsHashMap,AssociationsHashMap以object为键值,存储了 <object, ObjectAssociationMap>,ObjectAssociationMap以属性为键值,存储了<key, ObjectAssociation>,ObjectAssociation存储了我们想要存储的值,和协议

关联对象的源码解析

image.png

源码:
AssociationsManager

class AssociationsManager {
    using Storage = ExplicitInitDenseMap<DisguisedPtr<objc_object>, ObjectAssociationMap>;
    static Storage _mapStorage;

public:
    AssociationsManager()   { AssociationsManagerLock.lock(); }
    ~AssociationsManager()  { AssociationsManagerLock.unlock(); }

    AssociationsHashMap &get() {
        return _mapStorage.get();
    }

    static void init() {
        _mapStorage.init();
    }
};

AssociationsHashMap

typedef DenseMap<DisguisedPtr<objc_object>, ObjectAssociationMap> AssociationsHashMap;

ObjectAssociationMap

typedef DenseMap<const void *, ObjcAssociation> ObjectAssociationMap;

ObjcAssociation

class ObjcAssociation {
    uintptr_t _policy;
    id _value;
public:
    ObjcAssociation(uintptr_t policy, id value) : _policy(policy), _value(value) {}
    ...
    };

objc_setAssociatedObject 源码:


void
objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
{
   SetAssocHook.get()(object, key, value, policy);
}

static ChainedHookFunction<objc_hook_setAssociatedObject> SetAssocHook{_base_objc_setAssociatedObject};

_object_set_associative_reference(id object, const void *key, id value, uintptr_t policy)
{
   // This code used to work when nil was passed for object and key. Some code
   // probably relies on that to not crash. Check and handle it explicitly.
   // rdar://problem/44094390
   if (!object && !value) return;

   if (object->getIsa()->forbidsAssociatedObjects())
       _objc_fatal("objc_setAssociatedObject called on instance (%p) of class %s which does not allow associated objects", object, object_getClassName(object));

   DisguisedPtr<objc_object> disguised{(objc_object *)object};
   ObjcAssociation association{policy, value};

   // retain the new value (if any) outside the lock.
   association.acquireValue();

   {
       AssociationsManager manager;
       AssociationsHashMap &associations(manager.get());

       if (value) {
           auto refs_result = associations.try_emplace(disguised, ObjectAssociationMap{});
           if (refs_result.second) {
               /* it's the first association we make */
               object->setHasAssociatedObjects();
           }

           /* establish or replace the association */
           auto &refs = refs_result.first->second;
           auto result = refs.try_emplace(key, std::move(association));
           if (!result.second) {
               association.swap(result.first->second);
           }
       } else {
           auto refs_it = associations.find(disguised);
           if (refs_it != associations.end()) {
               auto &refs = refs_it->second;
               auto it = refs.find(key);
               if (it != refs.end()) {
                   association.swap(it->second);
                   refs.erase(it);
                   if (refs.size() == 0) {
                       associations.erase(refs_it);

                   }
               }
           }
       }
   }

   // release the old value (outside of the lock).
   association.releaseHeldValue();
}

objc_getAssociatedObject源码:

id
objc_getAssociatedObject(id object, const void *key)
{
    return _object_get_associative_reference(object, key);
}
_object_get_associative_reference(id object, const void *key)
{
    ObjcAssociation association{};

    {
        AssociationsManager manager;
        AssociationsHashMap &associations(manager.get());
        AssociationsHashMap::iterator i = associations.find((objc_object *)object);
        if (i != associations.end()) {
            ObjectAssociationMap &refs = i->second;
            ObjectAssociationMap::iterator j = refs.find(key);
            if (j != refs.end()) {
                association = j->second;
                association.retainReturnedValue();
            }
        }
    }

    return association.autoreleaseReturnedValue();
}
上一篇 下一篇

猜你喜欢

热点阅读