Практическое руководство. Предоставление рабочих функций классам call и transformer

Этот раздел иллюстрирует несколько способов предоставления рабочих функций для классов Concurrency::call и Concurrency::transformer.

Первый пример демонстрирует, как передать лямбда-выражение объекту call. Второй пример демонстрирует, как передать объект функции объекту call. Третий пример демонстрирует, как привязать метод класса к объекту call.

Для иллюстрации во всех примерах этого раздела используется класс call. Пример, который использует класс transformer, см. в разделе Практическое руководство. Использование преобразователя в конвейере данных.

Пример

В следующем примере показан обычный способ использования класса call. В этом примере конструктору call передается лямбда-функция.

// call-lambda.cpp
// compile with: /EHsc
#include <agents.h>
#include <iostream>

using namespace Concurrency;
using namespace std;

int wmain()
{
   // Stores the result of the computation.
   single_assignment<int> result;

   // Pass a lambda function to a call object that computes the square
   // of its input and then sends the result to the message buffer.
   call<int> c([&](int n) {
      send(result, n * n);
   });

   // Send a message to the call object and print the result.
   send(c, 13);
   wcout << L"13 squared is " << receive(result) << L'.' << endl;
}

После выполнения примера получается следующий результат.

13 squared is 169.

Следующий пример похож на предыдущий, но в нем класс call используется совместно с объектом функции (функтором).

// call-functor.cpp
// compile with: /EHsc
#include <agents.h>
#include <iostream>

using namespace Concurrency;
using namespace std;

// Functor class that computes the square of its input.
class square
{
public:
   explicit square(ITarget<int>& target)
      : _target(target)
   {
   }

   // Function call operator for the functor class.
   void operator()(int n)
   {
      send(_target, n * n);
   }

private:
   ITarget<int>& _target;
};

int wmain()
{
   // Stores the result of the computation.
   single_assignment<int> result;

   // Pass a function object to the call constructor.
   square s(result);
   call<int> c(s);

   // Send a message to the call object and print the result.
   send(c, 13);
   wcout << L"13 squared is " << receive(result) << L'.' << endl;
}

Следующий пример похож на предыдущий, но использует функции std::bind1st и std::mem_fun для привязки объекта call к методу класса.

Используйте этот способ, если требуется привязать объект call или transformer к конкретному методу класса, а не к оператору вызова функции, operator().

// call-method.cpp
// compile with: /EHsc
#include <agents.h>
#include <functional>
#include <iostream>

using namespace Concurrency;
using namespace std;

// Class that computes the square of its input.
class square
{
public:
   explicit square(ITarget<int>& target)
      : _target(target)
   {
   }

   // Method that computes the square of its input.
   void square_value(int n)
   {
      send(_target, n * n);
   }

private:
   ITarget<int>& _target;
};

int wmain()
{
   // Stores the result of the computation.
   single_assignment<int> result;

   // Bind a class method to a call object.
   square s(result);
   call<int> c(bind1st(mem_fun(&square::square_value), &s));

   // Send a message to the call object and print the result.
   send(c, 13);
   wcout << L"13 squared is " << receive(result) << L'.' << endl;
}

Результат функции bind1st также можно назначать объекту std::function или использовать ключевое слово auto, как показано в следующем примере.

// Assign to a function object.
function<void(int)> f1 = bind1st(mem_fun(&square::square_value), &s);
call<int> c1(f1);

// Alternatively, use the auto keyword to have the compiler deduce the type.
auto f2 = bind1st(mem_fun(&square::square_value), &s);
call<int> c2(f2);

Компиляция кода

Скопируйте код примера и вставьте его в проект Visual Studio или в файл с именем call.cpp, затем выполните в окне командной строки Visual Studio 2010 следующую команду.

cl.exe /EHsc call.cpp

См. также

Задачи

Практическое руководство. Использование преобразователя в конвейере данных

Ссылки

Класс call

Класс transformer

Основные понятия

Библиотека асинхронных агентов

Асинхронные блоки сообщений