[webkit-efl] FW: Use of pimpl idiom for Ewk classes

Dumez, Christophe christophe.dumez at intel.com
Fri Oct 19 04:28:03 PDT 2012


Hi,

I forgot to mention that "String vs const char*" is just one example and
may not be the best one because they are strongly related. In your example,
you could use eina_stringshare_add() or strdup() to avoid the crash and
convert. The difference is on client (browser) side because they now need
to free() or eina_stringshare_del() explicitly.

If you start thinking about "Vector vs Eina_List*" then it becomes
impossible to efficiently convert between the 2 and you will need 2 C++
methods to return both types.

Kr,

On Fri, Oct 19, 2012 at 2:21 PM, Dumez, Christophe <
christophe.dumez at intel.com> wrote:

>  > You can already see it in my patch for the destination() getter. It
>> has to return a "const char*" instead of a String because it is used by the
>> C API.
>> IMHO, external object(s) should treat Ewk classes as C++ as much as
>> possible in this case.****
>>
>>  Therefore, we don’t have to make functions that return char*.****
>>
>> Rather, caller can use utilization function to change String class to
>> char*.****
>>
>> ** **
>>
>> --- Ewk class****
>>
>> String destination() { … }****
>>
>> --- Utilization function****
>>
>> char* StoCP(String s)****
>>
>> {****
>>
>>     char* cp = s.utf8().data();****
>>
>>     return cp;****
>>
>> }****
>>
>> --- C layer****
>>
>> void i_am_c_public_api()****
>>
>> {****
>>
>>     char* cp = StoCP(destination);****
>>
>> }
>>
>
> Eh what? This is wrong and will crash.
>
> char* StoCP(String s) {
>     char* cp = s.utf8().data(); // s.utf8() returns a temporary CString
> object and you store a pointer to the internal representation of that
> temporary CString object
>     return cp; // You return a pointer to an area in memory that is no
> longer valid because it was temporary.
> }
>
> Secondly, even if that worked, we loose all benefit of Eina Stringsharing
> since we would return raw char* to the client.
>
> Kr,
> --
> Christophe Dumez
> Linux Software Engineer, PhD
> Intel Finland Oy - Open Source Technology Center
>
>


-- 
Christophe Dumez
Linux Software Engineer, PhD
Intel Finland Oy - Open Source Technology Center
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-efl/attachments/20121019/630e7ac1/attachment.html>


More information about the webkit-efl mailing list