Duncan Tourolle 2bca2a7554
All checks were successful
🧪 Test / test (push) Successful in 6m4s
Add static network
2026-05-08 20:00:15 +02:00

56 lines
1.8 KiB
C++

// 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<string,2>
// is inserted automatically — the user just writes two edges from the same port.
//
// +--> [print_key]
// [generate] --string--> [fan]
// +--> [print_upper]
//
// The FanoutNode<string,2> is owned by the StaticNetwork and invisible to the user.
#include <kpn/kpn.hpp>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
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<generate, "gen" >(5);
auto lower = make_node<print_lower, "lower">(5);
auto upper = make_node<print_upper, "upper">(5);
// Two edges from gen.output<0>() — make_network() detects the fan-out
// and inserts FanoutNode<std::string, 2> 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();
}