Advertisement
Guest User

blablabla

a guest
Jul 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. namespace T2
  2. {
  3.     struct Device
  4.     {
  5.         Device(const std::string& name) : name(name) {}
  6.         std::string name;
  7.     };
  8.  
  9.     struct Interface
  10.     {
  11.         Interface(int pin) {}
  12.  
  13.         virtual void write()
  14.         {
  15.             std::cout<<"write\n";
  16.         }
  17.  
  18.         virtual void read()
  19.         {
  20.             std::cout<<"read\n";
  21.         }
  22.     };
  23.  
  24.     struct RTSCTSInterface : public Interface
  25.     {
  26.         template <typename ... Args>
  27.         RTSCTSInterface(int rts, int rtsDelay, int cts, Args&& ... args)
  28.             : rts(rts), cts(cts), rtsDelay(rtsDelay), Interface(std::forward<Args>(args)...)
  29.  
  30.         {}
  31.  
  32.         void write() override
  33.         {
  34.             std::cout<<"rts down\n";
  35.             std::cout<<"rts delay\n";
  36.             std::cout<<"cts ok\n";
  37.             Interface::write();
  38.             std::cout<<"rts up\n";
  39.         }
  40.  
  41.         int rts;
  42.         int cts;
  43.         int rtsDelay;
  44.     };
  45.  
  46.     struct Powerable
  47.     {
  48.  
  49.         Powerable(int dummy) : dummy(dummy)
  50.         {}
  51.  
  52.         void turnOn()
  53.         {
  54.             std::cout<<"Turning on\n";
  55.         }
  56.  
  57.         void turnOff()
  58.         {
  59.             std::cout<<"turning off\n";
  60.         }
  61.  
  62.         int dummy;
  63.     };
  64.  
  65.     template <typename CustomDevice>
  66.     struct PowerableDevice : public Powerable, public CustomDevice
  67.     {
  68.         template <typename ... Args>
  69.         PowerableDevice(int dummy, Args&&... args)
  70.             : Powerable(dummy), CustomDevice(std::forward<Args>(args)...)
  71.         {}
  72.     };
  73.  
  74.     template <typename CustomInterface, typename CustomDevice>
  75.     struct CommunicableDevice;
  76.  
  77.     template <typename CustomDevice>
  78.     struct CommunicableDevice<Interface, CustomDevice> : public Interface, public CustomDevice
  79.     {
  80.         template <typename ... Args>
  81.         CommunicableDevice(int pin, Args&&... args)
  82.             : Interface(pin), CustomDevice(std::forward<Args>(args)...)
  83.         {}
  84.     };
  85.  
  86.     template <typename CustomDevice>
  87.     struct CommunicableDevice<RTSCTSInterface, CustomDevice> : public RTSCTSInterface, public CustomDevice
  88.     {
  89.         template <typename ... Args>
  90.         CommunicableDevice(int rts, int rtsDelay, int cts, int pin, Args&&... args)
  91.             : RTSCTSInterface(rts, rtsDelay, cts, pin), CustomDevice(std::forward<Args>(args)...)
  92.         {}
  93.     };
  94. }
  95.  
  96.  
  97. int main()
  98. {
  99.     using PowerableCommunicableDevice = T2::CommunicableDevice<T2::RTSCTSInterface, T2::PowerableDevice<T2::Device>>;
  100.     PowerableCommunicableDevice dev1{1, 200, 2, 3, 4, "dev1"};
  101.  
  102.     dev1.turnOn();
  103.     dev1.write();
  104.     dev1.turnOff();
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement