// Example 11 — Static Fan-Out (automatic FanoutNode insertion) // // Two consumers read from the same output port of [generate]. With the runtime // Network builder this would require an explicit make_fanout<>. With make_network() // the duplicate source port is detected at compile time and a FanoutNode // is inserted automatically — the user just writes two edges from the same port. // // +--> [print_key] // [generate] --string--> [fan] // +--> [print_upper] // // The FanoutNode is owned by the StaticNetwork and invisible to the user. #include #include #include #include #include #include static int gen_index = 0; static std::string generate() { static const char* words[] = {"hello", "kpn", "fanout", "static", "network"}; std::this_thread::sleep_for(std::chrono::milliseconds(80)); return words[gen_index++ % 5]; } static void print_lower(std::string s) { std::cout << "lower: " << s << '\n'; } static void print_upper(std::string s) { std::transform(s.begin(), s.end(), s.begin(), ::toupper); std::cout << "upper: " << s << '\n'; } int main() { using namespace kpn; auto gen = make_node(5); auto lower = make_node(5); auto upper = make_node(5); // Two edges from gen.output<0>() — make_network() detects the fan-out // and inserts FanoutNode automatically. auto net = make_network( edge(gen.output<0>(), lower.input<0>()), edge(gen.output<0>(), upper.input<0>()) ); net.start(); std::this_thread::sleep_for(std::chrono::milliseconds(500)); net.stop(); }