Обратные вызовы в C++ | страница 42



, которое возвращает тип объявленной переменной (см. Листинг 42).

Листинг 42.Инстанциирование шаблона асинхронного обратного вызова для лямбда-выражения

>int capture = 10;

>auto lambda = [capture](int eventID) {/*this is a body of lambda*/};

>Initiator callbackLambda1 (lambda); // Ok, initialization in constructor

>Initiator callbackLambda = lambda; // Ok, implicit constructor call

>Initiator callbackLambda2;  //Error: attempting to reference a deleted function

>callbackLambda.setup(lambda);  //Error:  ‘operator’ =  attempting to reference a deleted function

>callbackLambda.run();

4.4.3. Исполнитель

В Листинг 43 приведены примеры реализации исполнителя для различных типов аргументов. Объявления класса CallbackConverter представлены в Листинг 27 и Листинг 28 п. 4.2.2, инициатор используется из Листинг 41 п. 4.4.2.

Листинг 43. Исполнитель для шаблона-инициатора с различными типами аргумента

>class Executor  // (1)

>{

>public:

>  static void staticCallbackHandler(int eventID, Executor* executor) {}

>  void callbackHandler(int eventID) {}

>  void operator() (int eventID) {}

>};


>void ExternalHandler(int eventID, void* somePointer) {}  // (2)


>int main()

>{

>  Executor executor;  // (3)

>  int capturedValue = 0;


>  // (4)  Pointer to the external function

>  using PtrExtFunc = void(*) (int, void*);                                 // (5)

>  using CallbackExtFunction = CallbackConverter;        // (6)

>  Initiator initExtFunction;                          // (7)

>  initExtFunction.setup(CallbackExtFunction(ExternalHandler, &executor));  // (8)


>  // (9) Pointer to the static method

>  using PtrStaticMethod = void(*) (int, Executor*);  // (10)

>  using CallbacStaticMethod = CallbackConverter;                // (11)

>  Initiator initStaticMethod;                                          // (12)

>  initStaticMethod.setup(CallbacStaticMethod(Executor::staticCallbackHandler, &executor));  // (13)


>  // (14) Pointer to the class member method

>  using PtrMethod = void(Executor::*)(int);                                             // (15)

>  using CallbackMemberMethod = CallbackConverter;     // (16)

>  Initiator initMemberMethod;  // (17)

>  initMemberMethod.setup(CallbackMemberMethod(&executor, &Executor::callbackHandler));  // (18)


>  // (19) Functional object