1 #if !defined(FLOW_GENERIC_H)
8 #include <condition_variable>
25 namespace flow {
namespace samples {
namespace generic {
31 std::function<T ()> d_gen_f;
33 std::condition_variable d_awaken_cv;
34 std::mutex d_awaken_m;
41 generator(
timer& timer_r,
const std::function<T ()>& gen_f,
const std::string& name_r =
"generator") :
node(name_r),
producer<T>(name_r, 1), d_gen_f(gen_f), d_awaken(false)
51 std::unique_lock<std::mutex> ul(d_awaken_m);
53 d_awaken_cv.notify_one();
59 std::unique_lock<std::mutex> ul(d_awaken_m);
61 d_awaken_cv.notify_one();
71 std::unique_lock<std::mutex> ul(d_awaken_m);
72 d_awaken_cv.wait(ul, std::bind(std::equal_to<bool>(), std::ref(d_awaken),
true));
78 std::unique_ptr<packet<T>> packet_p(
new packet<T>(d_gen_f()));
91 std::condition_variable d_stopped_cv;
92 std::mutex d_stopped_m;
99 ostreamer(std::ostream& o_r,
const std::string& name_r =
"ostreamer") :
node(name_r),
consumer<T>(name_r, 1), d_o_r(o_r) {}
106 d_stopped_cv.notify_one();
122 d_o_r << packet_p->data() << std::endl;
124 else if(packet_p->consumption_time() > std::chrono::high_resolution_clock::now())
128 std::unique_lock<std::mutex> l_stopped(d_stopped_m);
129 d_stopped_cv.wait_until(l_stopped, packet_p->consumption_time());
133 d_o_r << packet_p->data() << std::endl;
161 std::unique_ptr<packet<T>> copy_p(
new packet<T>(*packet_p));
173 std::chrono::milliseconds d_offset;
178 template<
typename Duration>
179 delay(
const Duration& offset_r,
const std::string& name_r =
"delay") :
node(name_r),
transformer<T, T>(name_r, 1, 1), d_offset(offset_r) {}
192 packet_p->consumption_time() = std::chrono::high_resolution_clock::now() + d_offset;
196 packet_p->consumption_time() += d_offset;