От: | Barbar1an | ||
Дата: | 12.08.20 07:37 | ||
Оценка: |
template<class L> Subscribe(L lambda)
{
std::function<сигнатура вызова lambdы> f; // типа void(int, int)
std::function<void(сигнатура параметров lambdы)> f; // типа только "int, int"
}
Subscribe([](int, int){});
От: | Voivoid | ||
Дата: | 12.08.20 08:54 | ||
Оценка: |
От: | Barbar1an | ||
Дата: | 12.08.20 11:07 | ||
Оценка: |
От: | YuriV | ||
Дата: | 12.08.20 14:42 | ||
Оценка: |
B>template<class L> Subscribe(L lambda)
B>{
B> std::function<сигнатура вызова lambdы> f; // типа void(int, int)
B> std::function<void(сигнатура параметров lambdы)> f; // типа только "int, int"
B>}
B>Subscribe([](int, int){});
B>
namespace fn {
/**
* \Note Function details description
*/
template<typename Sign, typename R, typename... Args>
struct traits_base {
using signature = Sign;
using is_method = std::is_member_function_pointer<signature>;
using args = std::tuple<Args...>;
using arity = std::integral_constant<unsigned, sizeof...(Args)>;
using result = R;
};
/**
* \Note Function details specializations
*/
template<typename T>
struct traits : traits<decltype(&T::operator())> {};
template<typename R, typename... Args>
struct traits<R(*)(Args...)> : traits_base<R(*)(Args...), R, Args...> {};
template<typename R, typename... Args>
struct traits<R(* const)(Args...)> : traits_base<R(* const)(Args...), R, Args...> {};
#if __cplusplus >= 201703
template<typename R, typename... Args>
struct traits<R(*)(Args...) noexcept> : traits_base<R(*)(Args...), R, Args...> {};
template<typename R, typename... Args>
struct traits<R(* const)(Args...) noexcept> : traits_base<R(* const)(Args...), R, Args...> {};
#endif // __cplusplus >= 201703
template<typename R, typename C, typename... Args>
struct traits<R(C::*)(Args...)> : traits_base<R(C::*)(Args...), R, Args...> {};
template<typename R, typename C, typename... Args>
struct traits<R(C::* const)(Args...)> : traits_base<R(C::* const)(Args...), R, Args...> {};
template<typename R, typename C, typename... Args>
struct traits<R(C::*)(Args...) const> : traits_base<R(C::*)(Args...) const, R, Args...> {};
template<typename R, typename C, typename... Args>
struct traits<R(C::* const)(Args...) const> : traits_base<R(C::* const)(Args...) const, R, Args...> {};
#if __cplusplus >= 201703
template<typename R, typename C, typename... Args>
struct traits<R(C::*)(Args...) noexcept> : traits_base<R(C::*)(Args...), R, Args...> {};
template<typename R, typename C, typename... Args>
struct traits<R(C::* const)(Args...) noexcept> : traits_base<R(C::* const)(Args...), R, Args...> {};
template<typename R, typename C, typename... Args>
struct traits<R(C::*)(Args...) const noexcept> : traits_base<R(C::*)(Args...) const, R, Args...> {};
template<typename R, typename C, typename... Args>
struct traits<R(C::* const)(Args...) const noexcept> : traits_base<R(C::* const)(Args...) const, R, Args...> {};
#endif // __cplusplus >= 201703
} // namespace fn
// use, e.g. std::function<typename fn::traits<L>::signature> f;
От: | _NN_ | www.nemerleweb.com | |
Дата: | 12.08.20 16:12 | ||
Оценка: |
От: | Barbar1an | ||
Дата: | 12.08.20 18:17 | ||
Оценка: |
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 12.08.20 18:25 | ||
Оценка: |
От: | Barbar1an | ||
Дата: | 12.08.20 18:27 | ||
Оценка: |
template<class ...A> struct CActiveDelegate
{
CDelegate<void(A...)> Function;
std::function<void(A...)> Lambda;
EListen Listen;
CActiveDelegate()
{
}
};
template<class X, class Y, typename...A> CActiveDelegate<A...> CreateActiveDelegate(Y* x, void (X::*func)(A...), EListen l)
{
CActiveDelegate<A...> d;
d.Function = fastdelegate<void(A...)>(x, func);
d.Listen = l;
return d;
}
template<class ...L> CActiveDelegate<typename fn::traits<L>::args> CreateLambdaDelegate(L la, EListen l)
{
CActiveDelegate<typename fn::traits<L>::args> d;
d.Lambda = la;
d.Listen = l;
return d;
}
#define FunctionSubscriber(method, l) CreateActiveDelegate(this, &std::remove_pointer<decltype(this)>::type::method, l)
#define LambdaSubscriber(la, l) CreateLambdaDelegate(la, l)
template<class ...A> class CActiveEvent
{
public:
void operator += (CActiveDelegate<A...> & d)
{
Subscribers.push_back(d);
}
protected:
CArray<CActiveDelegate<A...>> Subscribers;
};
class A
{
void AAA(CString &)
{
CActiveEvent<CString &> aaa;
aaa += FunctionSubscriber(AAA, EListen::NormalAll);
aaa += LambdaSubscriber([](CString &){}, EListen::NormalAll);
}
};
От: | T4r4sB | ||
Дата: | 12.08.20 19:18 | ||
Оценка: |
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 13.08.20 00:08 | ||
Оценка: | +1 |
От: | YuriV | ||
Дата: | 13.08.20 07:39 | ||
Оценка: |
B> template<class ...L> CActiveDelegate<typename fn::traits<L>::args> CreateLambdaDelegate(L la, EListen l)
B> {
B> CActiveDelegate<typename fn::traits<L>::args> d;
B> d.Lambda = la;
B> d.Listen = l;
B> return d;
B> }
B>
template<class L> auto CreateLambdaDelegate(L la, EListen l) -> CActiveDelegate<typename fn::traits<L>::signature> {...}
От: | Barbar1an | ||
Дата: | 13.08.20 08:06 | ||
Оценка: |
template<typename S> struct CActiveDelegate
{
fastdelegate::FastDelegate<S> Function;
std::function<S> Lambda;
int Listen;
CActiveDelegate()
{
}
};
template<class X, class Y, typename...A> auto CreateActiveDelegate(Y* x, void (X::*func)(A...), int l)
{
CActiveDelegate<void(A...)> d;
d.Function = fastdelegate::FastDelegate<void(A...)>(x, func);
d.Listen = l;
return d;
}
template<class L> auto CreateLambdaDelegate(L la, int l)
{
CActiveDelegate<typename fn::traits<L>::signature> d;
d.Lambda = la;
d.Listen = l;
return d;
}
#define FunctionSubscriber(method, l) CreateActiveDelegate(this, &std::remove_pointer<decltype(this)>::type::method, l)
#define LambdaSubscriber(la, l) CreateLambdaDelegate(la, l)
template<class ...A> class CActiveEvent
{
public:
void operator += (CActiveDelegate<void(A...)> & d)
{
Subscribers.push_back(d);
}
protected:
std::list<CActiveDelegate<void(A...)>> Subscribers;
};
class A
{
void AAA(std::string &)
{
CActiveEvent<std::string &> aaa;
aaa += FunctionSubscriber(AAA, 1);
aaa += LambdaSubscriber([](std::string &){}, 2);
}
};
От: | YuriV | ||
Дата: | 13.08.20 09:24 | ||
Оценка: |
B> template<typename S> struct CActiveDelegate
B> {
B> fastdelegate::FastDelegate<S> Function;
B> std::function<S> Lambda;
B> int Listen;
B> CActiveDelegate()
B> {
B> }
B> };
B> template<class X, class Y, typename...A> auto CreateActiveDelegate(Y* x, void (X::*func)(A...), int l)
B> {
B> CActiveDelegate<void(A...)> d;
B> d.Function = fastdelegate::FastDelegate<void(A...)>(x, func);
B> d.Listen = l;
B> return d;
B> }
B> template<class L> auto CreateLambdaDelegate(L la, int l)
B> {
B> CActiveDelegate<typename fn::traits<L>::signature> d;
B> d.Lambda = la;
B> d.Listen = l;
B> return d;
B> }
B> #define FunctionSubscriber(method, l) CreateActiveDelegate(this, &std::remove_pointer<decltype(this)>::type::method, l)
B> #define LambdaSubscriber(la, l) CreateLambdaDelegate(la, l)
B> template<class ...A> class CActiveEvent
B> {
B> public:
B> void operator += (CActiveDelegate<void(A...)> & d)
B> {
B> Subscribers.push_back(d);
B> }
B> protected:
B> std::list<CActiveDelegate<void(A...)>> Subscribers;
B> };
B> class A
B> {
B> void AAA(std::string &)
B> {
B> CActiveEvent<std::string &> aaa;
B> aaa += FunctionSubscriber(AAA, 1);
B> aaa += LambdaSubscriber([](std::string &){}, 2);
B> }
B> };
B>
aaa += FunctionSubscriber(AAA, 1);
и скомпилить. Я этим кодом пользуюсь лет 6 наверное.
От: | Barbar1an | ||
Дата: | 13.08.20 09:40 | ||
Оценка: |
aaa += FunctionSubscriber(AAA, 1);
и скомпилить. Я этим кодом пользуюсь лет 6 наверное.От: | YuriV | ||
Дата: | 13.08.20 09:59 | ||
Оценка: |
aaa += FunctionSubscriber(AAA, 1);
и скомпилить. Я этим кодом пользуюсь лет 6 наверное.aaa += FunctionSubscriber(AAA, 1);
.
От: | Barbar1an | ||
Дата: | 13.08.20 10:03 | ||
Оценка: |
закоменчено //aaa += FunctionSubscriber(AAA, 1); | |
1>------ Build started: Project: Console, Configuration: Debug Win32 ------ 1>Console.cpp 1>M:\Projects\Console\Console.cpp(59,33): error C2079: 'CActiveDelegate<std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const>>::Function' uses undefined class 'fastdelegate::FastDelegate<S>' 1> with 1> [ 1> S=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>M:\Projects\Console\Console.cpp(79): message : see reference to class template instantiation 'CActiveDelegate<std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const>>' being compiled 1>M:\Projects\Console\Console.cpp(137): message : see reference to function template instantiation 'auto CreateLambdaDelegate<A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>>(L,int)' being compiled 1> with 1> [ 1> L=A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1093,19): error C2338: std::function does not accept non-function types as template arguments. 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1118): message : see reference to class template instantiation 'std::_Get_function_impl<_Fty>' being compiled 1> with 1> [ 1> _Fty=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>M:\Projects\Console\Console.cpp(60): message : see reference to class template instantiation 'std::function<S>' being compiled 1> with 1> [ 1> S=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1118,51): error C2039: 'type': is not a member of 'std::_Get_function_impl<_Fty>' 1> with 1> [ 1> _Fty=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1118): message : see declaration of 'std::_Get_function_impl<_Fty>' 1> with 1> [ 1> _Fty=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1118,56): error C2504: 'type': base class undefined 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1120,56): error C2039: 'type': is not a member of 'std::_Get_function_impl<_Fty>' 1> with 1> [ 1> _Fty=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1118): message : see declaration of 'std::_Get_function_impl<_Fty>' 1> with 1> [ 1> _Fty=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1120,1): error C2061: syntax error: identifier 'type' 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1132,1): error C2653: '_Mybase': is not a class or namespace name 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1132,1): error C4430: missing type specifier — int assumed. Note: C++ does not support default-int 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1153,1): error C2653: '_Mybase': is not a class or namespace name 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1153,1): error C4430: missing type specifier — int assumed. Note: C++ does not support default-int 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1187,1): error C2653: '_Mybase': is not a class or namespace name 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1187,1): error C4430: missing type specifier — int assumed. Note: C++ does not support default-int 1>M:\Projects\Console\Console.cpp(80,1): error C2679: binary '=': no operator found which takes a right-hand operand of type 'L' (or there is no acceptable conversion) 1> with 1> [ 1> L=A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1203,15): message : could be 'std::function<S> &std::function<S>::operator =(std::nullptr_t) noexcept' 1> with 1> [ 1> S=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1178,15): message : or 'std::function<S> &std::function<S>::operator =(std::function<S> &&) noexcept' 1> with 1> [ 1> S=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\functional(1162,15): message : or 'std::function<S> &std::function<S>::operator =(const std::function<S> &)' 1> with 1> [ 1> S=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1>M:\Projects\Console\Console.cpp(80,1): message : while trying to match the argument list '(std::function<S>, L)' 1> with 1> [ 1> S=std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const> 1> ] 1> and 1> [ 1> L=A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f> 1> ] 1>M:\Projects\Console\Console.cpp(137,1): error C2679: binary '+=': no operator found which takes a right-hand operand of type 'CActiveDelegate<std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const>>' (or there is no acceptable conversion) 1>M:\Projects\Console\Console.cpp(91,9): message : could be 'void CActiveEvent<std::string &>::operator +=(CActiveDelegate<void (std::string &)> &)' 1>M:\Projects\Console\Console.cpp(137,1): message : while trying to match the argument list '(CActiveEvent<std::string &>, CActiveDelegate<std::remove_const<void (__thiscall A::AAA::<lambda_b40f81b86c19d3a041e4fe2a8702035f>::* )(std::string &) const>>)' 1>Done building project "Console.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== | |
От: | YuriV | ||
Дата: | 13.08.20 13:16 | ||
Оценка: | 6 (1) +1 |
using cpp11_signature = R(Args...);
и используй как using sign = typename fn::traits<L>::cpp11_signature;
using fft = std::function<sign>;
event.lambda = fft(std::forward<L>(lambda));
...
. От: | Barbar1an | ||
Дата: | 13.08.20 14:25 | ||
Оценка: |
using cpp11_signature = R(Args...);
и используй как YV>using sign = typename fn::traits<L>::cpp11_signature;
YV>using fft = std::function<sign>;
YV>event.lambda = fft(std::forward<L>(lambda));
YV>...
YV>
. От: | T4r4sB | ||
Дата: | 13.08.20 20:01 | ||
Оценка: | +2 |
От: | Marty | https://www.youtube.com/channel/UChp5PpQ6T4-93HbNF-8vSYg | |
Дата: | 13.08.20 22:38 | ||
Оценка: |