November 06, 2020 A GtkD question: Is there an explicit public function to get D Classes from StructCtype*? | ||||
---|---|---|---|---|
| ||||
For instance, I have extern (C) int sortList1(GtkListBoxRow* _row1, GtkListBoxRow* _row2, void* userData) I pass it as listbox.setSortFunc(&sortList1, cast(void*)this, &_destroy2); In the sortList1 function I want to access derived members of my ListBoxRowWithData such as: auto row1 = cast(ListBoxRowWithData)getMyDObject!ListBoxRow(_row1); auto row2 = cast(ListBoxRowWithData)getMyDObject!ListBoxRow(_row2); if(row1.id < row2.id) return 1; A regular type cast returns null. T opCast(T)() in ObjectG.d returns null in this case. I dug into the sources a little, and my working solution is like: template getCType(T) { static if ( is(T == class) ) alias getCType = typeof(T.tupleof[0]); else alias getCType = void*; } T getMyDObject(T, TC = getCType!T)(void* data){ T ret = ObjectG.getDObject!(T)(cast(TC)data); return ret; } Is there an existing implementation for T getMyDObject(T, TC = getCType!T)(void* data)? |
Copyright © 1999-2021 by the D Language Foundation