• 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 #include "src/base/macros.h"
11 
12 namespace v8 {
13 namespace base {
14 
15 // The intention of the base::iterator_range class is to encapsulate two
16 // iterators so that the range defined by the iterators can be used like
17 // a regular STL container (actually only a subset of the full container
18 // functionality is available usually).
19 template <typename ForwardIterator>
20 class iterator_range {
21  public:
22   typedef ForwardIterator iterator;
23   typedef ForwardIterator const_iterator;
24   typedef typename std::iterator_traits<iterator>::pointer pointer;
25   typedef typename std::iterator_traits<iterator>::reference reference;
26   typedef typename std::iterator_traits<iterator>::value_type value_type;
27   typedef
28       typename std::iterator_traits<iterator>::difference_type difference_type;
29 
iterator_range()30   iterator_range() : begin_(), end_() {}
31   template <typename ForwardIterator2>
iterator_range(ForwardIterator2 const & begin,ForwardIterator2 const & end)32   iterator_range(ForwardIterator2 const& begin, ForwardIterator2 const& end)
33       : begin_(begin), end_(end) {}
34 
begin()35   iterator begin() { return begin_; }
end()36   iterator end() { return end_; }
begin()37   const_iterator begin() const { return begin_; }
end()38   const_iterator end() const { return end_; }
cbegin()39   const_iterator cbegin() const { return begin_; }
cend()40   const_iterator cend() const { return end_; }
41 
empty()42   bool empty() const { return cbegin() == cend(); }
43 
44   // Random Access iterators only.
45   reference operator[](difference_type n) { return begin()[n]; }
size()46   difference_type size() const { return cend() - cbegin(); }
47 
48  private:
49   const_iterator const begin_;
50   const_iterator const end_;
51 };
52 
53 }  // namespace base
54 }  // namespace v8
55 
56 #endif  // V8_BASE_ITERATOR_H_
57