Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace T2
- {
- struct Device
- {
- Device(const std::string& name) : name(name) {}
- std::string name;
- };
- struct Interface
- {
- Interface(int pin) {}
- virtual void write()
- {
- std::cout<<"write\n";
- }
- virtual void read()
- {
- std::cout<<"read\n";
- }
- };
- struct RTSCTSInterface : public Interface
- {
- template <typename ... Args>
- RTSCTSInterface(int rts, int rtsDelay, int cts, Args&& ... args)
- : rts(rts), cts(cts), rtsDelay(rtsDelay), Interface(std::forward<Args>(args)...)
- {}
- void write() override
- {
- std::cout<<"rts down\n";
- std::cout<<"rts delay\n";
- std::cout<<"cts ok\n";
- Interface::write();
- std::cout<<"rts up\n";
- }
- int rts;
- int cts;
- int rtsDelay;
- };
- struct Powerable
- {
- Powerable(int dummy) : dummy(dummy)
- {}
- void turnOn()
- {
- std::cout<<"Turning on\n";
- }
- void turnOff()
- {
- std::cout<<"turning off\n";
- }
- int dummy;
- };
- template <typename CustomDevice>
- struct PowerableDevice : public Powerable, public CustomDevice
- {
- template <typename ... Args>
- PowerableDevice(int dummy, Args&&... args)
- : Powerable(dummy), CustomDevice(std::forward<Args>(args)...)
- {}
- };
- template <typename CustomInterface, typename CustomDevice>
- struct CommunicableDevice;
- template <typename CustomDevice>
- struct CommunicableDevice<Interface, CustomDevice> : public Interface, public CustomDevice
- {
- template <typename ... Args>
- CommunicableDevice(int pin, Args&&... args)
- : Interface(pin), CustomDevice(std::forward<Args>(args)...)
- {}
- };
- template <typename CustomDevice>
- struct CommunicableDevice<RTSCTSInterface, CustomDevice> : public RTSCTSInterface, public CustomDevice
- {
- template <typename ... Args>
- CommunicableDevice(int rts, int rtsDelay, int cts, int pin, Args&&... args)
- : RTSCTSInterface(rts, rtsDelay, cts, pin), CustomDevice(std::forward<Args>(args)...)
- {}
- };
- }
- int main()
- {
- using PowerableCommunicableDevice = T2::CommunicableDevice<T2::RTSCTSInterface, T2::PowerableDevice<T2::Device>>;
- PowerableCommunicableDevice dev1{1, 200, 2, 3, 4, "dev1"};
- dev1.turnOn();
- dev1.write();
- dev1.turnOff();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement