/* * ControlSystemWrapper.hpp * * Created on: 30 мая 2020 г. * Author: LeonidTitov */ #ifndef UMLIBRARY_TECHNOLOGICAL_ADAPTER_CONTROLSYSTEMWRAPPER_HPP_ #define UMLIBRARY_TECHNOLOGICAL_ADAPTER_CONTROLSYSTEMWRAPPER_HPP_ #include "../../common/ResourceKeeper.hpp" namespace technological { namespace adapter { template struct TieInterface { virtual Input * try_connect() = 0; virtual void disconnect() = 0; typedef Input InputConnector; virtual ~TieInterface() = default; }; //Грабим два интерфейса, связываем, предоставляем один интерфейс. template class TieWrapper : public TieInterface { public: Input * try_connect(); void disconnect(); TieWrapper( ResourceKeeper & interconnect, TieInterface & output, OnConnect on_connect = OnConnect(), OnDisonnect on_disconnect = OnDisonnect() ) : interconnect(interconnect), output(output), on_connect(on_connect), on_disconnect(on_disconnect) {} typedef TieInterface Interface; private: Locable interconnect; TieInterface & output; OnConnect on_connect; OnDisonnect on_disconnect; }; template class EndpointWrapper : public TieInterface { public: Input * try_connect(); void disconnect(); EndpointWrapper( ResourceKeeper & endpoint, OnConnect on_connect = OnConnect(), OnDisonnect on_disconnect = OnDisonnect() ) : interconnect(endpoint), on_connect(on_connect), on_disconnect(on_disconnect) {} private: Locable interconnect; OnConnect on_connect; OnDisonnect on_disconnect; }; template class TieInterconnector : public TieInterface { public: Input * try_connect(); void disconnect(); TieInterconnector( TieInterface & a, TieInterface & b, Interconnect interconnect = Interconnect() ) : a(a), b(b), interconnect(interconnect) {} private: TieInterface & a; TieInterface & b; Interconnect interconnect; }; } } template inline Input * technological::adapter::TieWrapper::try_connect() { if( interconnect.try_lock() ) { Output * out_interface = output.try_connect(); if( out_interface ) { on_connect( interconnect.pointer(), out_interface ); } else interconnect.unlock(); } return interconnect.pointer(); } template inline void technological::adapter::TieWrapper::disconnect() { on_disconnect( interconnect.pointer() ); output.disconnect(); interconnect.unlock(); } template inline Input * technological::adapter::EndpointWrapper::try_connect() { if( interconnect.try_lock() ) { on_connect( interconnect.pointer() ); } return interconnect.pointer(); } template inline void technological::adapter::EndpointWrapper::disconnect() { on_disconnect( interconnect.pointer() ); interconnect.unlock(); } template inline Input * technological::adapter::TieInterconnector::try_connect() { Input * output = nullptr; Input * input_a = a.try_connect(); if( input_a ) { Input * input_b = b.try_connect(); if( input_b ) { output = interconnect( input_a, input_b ); } else a.disconnect(); } return output; } template inline void technological::adapter::TieInterconnector::disconnect() { b.disconnect(); a.disconnect(); } #endif /* UMLIBRARY_TECHNOLOGICAL_ADAPTER_CONTROLSYSTEMWRAPPER_HPP_ */