Modelling and optimisation of general hybrid systems in the continuous time domain