1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <tuple>
#include <string>
#include <iostream>
/************* generic interface for map_state ****************************
template < typename STATE_TYPE, typename... VAR_TYPES > struct map_state
{
STATE_TYPE operator() ( VAR_TYPES... variables ) const ;
STATE_TYPE operator() ( const std::tuple< VAR_TYPES... >& variables ) const ;
// TODO : announce types via typedefs
};
****************************************************************************/
struct map_my_state
{
double operator() ( int a, int b, double c, double d, std::string str ) const
{ return a + b + c + d + str.size() ; }
double operator() ( const std::tuple< int, int, double, double, std::string >& args ) const
{
return operator() ( std::get<0>(args), std::get<1>(args), std::get<2>(args),
std::get<3>(args), std::get<4>(args) ) ;
}
// TODO : announce types via typedefs
};
struct map_your_state
{
std::string operator() ( std::size_t n, char c ) const
{ return std::string( n, c ) ; }
std::string operator() ( const std::tuple< std::size_t, char >& args ) const
{ return operator() ( std::get<0>(args), std::get<1>(args) ) ; }
// TODO : announce types via typedefs
};
template < typename MAPPER_TYPE, typename STATE_TYPE > struct router
{
explicit constexpr router( const MAPPER_TYPE& state_mapper )
: map_them(state_mapper) {}
template < typename... VAR_TYPES >
void show_state( std::tuple< VAR_TYPES... >& data ) const
{ std::cout << map_them( data ) << '\n' ; }
template < typename... VAR_TYPES >
void show_state( VAR_TYPES... variables ) const
{ std::cout << map_them( variables... ) << '\n' ; }
const MAPPER_TYPE& mapper() const { return map_them ; }
void mapper( const MAPPER_TYPE& new_mapper ) { map_them = new_mapper ; }
// etc ...
private:
MAPPER_TYPE map_them ;
};
int main()
{
map_my_state my_mapper ;
router< map_my_state, double > my_router(my_mapper) ;
my_router.show_state( 1, 2, 3.9, 4.8, "12345" ) ;
std::tuple< int, int, double, double, std::string >
my_node_data( 14, 23, 1.05, 99.3, "abc" ) ;
my_router.show_state( my_node_data ) ;
map_your_state your_mapper ;
router< map_your_state, std::string > your_router(your_mapper) ;
your_router.show_state( 15, '@' ) ;
}
|