Re: Behaviour of getters wrt dup/ref



Am Sonntag, den 16.09.2007, 12:12 -0400 schrieb Behdad Esfahbod:
> On Sun, 2007-09-16 at 03:03 -0400, David Nečas (Yeti) wrote:
> > On Sat, Sep 15, 2007 at 11:08:38PM -0400, Behdad Esfahbod wrote:
> > > On Fri, 2007-09-14 at 10:35 -0400, Alexander Larsson wrote:
> > > > 
> > > > char *      g_data_input_stream_get_line (GDataInputStream *data_stream,
> > > >                                           gsize             *length,
> > > >                                           GCancellable     *cancellable,
> > > >                                           GError           **error);
> > > > 
> > > > This actually reads new data from the stream, so it has to dup. One
> > > > could imagine a similar call that returns some form of object instead
> > > > of a string. 
> > > 
> > > I think it's pretty common in glib and pango at least to return
> > > g_strdup'ed strings.  The no-ref-count rule is mostly for objects that
> > > have a literal ref/unref pair.
> > > 
> > > Other than that, for functions that return read data from the stream,
> > > some people may have reasons to want to avoid malloc/free'ing on each
> > > line.  One way to work around that is to have the function take a
> > > GString, so you can reuse the buffer from the previous line.  I know
> > > most people are not a big fan of that idea though.
> > 
> > The right interface for this type of functions have been
> > already invented: that of glibc's getline.  It can allocate
> > new buffers, it can reuse existing buffers resizing them if
> > necessary -- and it can be even used with GStrings [if they
> > use the same memory allocator] although that's a bit dirty.
> 
> Well, that's exactly what happens if you make the API take GString.

Don't know if you try to support GString APIs, but I do not understand
any GString bashing (expect maybe for the missing reference counting).
In my opinion GString is a very useful member of the GLib API - so it
also should be used in the public API of other libraries when it makes
sense. Reading lines is such a place. A pattern I found useful for
read_line like stuff is this:

   const gchar*
   maman_bar_read_line (MamanBar *self, GString *buffer)
   {
     g_size offset;

     g_return_val_if_fail (MAMAN_IS_BAR (self), NULL);

     if (NULL == buffer)
       {
         g_string_truncate (self->priv->buffer, 0);
         buffer = self->priv->buffer;
       }

     offset = buffer->len;
     manan_bar_real_read_line (self, buffer);
     return buffer->str + offset;
   }

Sidenote to Alex: g_data_input_stream_get_line and its friend really
should use "read" as verb, not "get". The verb "get" usually indicates
you do not change the state of an object - expect maybe for buffering
expensive data.

Ciao,
Mathias
-- 
Mathias Hasselmann <mathias hasselmann gmx de>
http://taschenorakel.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]