daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- language: lang-cc -->
  2. #include "utilities.h"
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <tuple>
  6.  
  7. // https://stackoverflow.com/questions/42255534 (Yakk)
  8. namespace notstd {
  9.     template<class T> struct tag_t { constexpr tag_t() {}; using type=T; };
  10.     template<class T> constexpr tag_t<T> tag{};
  11.     template<class Tag> using type_t = typename Tag::type;
  12.  
  13.     template<class...Ts, class F>
  14.     void for_each_type(F&& f) {
  15.         using discard=int[];
  16.         (void)discard{ 0,(void(
  17.             f( tag<Ts> )
  18.         ),0)...};
  19.     }
  20. }
  21.  
  22. // A component
  23. class icomponent {
  24. public:
  25.     virtual std::string id() = 0;
  26.     virtual ~icomponent() = default;
  27.  
  28.     virtual void init() = 0;
  29.     virtual void dispose() = 0;
  30. };
  31.  
  32. class component_base : public icomponent
  33. {  
  34. public:
  35.     virtual ~component_base() = default;
  36.  
  37.     virtual void init()
  38.     {
  39.         // ... init context
  40.     }
  41.  
  42.     virtual void dispose()
  43.     {
  44.         // ...
  45.     }
  46.  
  47.     // ... more
  48. };
  49.  
  50. // Sample components
  51. class component_a : public component_base {
  52. public:    
  53.     virtual std::string id() override { return "component a"; }
  54. };
  55. class component_b : public component_base {
  56. public:    
  57.     virtual std::string id() override { return "component b"; }
  58. };
  59. class component_c : public component_base {
  60. public:    
  61.     virtual std::string id() override { return "component c"; }
  62. };
  63.  
  64. // Interface component manager
  65. class iprocessing_component_manager {
  66. public:
  67.     virtual ~iprocessing_component_manager() = default;
  68.  
  69.     virtual void init() = 0;
  70.     virtual icomponent* prepare() = 0;
  71.     virtual void recycle(icomponent* p) = 0;
  72.     virtual void dispose() = 0;
  73. };
  74.  
  75. // Implementation component manager
  76. template<typename T>
  77. class  type_processing_component_manager
  78.     : public iprocessing_component_manager {
  79. public:
  80.     virtual ~type_processing_component_manager() = default;
  81.  
  82.     virtual T* prepare() override
  83.     {
  84.         // Default create T or fetch from a object pool, etc ...
  85.         return new T;
  86.     }
  87. };
  88.  
  89. // Implementation virt. methods component mgr
  90. template<typename ... Ts>
  91. class tuple_processing_component_manager
  92.     : public type_processing_component_manager<Ts>... {
  93. public:
  94.     virtual ~tuple_processing_component_manager() = default;
  95.  
  96.     virtual void init() override
  97.     {
  98.  
  99.     }
  100.  
  101.     template<typename T>
  102.     T* prepare()
  103.     {
  104.         return type_processing_component_manager<T>::prepare();
  105.     }
  106.  
  107.     virtual void recycle(icomponent* p) override
  108.     {            
  109.         // Delete pointer or return to an object pool, etc
  110.         delete p;
  111.     }
  112.  
  113.     virtual void dispose() override
  114.     {
  115.  
  116.     }
  117. };
  118.  
  119. // The controller
  120. template <typename ...Ts>
  121. class controller {
  122.     std::unique_ptr<tuple_processing_component_manager<Ts...>> m_component_manager;
  123. //    iprocessing_component_manager* m_component_manager;
  124.  
  125. public:
  126.     controller()
  127.     : m_component_manager(std::make_unique<tuple_processing_component_manager<Ts...>>())
  128. //    : m_component_manager(new tuple_processing_component_manager<Ts...>())
  129.     {
  130.     }
  131.     ~controller() = default;
  132.  
  133.     // Do some initialization
  134.     void init()
  135.     {
  136.         m_component_manager->init();
  137.     }
  138.  
  139.     // Process components
  140.     void process()
  141.     {
  142.         // A simple loop over components.
  143.         notstd::for_each_type<Ts...>([&](auto tag) {
  144.             using component_t = notstd::type_t<decltype(tag)>;
  145.  
  146.             component_t* x = m_component_manager->template prepare<component_t>();
  147.             // Do some processing, here I just print the component id
  148.             std::cout << x->id() << "n";
  149.  
  150.             // Recycle.
  151.             m_component_manager->recycle(x);
  152.         });
  153.     }
  154.  
  155.     // ... more stuff
  156. };
  157.    
  158. <!-- language: lang-cc -->
  159. int main(int argc, char** argv)
  160. {
  161.     controller<component_a, component_c> c;
  162.     c.init();
  163.     c.process();
  164.  
  165.     return 0;
  166. }
  167.    
  168. <!-- language: lang-cc -->
  169. std::unique_ptr<tuple_processing_component_manager<Ts...>> m_component_manager = std::make_unique<tuple_processing_component_manager<Ts...>>();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top