coroutine etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
coroutine etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

6 Ekim 2017 Cuma

coroutine pull_type Sınıfı

Giriş
Coroutine2 kütüphanesi Coroutine1'e tercih edilmeli
Version 1
Constructor
Şöyle yaparız.
// example.cpp

#include <boost/coroutine/all.hpp>
#include <iostream>
using namespace boost::coroutines;

void mycorofunc(coroutine<void>::push_type &sink){
  std::cout << "1";
  sink();
  std::cout << " 3";
}

int main() {
  coroutine<void>::pull_type source{mycorofunc};
  std::cout << " 2";
  source();
  std::cout << " 4!" << std::endl;
}
Version 2
Şu satırı dahil ederiz.
#include <boost/coroutine2/all.hpp>
Tanımlama
Şöyle yaparız. Sink ve pull_type long,long long double ve string nesneleri alabilir.
using V    = boost::variant<long, long long, double, std::string>;
using Coro = boost::coroutines2::coroutine<V>;
Constructor - Sink'i dolduran metodu ver
Şöyle yaparız.
class worker {
public:
  void import_data(Coro::push_type &sink) {
    sink(stol(fieldbuffer));
    sink(stod(fieldbuffer));
    sink(fieldbuffer); // Fieldbuffer is a std::string
  }

  std::string fieldbuffer = "+042.42";
};

Coro::pull_type _fieldloader(boost::bind(&worker::import_data, &w, _1));
Constructor - lambda 
Sink'i dolduran metod lambda olabilir. Şöyle yaparız.
using coro_t = boost::coroutines2::coroutine<bool>;

coro_t::pull_type source([](coro_t::push_type& sink) {
  ...
});
get metodu
Şöyle yaparız.
while (_fieldloader) {
  std::cout << _fieldloader.get() << "\n";
  _fieldloader();
}




19 Eylül 2017 Salı

coroutine push_type Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <boost/coroutine2/all.hpp>
Tanımlama
Şöyle yaparız.
using Coroutine_t = boost::coroutines2::coroutine<int>::push_type;
operator() metodu
Örnek
Şöyle yaparız.
#include <boost/coroutine2/all.hpp>

#include <iostream>
#include <cassert>

int main() {
  auto sum = 0;
  using Coroutine_t = boost::coroutines2::coroutine<int>::push_type;
  auto coro = Coroutine_t{[&](auto& yield) {
    for (;;) {
      auto val = yield.get();
      std::cout << "Currently " << val << std::endl;
      sum += val;
      yield(); // jump back to starting context
    }
  }};

  std::cout << "Transferring 1" << std::endl;
  coro(1); // transfer {1} to coroutine-function
  std::cout << "Transferring 2" << std::endl;
  coro(2); // transfer {1} to coroutine-function

}

29 Eylül 2016 Perşembe

coroutine symmetric_coroutine Sınıfı

call_type Tipi
Constructor
Şöyle yaparız.
boost::coroutines::symmetric_coroutine<int>::call_type coro (
  [&](boost::coroutines::symmetric_coroutine<int>::yield_type& yield)
  {
    ...
  }
);
operator () metodu
Şöyle yaparız.
coro (3);
yield_type Tipi
get metodu
Şöyle yaparız.
boost::coroutines::symmetric_coroutine<int>::yield_type yield = ...
std::cout << yield.get() << std::endl;
operator () metodu
Şöyle yaparız.
boost::coroutines::symmetric_coroutine<int>::yield_type& yield = ...;
yield ();