Re: Behaviour of getters wrt dup/ref

Hi Alex,

Strange that nobody replied yet, so let's start discussion by dropping
some random thoughts:

For convenience and performance I consider returning a weak reference
(const char*) the most reasonable choice for getters. If you want to
keep a reference to the value returned longer than it is guaranteed to
be valid, you still can call g_strdup on the caller side --- without
putting any memory management overhead on callers which just want to
have a short look on your properties.

Well, but you raised the question for properties which have to be
evaluated before returning them so for those properties I see two

1) rename the methods from "maman_get_bar" to "maman_eval_bar" 
   (or similiar)

2) handle them as lazy evaluated properties:

     const char* maman_get_expensive_info (Maman *self) 
       if (NULL == self->priv->expensive_info)
         self->priv->expensive_info = eval_expensive_info ();

       return self->priv->expensive_info;

Every time you know that property could become invalid you reset the
_expensive_info field and of course you have to reset that field in your
object's dispose method. IMHO this pattern is quite common in OOP and
gives most convenience without sacrifing performance. 

Well, and if you think its reasonable to transfer ownership for that
expensive field to the caller, you still can follow the pattern
introduced by GValue:

     char* maman_steal_expensive_info (Maman *self) 
       char *result = maman_get_expensive_info (self);
       self->priv->expensive_info = NULL;
       return result;

Hope those thoughts help.

Mathias Hasselmann <mathias hasselmann gmx de>

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]