Object origin from edit mode

i have been trying to understand the code which moves the origin because i wanted to try to make it available directly from edit mode.
first i need to say that this is the first time i do something with C so i have to learn a lot and its a bit hard to understand everything that goes on in the code. i have been writing some python addons so i have a bit experience with programming.

now to the issue i face at this point, i came to understand that moving the origin of a object basicly happens in edit mode by shifting the mesh elements and then moving the object in object mode to the offset position.

now i wanted first to see if i can get this to work for “origin to curosr” from edit mode.
the idea was if the object is in edit mode switch out to object mode and do the transformation and then switch back to edit mode again.
now the thing is that the object seems to switch into object mode but the transformation of the origin does not happen, i disabled the switching to edit mode to try to figure out what is goin on but as far as i can tell the object switches correctly but then does not perform the same action than when im executing it directly from object mode.
is there some kind of update or so that i need to initiate when i switch modes? or does anyone have a hint why this is not working?
also if this is the wrong approach i would be happy to know what would be a better way of doing this.

here is what i tried to do in the object_transform.c file at line ~920

/* and we move the obejcts to bring the origin into the position chosen*/
	for (ctx_ob = ctx_data_list.first;
	     ctx_ob = ctx_ob->next)
		Object *ob = ctx_ob->ptr.data;

		if ((ob->flag & OB_DONE) == 0) {
			bool do_inverse_offset = false;
			ob->flag |= OB_DONE;

			if (centermode == ORIGIN_TO_CURSOR) {
				if (ob->mode == OB_MODE_EDIT) {
					//switch to obejct mode for origin transformation
					ED_object_mode_set(C, OB_MODE_OBJECT);

					copy_v3_v3(cent, cursor);
					invert_m4_m4(ob->imat, ob->obmat);
					mul_m4_v3(ob->imat, cent);

					//switch back to edit mode
					//ED_object_mode_set(C, OB_MODE_EDIT);
				else {
					copy_v3_v3(cent, cursor);
					invert_m4_m4(ob->imat, ob->obmat);
					mul_m4_v3(ob->imat, cent);

The code around which you placed the ED_object_mode_set() calls is not the one that sets the origin, it only initializes some variables for later use.

Switching modes would need to be done at the very start and end of object_origin_set_exec(),

Hm ok then i will need to look again how this is working since i clearly did not fully understand it yet.
Thanks for the hints, this is very good information!
i will look into it and come back later when im stuck again.