Re: GtkTreeIter internals (was RE: signals versus vfuncs)



On Wed, 21 Jan 2004 Murray Cumming Comneon com wrote:

> > > > Iterators usually contain pointers to internal data structures,
> > > > Paths are just an ordinal representation of a (potential) node
> > > > position. Prepare to accept zero'd iterators (I sometimes
> > get some
> > > > from GtkTreeView after changing between models).
> >
> > [...]
> >
> > > is there any way at all to get notification when an iter is
> > no longer
> > > in use? perhaps there's something simple i'm missing...
> >
> > there's no way to know when an iterator is being freed or
> > copied since they are pure value objects. but that's not a
> > real problem, because an iterator basically "refers to a
> > certain state of a model", rather than maintains its own
> > state (which would require notification/hooks).
> >
> > if you implement your own model and need to deal with
> > life-time issues and iterators, you basically do:
> >
> > setup_model() {
> >   model->stamp = 1;
> >   model->glue_object = new_glue_object();
> > }
> >
> > modify_model() {
> >   model->stamp++; /* "invalidates" all iterators with old stamp */
> >   destroy_glue_object (model->glue_object);
> >   model->glue_object = new_glue_object();
> > }
> >
> > fill_iter() {
> >   iter->stamp = model->stamp;
> >   iter->data1 = model->glue_object; /* valid as long as stamp
> > is valid */
> >   iter->data2 = ...;                /* valid as long as data1
> > is valid */
> > }
> >
> > use_iter() {
> >   g_return_if_fail (iter->stamp == model->stamp);
> >   /* since iter->stamp is valid, iter->data1 is also valid */
> >   use_glue_object (iter->data1, iter->data2);
> > }
>
> This is a very useful email. Thanks.
>
> I just implemented a custom TreeModel in gtkmm:
> http://cvs.gnome.org/bonsai/cvsblame.cgi?file=gtkmm%2Fexamples%2Ftreemodelcu
> stom/exampletreemodel.cc&rev=&root=/cvs/gnome
>
> At the moment I'm just mashing a row-number integer into
> GtkTreeIter::user_data, as you can see on line 255 there. Obviously I need
> to store some more complex data to represent a full potential path in a
> model with children.
>
> I don't think I've understood how I would use this glue_object idea to do
> that. I suppose this glue_object could map GtkTreeIters to my "potential
> path" data, but how would I release (or update) the old invalid glue_object
> after creating a new one or updating it, when I mark all old GtkTreeIters as
> invalid?

you get to implement the glue object (or objects, this could for instance
be a vector<int> to remember row numbers) and the model yourself, so you release
the glue object (delete the vecotr) when you increment the stamp, since then all
iterators referring to your glue object (or to a member of your vector) become
invalid. i.e. all external references are gone => you can get rid of it.

>
> Murray Cumming
> www.murrayc.com
> murrayc usa net
>

---
ciaoTJ




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