Commit 0daaef05 authored by Raphael Kubo da Costa's avatar Raphael Kubo da Costa Committed by Commit Bot

Make cc::ListContainer's iterators STL-compatible

This fixes the libstdc++ build, and future-proofs the code against libc++
changes related to https://cplusplus.github.io/LWG/lwg-defects.html#2408

That defect report states that std::iterator_traits should be
SFINAE-friendly, and libstdc++ already complies with that, so when commit
2808b7ec ("cros: Improve damage for underlays") started using std::any_of()
with a cc::ListContainer the underlying code that function assumes (per the
standard) that the iterators it receives are at least input iterators. For
that to be true, std::iterator_traits needs to have a valid specialization
rather than the empty one (SFINAE), otherwise we end up with an error like
this:

In file included from ../../components/viz/service/display/overlay_processor.cc:5:
In file included from ../../components/viz/service/display/overlay_processor.h:10:
In file included from ../../base/containers/flat_map.h:12:
In file included from ../../base/containers/flat_tree.h:8:
In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/algorithm:62:
/usr/bin/../lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/bits/stl_algo.h:162:10: error: no matching function for call to '__iterator_category'
                       std::__iterator_category(__first));
                       ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/bits/stl_algo.h:3930:19: note: in instantiation of function template specialization 'std::__find_if<cc::ListContainer<viz::DrawQuad>::ConstIterator,
      __gnu_cxx::__ops::_Iter_pred<(lambda at ../../components/viz/service/display/overlay_processor.cc:258:49)> >' requested here
      return std::__find_if(__first, __last,
                  ^
/usr/bin/../lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/bits/stl_algo.h:526:40: note: in instantiation of function template specialization 'std::find_if<cc::ListContainer<viz::DrawQuad>::ConstIterator,
      (lambda at ../../components/viz/service/display/overlay_processor.cc:258:49)>' requested here
    { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
                                       ^
/usr/bin/../lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/bits/stl_algo.h:544:20: note: in instantiation of function template specialization 'std::none_of<cc::ListContainer<viz::DrawQuad>::ConstIterator,
      (lambda at ../../components/viz/service/display/overlay_processor.cc:258:49)>' requested here
    { return !std::none_of(__first, __last, __pred); }
                   ^
../../components/viz/service/display/overlay_processor.cc:257:30: note: in instantiation of function template specialization 'std::any_of<cc::ListContainer<viz::DrawQuad>::ConstIterator,
      (lambda at ../../components/viz/service/display/overlay_processor.cc:258:49)>' requested here
      bool has_damage = std::any_of(
                             ^
/usr/bin/../lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/bits/stl_iterator_base_types.h:205:5: note: candidate template ignored: substitution failure [with _Iter = cc::ListContainer<viz::DrawQuad>::ConstIterator]: no type named
      'iterator_category' in 'std::iterator_traits<cc::ListContainer<viz::DrawQuad>::ConstIterator>'
    __iterator_category(const _Iter&)
    ^

Fix it by adding a few types confirming to STL's naming conventions so that
std::iterator_traits works with all 4 iterator types cc::ListContainer
defines.

Bug: 819294
Change-Id: I1424e22b3793bec810d6d83be2490b4a402ed94d
Reviewed-on: https://chromium-review.googlesource.com/c/1496991
Commit-Queue: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Auto-Submit: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636860}
parent 1d23b6a1
...@@ -202,6 +202,13 @@ class ListContainer { ...@@ -202,6 +202,13 @@ class ListContainer {
return *this; return *this;
} }
// STL compatibility.
using iterator_category = std::forward_iterator_tag;
using value_type = BaseElementType*;
using reference = value_type&;
using pointer = value_type*;
using difference_type = ptrdiff_t;
private: private:
explicit Iterator(const ListContainerHelper::Iterator& base_iterator) explicit Iterator(const ListContainerHelper::Iterator& base_iterator)
: ListContainerHelper::Iterator(base_iterator) {} : ListContainerHelper::Iterator(base_iterator) {}
...@@ -242,6 +249,13 @@ class ListContainer { ...@@ -242,6 +249,13 @@ class ListContainer {
return *this; return *this;
} }
// STL compatibility.
using iterator_category = std::forward_iterator_tag;
using value_type = const BaseElementType*;
using reference = value_type&;
using pointer = value_type*;
using difference_type = ptrdiff_t;
private: private:
explicit ConstIterator( explicit ConstIterator(
const ListContainerHelper::ConstIterator& base_iterator) const ListContainerHelper::ConstIterator& base_iterator)
...@@ -281,6 +295,13 @@ class ListContainer { ...@@ -281,6 +295,13 @@ class ListContainer {
return *this; return *this;
} }
// STL compatibility.
using iterator_category = std::forward_iterator_tag;
using value_type = BaseElementType*;
using reference = value_type&;
using pointer = value_type*;
using difference_type = ptrdiff_t;
private: private:
explicit ReverseIterator(ListContainerHelper::ReverseIterator base_iterator) explicit ReverseIterator(ListContainerHelper::ReverseIterator base_iterator)
: ListContainerHelper::ReverseIterator(base_iterator) {} : ListContainerHelper::ReverseIterator(base_iterator) {}
...@@ -320,6 +341,13 @@ class ListContainer { ...@@ -320,6 +341,13 @@ class ListContainer {
return *this; return *this;
} }
// STL compatibility.
using iterator_category = std::forward_iterator_tag;
using value_type = const BaseElementType*;
using reference = value_type&;
using pointer = value_type*;
using difference_type = ptrdiff_t;
private: private:
explicit ConstReverseIterator( explicit ConstReverseIterator(
ListContainerHelper::ConstReverseIterator base_iterator) ListContainerHelper::ConstReverseIterator base_iterator)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment