A theory of emergent quantum mechanics and classical emergent gravity