#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct buf_rx
{
using callback_type = std:: function < void ( std:: string ) > ;
void receive( std:: istringstream in)
{
std:: string word;
while ( in >> word)
for ( auto func : _cb)
func( word) ;
}
void register_cb( callback_type cb_func)
{
_cb.push_back ( cb_func) ;
}
private :
std:: vector < callback_type> _cb;
} ;
void string_processor( const std:: string & s)
{
std:: cout << "string_processor: " << s << '\n ' ;
}
struct some_type
{
some_type( ) : _id( id++ ) { }
void process_string( const std:: string & s) {
std:: cout << "some_type (" << _id << "): " << s << '\n ' ;
}
private :
static unsigned id;
unsigned _id;
} ;
unsigned some_type:: id = 0 ;
int main( )
{
some_type a;
some_type b;
buf_rx buffer;
buffer.register_cb ( string_processor) ;
buffer.register_cb ( [ & ] ( std:: string s) { a.process_string ( s) ; } ) ;
buffer.register_cb ( [ & ] ( std:: string s) { b.process_string ( s) ; } ) ;
buffer.receive ( std:: istringstream { "a b c d e" } ) ;
}
I2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHNzdHJlYW0+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDx2ZWN0b3I+CgpzdHJ1Y3QgYnVmX3J4CnsKICAgIHVzaW5nIGNhbGxiYWNrX3R5cGUgPSBzdGQ6OmZ1bmN0aW9uPHZvaWQoc3RkOjpzdHJpbmcpPiA7CgogICAgdm9pZCByZWNlaXZlKHN0ZDo6aXN0cmluZ3N0cmVhbSBpbikKICAgIHsKICAgICAgICBzdGQ6OnN0cmluZyB3b3JkOwogICAgICAgIHdoaWxlIChpbiA+PiB3b3JkKQogICAgICAgICAgICBmb3IgKGF1dG8gZnVuYyA6IF9jYikKICAgICAgICAgICAgICAgIGZ1bmMod29yZCk7CiAgICB9CgogICAgdm9pZCByZWdpc3Rlcl9jYihjYWxsYmFja190eXBlIGNiX2Z1bmMpCiAgICB7CiAgICAgICAgX2NiLnB1c2hfYmFj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