Closed Bug 1386838 Opened 7 years ago Closed 4 years ago

Give nsTObserverArray standard iterator types

Categories

(Core :: XPCOM, enhancement, P3)

enhancement

Tracking

()

RESOLVED FIXED
mozilla79
Tracking Status
firefox79 --- fixed

People

(Reporter: ehsan.akhgari, Assigned: sg)

References

(Blocks 1 open bug)

Details

Attachments

(1 file)

Basically things adhering to this type definition for nsTArray: https://searchfox.org/mozilla-central/rev/bbc1c59e460a27b20929b56489e2e55438de81fa/xpcom/ds/nsTArray.h#864 This will allow these iterators to be used in C++11 for loops, Reverse(), etc.
This is a bit tricky since we support mutating the array during iteration. We have a couple types of iterators in there, ForwardIterator and EndLimitedIterator, that behave differently depending on mutation behavior. They also require that `HasMore` is checked before retrieving the next item. I'm not even sure how we could define `end()` in this case.
Priority: -- → P3

(In reply to Eric Rahm [:erahm] from comment #1)

This is a bit tricky since we support mutating the array during iteration.
We have a couple types of iterators in there, ForwardIterator and
EndLimitedIterator, that behave differently depending on mutation behavior.

That's not a fundamental issue for providing standard iterators. They could be returned by non-standard member functions, e.g. forward_begin, forward_end, end_limited_begin, end_limited_end.

Only if we want to be a Range (and support range-based for), we would need to decide which of these iterator flavours should be used for that. If we think the user needs the choice, we could provide them via a simple wrapper type templates such that one could write:

nsTObserverArray<Foo> array;
for (const Foo& foo : ForwardIterated(array)) { ... }
for (const Foo& foo : EndLimitedIterated(array)) { ... }

with (just a sketch)

template<typename ObserverArrayT> struct ForwardIterated {
// maybe define value_type etc.

ForwardIterated(ObserverArrayT &aArray);

auto begin() { return mArray.forward_begin(); }
auto end() { return mArray.forward_end(); }

private:
ObserverArrayT &aArray;
};

They also require that HasMore is checked before retrieving the next item.

I'm not even sure how we could define end() in this case.

A Range can now have different types for begin and end iterators, maybe this could be exploited here.

Range-based for also allows for that with C++17.

Assignee: nobody → sgiesecke
Status: NEW → ASSIGNED
Attachment #9155922 - Attachment description: Bug 1386838 - Add STL-style range for nsTObserverArray based on ForwardIterator. r=#xpcom-reviewers → Bug 1386838 - Add STL-style ranges for nsTObserverArray based on ForwardIterator/EndLimitedIterator/BackwardIterator. r=#xpcom-reviewers
Blocks: 1645339
Pushed by sgiesecke@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/f75d1d3e2f64 Add STL-style ranges for nsTObserverArray based on ForwardIterator/EndLimitedIterator/BackwardIterator. r=froydnj
Status: ASSIGNED → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla79
Blocks: 1680209
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: