美文网首页
iOS原理之Block的__block(修饰基本数据类型)的实现

iOS原理之Block的__block(修饰基本数据类型)的实现

作者: yqc5521 | 来源:发表于2019-05-19 12:52 被阅读0次
    //
    //  BlockBasement__BlockViewController.swift
    //  iOS底层
    //
    //  Created by yanqunchao on 2019/5/19.
    //  Copyright © 2019 yanqunchao. All rights reserved.
    //
    
    import UIKit
    
    class BlockBasement__BlockViewController: UIViewController {
    
        typealias FuncPt = ((MainBlockImpl0)->())
        typealias CopyFuncPt = ((MainBlockImpl0,MainBlockImpl0)->()) 
        typealias DisposeFuncPt = ((MainBlockImpl0)->())
        
        
        /*
         * __block修饰的变量的包装类
         **/
        
        class __BlockByrefAge0: NSObject {
            var isa:UnsafeRawPointer?
            var __forwarding:__BlockByrefAge0?
            var flags:Int?
            var size:Int?
            var age:Int?
            
            override init() {
                
            }
            
            init(isa:UnsafeRawPointer?,flags:Int,size:Int,age:Int) {
                super.init()
                self.isa = isa
                self.__forwarding = self
                self.flags = flags
                self.size = size
                self.age = age
            }
        }
        
        /*
         * 函数实现类
         **/
        
        class BlockImpl: NSObject {
            var isa:UnsafeRawPointer?
            var Flags:Int?
            var Reserved:Int?
            var FuncPtr:FuncPt? //函数地址,
        }
        /*
         * 访问对象类型局部变量的Desc类
         **/
        
        class MainBlockDesc0: NSObject {
            var reserved:Int?
            var BlockSize:Int?
            var copy:CopyFuncPt?
            var dispose:DisposeFuncPt?
            
            init(reserved:Int,BlockSize:Int,copy:@escaping CopyFuncPt,dipose:@escaping DisposeFuncPt) {
                self.reserved = reserved
                self.BlockSize = BlockSize
                self.copy = copy
                self.dispose = dipose
            }
            override init() {
                
            }
            
        }
        
        
        /*
         * 访问auto局部变量(对象类型)的block类
         **/
        
        class MainBlockImpl0: NSObject {
            var imp: BlockImpl?
            var Desc: MainBlockDesc0?
            var age: __BlockByrefAge0?
            init(fp:@escaping FuncPt,Desc:MainBlockDesc0,flag:Int, age:__BlockByrefAge0) {
                imp = BlockImpl()
    //            imp!.isa = &_NSConcreateStackBlock
                imp?.Flags = 0
                imp?.FuncPtr = fp
                self.Desc = Desc
                self.age = age
            }
            override init() {
                
            }
        }
        
        /*
         *block调用的函数地址
         **/
        
        func mainBlockFunc0(block:MainBlockImpl0){
            let byrefAge = block.age
            byrefAge?.__forwarding!.age = 30
        }
        /*
         *当block调用copy操作的时候,就会调用此函数
         **/
        func mainBlockCopy0(destBlock:MainBlockImpl0,srcBlock:MainBlockImpl0){
            blockObjectAssign(destObjc: destBlock.age!, srcObjc: srcBlock.age!, type: 8) //8表示 BLOCK_FIELD_IS_BYREF
        }
        /*
         * 依据传入的srcObjc的类型,决定是否对其强引用.
         * 如果是strong,就会对其进行强引用(类似于retainCount + 1 的操作),如果是weak,就会对其弱引用。
         **/
        func blockObjectAssign(destObjc:AnyObject,srcObjc:AnyObject,type:Int){
            //            if srcObjc 为强引用 {
            //
            //            }else{
            //
            //            }
        }
        /*
         *当block从堆中移除,就会调用此函数
         **/
        func mainBlockDispose0(srcBlock:MainBlockImpl0){
            blockObjectDispose(srcObjc: srcBlock.age!, type: 3)
        }
        /*
         *会自动释放引用的auto变量,类似于 retainCount - 1 的操作
         **/
        func blockObjectDispose(srcObjc:AnyObject,type:Int){
            
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = .white 
            blockWith__BlockLocalVar()
        }
        func blockWith__BlockLocalVar(){ // // 修改局部变量的block
            
            let age = __BlockByrefAge0(isa: UnsafeRawPointer(bitPattern: 0), flags: 33554432, size: MemoryLayout.size(ofValue: __BlockByrefAge0.self), age: 10)
        
            let mainBlockDesc0Data = MainBlockDesc0(reserved: 0, BlockSize: MemoryLayout.size(ofValue: MainBlockImpl0.self), copy: mainBlockCopy0,dipose: mainBlockDispose0)
            
            let block = MainBlockImpl0(fp: mainBlockFunc0, Desc: mainBlockDesc0Data,flag: 0, age: age)
            
            block.imp?.FuncPtr!(block)
            
            print(age.age!)
           
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS原理之Block的__block(修饰基本数据类型)的实现

          本文链接:https://www.haomeiwen.com/subject/rqafzqtx.html