On Fri, 1 May 2009, Gustavo Sverzut Barbieri wrote:

> On Thu, Apr 30, 2009 at 9:13 PM, Carsten Haitzler <ras...@rasterman.com> 
> wrote:
>> On Thu, 30 Apr 2009 18:02:45 -0300 Gustavo Sverzut Barbieri
>> <barbi...@profusion.mobi> said:
>>
>>> On Thu, Apr 30, 2009 at 10:11 AM, Albin Tonnerre
>>> <albin.tonne...@gmail.com> wrote:
>>>> Hello,
>>>>
>>>> While playing with Eina_Inlist, I noticed that given a pointer to an
>>>> Eina_Inlist, one can't easily get a pointer to the structure containing it.
>>>> The only option now is to put the Eina_Inlist first in the structure, and
>>>> then cast this structure to an Eina_Inlist, which is far from optimal.
>>>> I'd therefore like to propose that we add in eina_inlist.h a macro along 
>>>> the
>>>> lines of
>>>>
>>>> #define FIND_A_SENSBILE_NAME_FOR_THIS(ptr, type) ((type *) ((Eina_Inlist *)
>>>> #ptr - offsetof(type, __in_list)))
>>>>
>>>> Then, we could get a pointer to the containing structure, no matter where
>>>> the Eina_Inlist is (note that I used __in_list as the member as we already
>>>> have a macro '#define EINA_INLIST Eina_Inlist __in_list', but a member
>>>> argument could be added just as well)
>>>>
>>>> Thoughts ?
>>>
>>> As we discussed on IRC, I like this. It would help avoid casts and try
>>> to make code bit easier to get right.
>>
>> much uglier as now you need to drag an offset # around all the time, and now
>> you still need to cast anyway (use a raw pointer offset and cast to ah Inlist
>> pointer). you don't get rid of it.
>>
>> inheritance in c in the end requires casting. thats how c does it. dont be
>> afraid of casting or think its ugly. it's how c becomes powerful and 
>> flexible.
>
> As the macro says, it still does the cast, BUT this cast will add some
> safety since if you change types somewhere and forget in some place,
> compiler will warn you.
>
> The only problem I see and I already talked to Albin about it is on
> weird compilers without offsetof() macros.

offsetof seems to exist in the lib c of Microsoft

Vincent

------------------------------------------------------------------------------
Register Now & Save for Velocity, the Web Performance & Operations 
Conference from O'Reilly Media. Velocity features a full day of 
expert-led, hands-on workshops and two days of sessions from industry 
leaders in dedicated Performance & Operations tracks. Use code vel09scf 
and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to