id
_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();
}
void
_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();
bool isFirstAssociation = false;
{
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 */
isFirstAssociation = true;
}
/* 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);
}
}
}
}
}
// Call setHasAssociatedObjects outside the lock, since this
// will call the object's _noteAssociatedObjects method if it
// has one, and this may trigger +initialize which might do
// arbitrary stuff, including setting more associated objects.
if (isFirstAssociation)
object->setHasAssociatedObjects();
// release the old value (outside of the lock).
association.releaseHeldValue();
}
關(guān)聯(lián)對(duì)象:設(shè)置流程
1:創(chuàng)建一個(gè)AssociationsManager 管理類
2:獲取唯一的全局靜態(tài)哈希Map
3:判斷是否插入的關(guān)鍵值是否存在:
3.1存在走第四步
3.2 不存在就走:關(guān)聯(lián)對(duì)象插入空流程
4:創(chuàng)建一個(gè)空的 ObjectAssociationMap 去取查詢的鍵值對(duì)
5:如果發(fā)現(xiàn)沒(méi)有這個(gè)key就插入一個(gè)空的 BucketT 進(jìn)去返回
6:標(biāo)記對(duì)象存在關(guān)聯(lián)對(duì)象
7:用當(dāng)前修飾策略 和值 組成了一個(gè) ObjectAssociation 替換原來(lái)的 BucketT中的空
8:標(biāo)記一下ObjectAssociationMap的第一次為false
關(guān)聯(lián)對(duì)象插入空流程
1:根據(jù)DisguisedPtr 找到AssociationHashMap 中的iterator 迭代查詢器
2:清理迭代器
3:其實(shí)如果插入空置 相當(dāng)于清除
關(guān)聯(lián)對(duì)象:取值流程
1:創(chuàng)建一個(gè)AssociationsManager 管理類
2:獲取唯一的全局靜態(tài)哈希 map
3:根據(jù)DisguisedPtr 找到AssociationHashMap 中的iterator 迭代查詢器
4:如果這個(gè)迭代查詢器不是最后一個(gè) 獲取 ObjectAssociationMap(這里有策略和值)
5:找到AssociationHashMap的迭代查詢器獲取一個(gè)經(jīng)過(guò)屬性修飾符修飾的value
6:返回_value
總結(jié):倆層哈希map,存取的時(shí)候倆層處理(類似二位數(shù)組)