Open yanminhui opened 1 year ago
Likely, yes, but TDLib is written in C++14, hence we can't use std::apply and other minor features used in the PoC code.
Also, what benefits do you try to achieve by merging the classes?
LambdaEvent
can be replaced with ClosureEvent
as well. Using lambda and member function pointer the same way.
int a = 1;
int b = 2;
const Base* obj = new Derive{};
auto c = MakeImmediateClosure(&Derive::add, a, b);
std::cout << "run memptr: " << c.run(obj) << std::endl;
delete obj;
auto c2 = MakeImmediateClosure([](int a, int b) { return a + b + 4; }, a, b);
std::cout << "run lambda: " << c2.run(obj) << std::endl; // or c2.run(), obj is ignored.
It is definitely possible to achieve the same behavior in many ways. But is there reason to change the current implementation?
It is for simplify logic code, and usage consistency for lambda and member pointer function only.
In addition, the events that send to actor is mostly custom events (i.e. member pointer functor), it can improve performance like std::function
by allocate object on the stack.
_FunAlloc __af(__a);
if (__use_small_storage<_Fun>())
{
::new ((void*)&__buf_.__small)
_Fun(_VSTD::move(__f), _Alloc(__af));
}
else
{
typedef __allocator_destructor<_FunAlloc> _Dp;
unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
::new ((void*)__hold.get())
_Fun(_VSTD::move(__f), _Alloc(__af));
__buf_.__large = __hold.release();
}
https://github.com/tdlib/td/blob/70bee089d492437ce931aa78446d89af3da182fc/tdutils/td/utils/Closure.h#L56-L118
It seems
ImmediateClosure
andDelayedClosure
can merge to one class, for example (see Insights):