当前位置:网站首页>Load Objective-C at runtime
Load Objective-C at runtime
2022-07-19 14:47:00 【Fighting Horse】
When a program is only weakly dependent on a function , You can use dynamic libraries to organize dependent functions , And use the code to load the dynamic library and its functional modules in the main program , This method is called runtime loading .
about C/C++ Language , I believe many people know about runtime loading .
For example Win32 platform :
auto module = LoadLibrary(L"A.dll");
// void test(int)
auto testFunc = (void(*)(int)) GetProcAddress(module, "test");
In the class unix platform :
auto module = dlopen("A.so", RTLD_LAZY); // stay MacOSX On ,A.dylib
// void test(int)
auto testFunc = (void(*)(int)) dlsym(module, "test");
When there are many functions , You usually need to use C++ Class encapsulation , Using pure virtual interface classes can facilitate runtime loading . Just export a function that creates objects .
class StreamBase
{
virtual ~Base() = 0;
virtual Open(int) = 0;
virtual Read(char *, int) = 0;
virtual White(char const *, int) = 0;
virtual Close() = 0;
};
extern "C" StreamBase * CreateStream();When we're in MacOSX platform , Use Objective-C When the function is realized , We can realize runtime loading more conveniently . There is no need to deliberately introduce a pure virtual interface class , There is no need to export a creator function . in fact ,Objective-C What is exported is the class object itself . With class object , Everything will be there .
Let's take an example :
This is a player implementation , It is provided to the main program through the dynamic library
@interface Player : NSObject
+ (void) initialize;
- (instancetype) initWithDisplayLayer: (AVSampleBufferDisplayLayer*) layer;
- (instancetype) initWithImageView: (NSView*) view;
- (int) open: (char const *) url;
- (NSSize) videoSize;
- (int) play;
- (void) stop;
- (void) close;
@endIn the main program , After getting the class object , Everything works as usual
auto module = dlopen("libPlayer.dylib", RTLD_LAZY);
Class cls = (__bridge Class) dlsym(module, "OBJC_CLASS_$_Player");
if (cls == nullptr) {
m_error = -2;
return;
}
Player * player = [cls alloc];
[player initWithImageView: imageView];
Go and have a try !
边栏推荐
猜你喜欢

Cmake learning notes

CF 807 E. Mark and Professor Koro(权值线段树)

敏捷的第一步:把 “迭代” 变为 “冲刺” 开始!

CMAKE学习笔记

中断的分类

Which company is better in data filling and report presentation? Yixin ABI gives you the answer

MySQL read / write separation

Huawei wireless device configuration dynamic load balancing

Database SQL Server

Authing practice | unified management solution for manufacturing identity authentication
随机推荐
手册不全,如何手工刨出TongWeb的監控信息?
Win10 Microsoft Store打不开(开启TLS 1.2)
Redis 与 Mysql 的数据一致性
Encrypt Ogg users
CF 807 E. mark and Professor Koro (weight segment tree)
常见的内置函数、可迭代对象、迭代器对象、异常捕获、异常捕获的用途、生成器对象、模块、绝对导入与相对导入、包的概念、模块
Labview32-bit and 64 bit compatibility
Principle and simple implementation of custom MVC
The first step of agile: turn "iteration" into "sprint" and start!
273. Grading - acwing question bank [DP]
Tongweb production system emergency treatment plan
Matplotlib draw multi line graph (solve the problem that Matplotlib Chinese cannot be displayed)
Classes abstraites et dérivées
TongWeb生产系统应急处理方案
Wechat applet -- wxss template style
MySQL view
Oserror: sndfile library not found solution
Configure spectrum navigation for Huawei wireless devices
慎用TongWeb的热部署功能
CompositionAPI 组件开发范式