jeudi 28 avril 2016

How to write a dispatch function using libprocess library in c++

#include <iostream>
#include <sstream>
#include <process/defer.hpp>
#include <process/dispatch.hpp>
#include <process/future.hpp>
#include <process/http.hpp>
#include <process/process.hpp>

using namespace std;
using namespace process;
using namespace process::http;
using std::string;

class SimpleProcess : public process::Process<SimpleProcess>
{
public:
  Future<Nothing> doSomething(const std::string msg)
  {
    std::cout << "Wrapping message: " << msg << std::endl;
    return Nothing();
  }
  Future<int> calc(int lhs, int rhs)
  {
    return Promise<int>(lhs + rhs).future();
  }
private:
  Promise<int> p;
};

int main(int argc, char **argv)
{
  SimpleProcess simpleProcess;
  Promise<int> p;
  process::PID<SimpleProcess> pid = process::spawn(&simpleProcess);
  process::dispatch(pid, &SimpleProcess::doSomething, "test test test");
  process::dispatch(pid, &SimpleProcess::calc, 99, 101)
  .then([+ , &p](int i , int j) {
    p.set( i + j);
    return p.future();
  });
return 0;

Aucun commentaire:

Enregistrer un commentaire