Porady: zapewnianie funkcji pracy dla wywoływania oraz klasy transformatora
W tym temacie przedstawiono kilka sposobów, aby zapewnić funkcje pracy do concurrency::call i concurrency::transformer klas.
Pierwszy przykład przedstawia sposób przekazać Wyrażenie lambda, aby call obiektu.Drugi przykład przedstawia sposób przekazać obiekt funkcji do call obiektu.W trzecim przykładzie przedstawiono sposób powiązać metody klasy do call obiektu.
Dla celów ilustracyjnych, co w tym temacie przykładzie call klasy.Na przykład, która korzysta z transformer klasy, zobacz Porady: używanie transformatora w potoku danych.
Przykład
W poniższym przykładzie pokazano typowy sposób użyć call klasy.W tym przykładzie przekazuje do funkcji lambda call Konstruktor.
// 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;
}
Ten przykład generuje następujące wyniki.
Poniższy przykład podobny poprzedniego, chyba że używa call klasy wraz z obiekt funkcji (functor).
// 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;
}
Poniższy przykład podobny poprzedniego, chyba że używa std::bind1st i std::mem_fun funkcje powiązać call obiektu do metody klasy.
Użyj tej techniki, jeśli masz do wiązania call lub transformer obiektu do metody określonej klasy zamiast operator wywołania funkcji 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;
}
Można także przypisać wynik bind1st funkcji std::function obiektu lub użyj auto słowa kluczowego, jak pokazano w następującym przykładzie.
// 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);
Kompilowanie kodu
Skopiuj przykładowy kod i wklej go w projekcie programu Visual Studio lub wkleić go w pliku o nazwie call.cpp , a następnie uruchomić następujące polecenie w oknie wiersza polecenia programu Visual Studio.
cl.exe /EHsc call.cpp
Zobacz też
Zadania
Porady: używanie transformatora w potoku danych