BBASMAdapterBaseImplement.h
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// 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