Implement TODO for implicit C-array-to-ArrayRef conversion.
[oota-llvm.git] / include / llvm / ADT / ArrayRef.h
1 //===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ADT_ARRAYREF_H
11 #define LLVM_ADT_ARRAYREF_H
12
13 #include "llvm/ADT/SmallVector.h"
14 #include <vector>
15
16 namespace llvm {
17   class APInt;
18   
19   /// ArrayRef - Represent a constant reference to an array (0 or more elements
20   /// consecutively in memory), i.e. a start pointer and a length.  It allows
21   /// various APIs to take consecutive elements easily and conveniently.
22   ///
23   /// This class does not own the underlying data, it is expected to be used in
24   /// situations where the data resides in some other buffer, whose lifetime
25   /// extends past that of the StringRef. For this reason, it is not in general
26   /// safe to store a ArrayRef.
27   ///
28   /// This is intended to be trivially copyable, so it should be passed by
29   /// value.
30   template<typename T>
31   class ArrayRef {
32   public:
33     typedef const T *iterator;
34     typedef const T *const_iterator;
35     typedef size_t size_type;
36     
37   private:
38     /// The start of the array, in an external buffer.
39     const T *Data;
40     
41     /// The number of elements.
42     size_t Length;
43     
44   public:
45     /// @name Constructors
46     /// @{
47     
48     /// Construct an empty ArrayRef.
49     /*implicit*/ ArrayRef() : Data(0), Length(0) {}
50     
51     /// Construct an ArrayRef from a single element.
52     /*implicit*/ ArrayRef(const T &OneElt)
53       : Data(&OneElt), Length(1) {}
54     
55     /// Construct an ArrayRef from a pointer and length.
56     /*implicit*/ ArrayRef(const T *data, size_t length)
57       : Data(data), Length(length) {}
58     
59     /// Construct an ArrayRef from a SmallVector.
60     /*implicit*/ ArrayRef(const SmallVectorImpl<T> &Vec)
61       : Data(Vec.data()), Length(Vec.size()) {}
62
63     /// Construct an ArrayRef from a std::vector.
64     /*implicit*/ ArrayRef(const std::vector<T> &Vec)
65       : Data(Vec.empty() ? (T*)0 : &Vec[0]), Length(Vec.size()) {}
66     
67     /// Construct an ArrayRef from a C array.
68     template <size_t N>
69     /*implicit*/ ArrayRef(const T (&Arr)[N])
70       : Data(Arr), Length(N) {}
71     
72     /// @}
73     /// @name Simple Operations
74     /// @{
75
76     iterator begin() const { return Data; }
77     iterator end() const { return Data + Length; }
78     
79     /// empty - Check if the array is empty.
80     bool empty() const { return Length == 0; }
81     
82     /// size - Get the array size.
83     size_t size() const { return Length; }
84     
85     /// front - Get the first element.
86     const T &front() const {
87       assert(!empty());
88       return Data[0];
89     }
90     
91     /// back - Get the last element.
92     const T &back() const {
93       assert(!empty());
94       return Data[Length-1];
95     }
96     
97     /// @}
98     /// @name Operator Overloads
99     /// @{
100     
101     const T &operator[](size_t Index) const {
102       assert(Index < Length && "Invalid index!");
103       return Data[Index];
104     }
105     
106     /// @}
107     /// @name Expensive Operations
108     /// @{
109     
110     std::vector<T> vec() const {
111       return std::vector<T>(Data, Data+Length);
112     }
113     
114     /// @}
115   };
116   
117   // ArrayRefs can be treated like a POD type.
118   template <typename T> struct isPodLike;
119   template <typename T> struct isPodLike<ArrayRef<T> > {
120     static const bool value = true;
121   };
122 }
123
124 #endif