• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_BASE_ITERATOR_H_
6 #define V8_BASE_ITERATOR_H_
7 
8 #include <iterator>
9 
10 namespace v8 {
11 namespace base {
12 
13 // The intention of the base::iterator_range class is to encapsulate two
14 // iterators so that the range defined by the iterators can be used like
15 // a regular STL container (actually only a subset of the full container
16 // functionality is available usually).
17 template <typename ForwardIterator>
18 class iterator_range {
19  public:
20   typedef ForwardIterator iterator;
21   typedef ForwardIterator const_iterator;
22   typedef typename std::iterator_traits<iterator>::pointer pointer;
23   typedef typename std::iterator_traits<iterator>::reference reference;
24   typedef typename std::iterator_traits<iterator>::value_type value_type;
25   typedef
26       typename std::iterator_traits<iterator>::difference_type difference_type;
27 
iterator_range()28   iterator_range() : begin_(), end_() {}
29   template <typename ForwardIterator2>
iterator_range(ForwardIterator2 const & begin,ForwardIterator2 const & end)30   iterator_range(ForwardIterator2 const& begin, ForwardIterator2 const& end)
31       : begin_(begin), end_(end) {}
32 
begin()33   iterator begin() { return begin_; }
end()34   iterator end() { return end_; }
begin()35   const_iterator begin() const { return begin_; }
end()36   const_iterator end() const { return end_; }
cbegin()37   const_iterator cbegin() const { return begin_; }
cend()38   const_iterator cend() const { return end_; }
39 
empty()40   bool empty() const { return cbegin() == cend(); }
41 
42   // Random Access iterators only.
43   reference operator[](difference_type n) { return begin()[n]; }
size()44   difference_type size() const { return cend() - cbegin(); }
45 
46  private:
47   const_iterator const begin_;
48   const_iterator const end_;
49 };
50 
51 }  // namespace base
52 }  // namespace v8
53 
54 #endif  // V8_BASE_ITERATOR_H_
55