BBASMAdapterBaseImplement.h 1.54 KB
//
//  BBASMAdapterBaseImplement.h
//  SWAN
//
//  Created by baidu on 2018/11/19.
//  Copyright © 2018 baidu. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Pyramid/Pyramid.h>
#import <objc/runtime.h>

NS_ASSUME_NONNULL_BEGIN

/**
 * 外部集成该类,并实现相应协议,保证协议调取安全
 */
@interface BBASMAdapterBaseImplement : NSObject

#define RegisterBBASMAdapter()\
__attribute__((constructor))\
static void registerBBASMAdapter(void) {\
    NSString *fileName = [[NSString stringWithFormat:@"%s", __FILE__] lastPathComponent];\
    NSArray *fileNameArr = [fileName componentsSeparatedByString:@"."];\
    NSString *adapterImpClassName = nil;\
    if ([fileNameArr count] != 2) {\
        return ;\
    }\
    adapterImpClassName = fileNameArr[0];\
    Class adapterImpClass = NSClassFromString(adapterImpClassName);\
    if (!adapterImpClass) {\
        return ;\
    }\
    u_int protocolCount = 0;\
    Protocol *protocol = nil;\
    __unsafe_unretained Protocol **protocols = class_copyProtocolList(adapterImpClass, &protocolCount);\
    NSString *curPName = nil;\
    for (u_int i = 0; i < protocolCount; i++) {\
        protocol = protocols[i];\
        const char *pName = protocol_getName(protocol);\
        curPName = [NSString stringWithUTF8String:pName];\
        if ([curPName hasSuffix:@"Protocol"]) {\
            break;\
        }\
    }\
    if (protocol) {\
        [Pyramid registerExternService:protocol implClass:adapterImpClass identifier:curPName];\
    }\
    free(protocols);\
}\

@end

NS_ASSUME_NONNULL_END