LLVM API Documentation

iterator_range.h
Go to the documentation of this file.
00001 //===- iterator_range.h - A range adaptor for iterators ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 /// \file
00010 /// This provides a very simple, boring adaptor for a begin and end iterator
00011 /// into a range type. This should be used to build range views that work well
00012 /// with range based for loops and range based constructors.
00013 ///
00014 /// Note that code here follows more standards-based coding conventions as it
00015 /// is mirroring proposed interfaces for standardization.
00016 ///
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_ADT_ITERATOR_RANGE_H
00020 #define LLVM_ADT_ITERATOR_RANGE_H
00021 
00022 #include <utility>
00023 
00024 namespace llvm {
00025 
00026 /// \brief A range adaptor for a pair of iterators.
00027 ///
00028 /// This just wraps two iterators into a range-compatible interface. Nothing
00029 /// fancy at all.
00030 template <typename IteratorT>
00031 class iterator_range {
00032   IteratorT begin_iterator, end_iterator;
00033 
00034 public:
00035   iterator_range() {}
00036   iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
00037       : begin_iterator(std::move(begin_iterator)),
00038         end_iterator(std::move(end_iterator)) {}
00039 
00040   IteratorT begin() const { return begin_iterator; }
00041   IteratorT end() const { return end_iterator; }
00042 };
00043 
00044 /// \brief Convenience function for iterating over sub-ranges.
00045 ///
00046 /// This provides a bit of syntactic sugar to make using sub-ranges
00047 /// in for loops a bit easier. Analogous to std::make_pair().
00048 template <class T> iterator_range<T> make_range(T x, T y) {
00049   return iterator_range<T>(std::move(x), std::move(y));
00050 }
00051 }
00052 
00053 #endif