Connecting processing blocks
Generate usage scenario:
Filter* source;
Filter* sink;
// connect source's output to sink's input
source->GetPort("output")->Connect(sink->GetPort("input"));
// main loop
while (true) {
// data output from source will be sent to sink for processing automatically
source->Process();
}A more "practical" example, in which a block's output port can be connected to 2 other input blocks:
#include "filter_graph.h"
// A block that generates random integers.
class IntegerGenerator : public filter_graph::Filter {
public:
IntegerGenerator() : rand_out_("rand_out") {
AddOutput(&rand_out_);
}
protected:
void OnProcess() {
int number = rand();
rand_out_.Write(number, sizeof(int));
}
private:
Port rand_out;
};
// A block that consumes an integer and print out to standard output.
class IntegerPrinter : public filter_graph::Filter {
public:
IntegerPrinter() : integer_in_("integer_in") {
AddInput(&integer_in_);
}
protected:
void OnProcess() {
int* number;
integer_in_.Read(&number);
cout << *number << endl;
}
private:
Port integer_in_;
};
// A block that consumes an integer and write to log file.
class IntegerLogger : public filter_graph::Filter {
public:
IntegerLogger() : integer_in_("integer_in") {
AddInput(&integer_in_);
}
protected:
void OnProcess() {
int* number;
integer_in_.Read(&number);
fwrite(number, sizeof(int), 1, log_file_);
}
private:
Port integer_in_;
};
// set up of the filter graph
IntegerGenerator generator;
IntegerPrinter printer;
IntegerLogger logger;
generator["rand_out"]->Connect(printer["integer_in"]);
generator["rand_out"]->Connect(logger["integer_in"]);
// run the graph 50 iterations
for (int i=0; i<50; ++i)
generator.Process(); // random integers generated by the generator block
// will be printed to the screen and written to log
// file at the same time.