Guest User

Untitled

a guest
Jul 27th, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <functional>
  2. #include <iostream>
  3. #include <string>
  4. #include <memory>
  5.  
  6. //
  7. // Basic types
  8. //
  9. class dispatcher_t
  10. {
  11. public :
  12.    virtual ~dispatcher_t()
  13.    {}
  14.  
  15.    virtual void
  16.    say() = 0;
  17. };
  18.  
  19. typedef std::unique_ptr< dispatcher_t > dispatcher_unique_ptr_t;
  20.  
  21. class binder_t
  22. {
  23. public :
  24.    virtual ~binder_t()
  25.    {}
  26.  
  27.    virtual void
  28.    say() = 0;
  29. };
  30.  
  31. typedef std::unique_ptr< binder_t > binder_unique_ptr_t;
  32.  
  33. //
  34. // Basic types implementations.
  35. //
  36.  
  37. class first_dispatcher_t : public dispatcher_t
  38. {
  39. public :
  40.    virtual void
  41.    say() override
  42.    {
  43.       std::cout << "first dispatcher" << std::endl;
  44.    }
  45. };
  46.  
  47. class first_binder_t : public binder_t
  48. {
  49. public :
  50.    virtual void
  51.    say() override
  52.    {
  53.       std::cout << "first binder" << std::endl;
  54.    }
  55. };
  56.  
  57. dispatcher_unique_ptr_t
  58. create_first_dispatcher()
  59. {
  60.    return dispatcher_unique_ptr_t( new first_dispatcher_t() );
  61. }
  62.  
  63. binder_unique_ptr_t
  64. create_first_binder()
  65. {
  66.    return binder_unique_ptr_t( new first_binder_t() );
  67. }
  68.  
  69. class second_dispatcher_t : public dispatcher_t
  70. {
  71. public :
  72.    virtual void
  73.    say() override
  74.    {
  75.       std::cout << "second dispatcher" << std::endl;
  76.    }
  77. };
  78.  
  79. class second_binder_t : public binder_t
  80. {
  81. public :
  82.    virtual void
  83.    say() override
  84.    {
  85.       std::cout << "second binder" << std::endl;
  86.    }
  87. };
  88.  
  89. dispatcher_unique_ptr_t
  90. create_second_dispatcher()
  91. {
  92.    return dispatcher_unique_ptr_t( new second_dispatcher_t() );
  93. }
  94.  
  95. binder_unique_ptr_t
  96. create_second_binder()
  97. {
  98.    return binder_unique_ptr_t( new second_binder_t() );
  99. }
  100.  
  101. //
  102. // Test infrastructure
  103. //
  104.  
  105. struct factories_t
  106. {
  107.    std::function< dispatcher_unique_ptr_t() > m_disp_factory;
  108.    std::function< binder_unique_ptr_t() > m_bind_factory;
  109. };
  110.  
  111. void
  112. test( const factories_t & factories )
  113. {
  114.    std::cout << "creating dispatcher..." << std::flush;
  115.    auto d = factories.m_disp_factory();
  116.    std::cout << "ok" << std::endl;
  117.    d->say();
  118.  
  119.    std::cout << "creating binder..." << std::flush;
  120.    auto b = factories.m_bind_factory();
  121.    std::cout << "ok" << std::endl;
  122.    b->say();
  123. }
  124.  
  125. factories_t
  126. create_factories( const std::string & name )
  127. {
  128.    if( "first" == name )
  129.       return {
  130.          []() { return create_first_dispatcher(); },
  131.          [name]() { return create_first_binder(); }
  132.       };
  133.  
  134.    return {
  135.       []() { return create_second_dispatcher(); },
  136.       []() { return create_second_binder(); }
  137.    };
  138. }
  139.  
  140. int main()
  141. {
  142.    try
  143.    {
  144.       test( create_factories( "second" ) );
  145.       test( create_factories( "first" ) );
  146.  
  147.       return 0;
  148.    }
  149.    catch( const std::exception & x )
  150.    {
  151.       std::cerr << "Exception: " << x.what() << std::endl;
  152.    }
  153.  
  154.    return 2;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment