A generalization of the ham sandwich theorem