After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 142126 - broken Gtk::TextIter API
broken Gtk::TextIter API
Status: RESOLVED FIXED
Product: gtkmm
Classification: Bindings
Component: general
2.4
Other All
: Normal major
: 3
Assigned To: gtkmm-forge
gtkmm-forge
Depends on:
Blocks:
 
 
Reported: 2004-05-08 10:17 UTC by Daniel Elstner
Modified: 2017-01-12 15:56 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
patch: Split Gtk::TextIter into TextIter and ConstTextIter (41.60 KB, patch)
2014-03-10 16:19 UTC, Kjell Ahlstedt
none Details | Review

Description Daniel Elstner 2004-05-08 10:17:00 UTC
In gtkmm 2.4 a non-const overload for Gtk::TextIter::get_marks() was introduced,
and the const variant was changed to return a list of const TextMark objects. 
This is severely broken; a const iterator is _not_ the same as a const_iterator!
 Other methods of Gtk::TextIter are likely to be affected too.
Comment 1 Murray Cumming 2004-05-08 10:22:55 UTC
Does this mean that something can no longer be done? Is a const_cast<> necessary
as a workaround? If so, then we should add that to the docs.

Just because a const iterator is not the same as a const_iterator does not mean
that there will never be a const iterator. Of course, I don't think that we have
a  TextBuffer::const_iterator.
Comment 2 Daniel Elstner 2004-05-08 10:33:02 UTC
Note that signal handler prototypes usually have const Gtk::TextIter& as
parameter. To be able to use that parameter properly, you have to make a dummy
copy as a work-around (that's what you did in regexxer).  A
const_cast<Gtk::TextIter&> would also work.

Right, we don't have const_iterator equivalent for for Gtk::TextIter yet, but
that's a different problem.
Comment 3 Daniel Elstner 2009-06-12 17:02:23 UTC
We might want to reexamine our current approach of always propagating indirect constness in general.
Comment 4 D. Scheffter 2011-07-05 14:31:41 UTC
Btw., I would like that

iterator Gtk::TextBuffer::begin();
iterator Gtk::TextBuffer::end();

would be

iterator Gtk::TextBuffer::begin() const;
iterator Gtk::TextBuffer::end() const;
Comment 5 Kjell Ahlstedt 2014-03-10 16:19:51 UTC
Created attachment 271447 [details] [review]
patch: Split Gtk::TextIter into TextIter and ConstTextIter

This patch tries to make TextBuffer and related classes more const correct.
It brakes ABI and probably also API. It's not meant to be pushed as is.
It's more of a first draft and a basis for discussion.
I have not made any changes to TextView. I'm sure some of its member functions
can take a const_iterator, a const const_iterator or a const iterator instead
of an iterator.

I have made a small class hierarchy:
  class TreeIterBase
  class TreeIter : public TreeIterBase
  class ConstTreeIter : public TreeIterBase

An alternative would be to let TreeIter and ConstTreeIter be two stand-alone
classes without a common base. That's probably a cleaner solution, but it
would contain a lot of duplicated code. GtkTreeIter contains a lot of
functions, most of which are identical in TreeIter and ConstTreeIter.

(In reply to comment #4)
> iterator Gtk::TextBuffer::begin() const;
> iterator Gtk::TextBuffer::end() const;

To mimic the STL containers (std::vector etc) it should be
  iterator Gtk::TextBuffer::begin();
  iterator Gtk::TextBuffer::end();
  const_iterator Gtk::TextBuffer::begin() const;
  const_iterator Gtk::TextBuffer::end() const;
and to have it like C++11, also
  const_iterator Gtk::TextBuffer::cbegin() const;
  const_iterator Gtk::TextBuffer::cend() const;
Comment 6 Kjell Ahlstedt 2016-08-04 08:36:51 UTC
In comment 5: s/TreeIter/TextIter/g
Comment 7 Kjell Ahlstedt 2017-01-12 15:56:52 UTC
https://git.gnome.org/browse/gtkmm/commit/?id=001e714e54b10d8bb3d7417046c6c4605b7a9b2f

gtkmm-4: I have pushed a patch that splits TextIter into a small class hierarchy.
  class TextIterBase
  class TextIter : public TextIterBase
  class TextConstIter : public TextIterBase

There are also aliases,
  TextBuffer::iterator
  TextBuffer::const_iterator

This bug report started with a critique of TextIter::get_marks(). Now there are
  std::vector<Glib::RefPtr<TextMark>> TextIter::get_marks() const
  std::vector<Glib::RefPtr<const TextMark>> TextConstIter::get_marks() const  

I have changed TextBuffer::iterator to TextBuffer::const_iterator where I think
appropriate in TextBuffer, TextView, TextMark and TextTag.

I would not be surprised if someone would prefer const_iterator in more methods.
A const_iterator in STL means "You can't use this iterator for modifying the
element pointed to." But if you have a non-const access to the container, you
can use the const_iterator for other types of modifications of the container,
e.g. for deleting the element that the const_iterator points to. I have
chosen not to change to const_iterator e.g. in TextBuffer::erase().