方法: call クラスおよび transformer クラスに処理関数を提供する

ここでは、Concurrency::call クラスおよび Concurrency::transformer クラスに処理関数を提供するいくつかの方法について説明します。

最初の例では、ラムダ式を call オブジェクトに渡す方法を示します。 2 番目の例では、関数オブジェクトを call オブジェクトに渡す方法を示します。 3 番目の例では、クラス メソッドを 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 オブジェクトをクラス メソッドにバインドする点が異なります。

この方法は、関数呼び出し演算子 operator() ではなく、特定のクラス メソッドに call オブジェクトまたは transformer オブジェクトをバインドする必要がある場合に使用します。

// 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::ffunction オブジェクトに割り当てるか、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 クラス

概念

非同期エージェント ライブラリ

非同期メッセージ ブロック

その他の技術情報

方法: データ パイプラインでトランスフォーマーを使用する