Unofficial OpenGL Software Development Kit  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
array_ref.h
Go to the documentation of this file.
1 #ifndef ARRAY_REF_OPENGL_UTILITY_H
2 #define ARRAY_REF_OPENGL_UTILITY_H
3 
9 #include <vector>
10 #include <iterator>
11 #include <limits>
12 #include <stdexcept>
13 #include <boost/array.hpp>
14 
22 namespace refs
23 {
54  template<typename T>
55  class array_ref
56  {
57  public:
58  typedef T value_type;
59  typedef const T * pointer;
60  typedef const T & reference;
61  typedef const T & const_reference;
62  typedef const T * const_iterator;
63  typedef const_iterator iterator;
64  typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
65  typedef const_reverse_iterator reverse_iterator;
66  typedef size_t size_type;
67  typedef ptrdiff_t difference_type;
68 
69  // construct/copy
70  array_ref() : m_data(NULL), m_size(0) {}
71 
72  array_ref(const T *arr, size_t length) : m_data(arr), m_size(length) {}
73  array_ref(const std::vector<T> & v) : m_data(&v[0]), m_size(v.size()) {}
74  template<size_t N>
75  array_ref(const T(&a)[N]) : m_data(a), m_size(N) {}
76  template<size_t N>
77  array_ref(const boost::array< T, N > & a) : m_data(a.data()), m_size(N) {}
78 
79  const_iterator begin() const {return m_data;}
80  const_iterator end() const {return m_data + m_size;}
81  const_iterator cbegin() const {return m_data;}
82  const_iterator cend() const {return m_data + m_size;}
83  const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
84  const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
85  const_reverse_iterator crbegin() const {return const_reverse_iterator(cend());}
86  const_reverse_iterator crend() const {return const_reverse_iterator(cbegin());}
87 
88  size_type size() const {return m_size;}
89  size_type max_size() const {return std::numeric_limits<size_type>::max();}
90  bool empty() const {return m_size == 0;}
91 
92  const T & operator[](size_t i) const {return m_data[i];}
93  const T & at(size_t i) const
94  {
95  if(i >= m_size)
96  throw std::out_of_range();
97  return m_data[i];
98  }
99  const T & front() const {return m_data[0];}
100  const T & back() const {return m_data[m_size - 1];}
101  const T * data() const {return m_data;}
102 
103  std::vector<T> vec() const {return std::vector<T>(m_data, m_data + m_size);}
104  std::basic_string<T> str() const {return std::basic_string<T>(m_data, m_data + m_size);}
105 
106  void clear() {m_size = 0; m_data = NULL;}
107  void remove_prefix(size_type n)
108  {
109  m_data += n;
110  m_size -= n;
111  if(m_size <= 0)
112  clear();
113  }
114 
115  void remove_suffix(size_type n)
116  {
117  m_size -= n;
118  if(m_size <= 0)
119  clear();
120  }
121 
122  void pop_front() {remove_prefix(1);}
123  void pop_back() {remove_suffix(1);}
124  private:
125  const T *m_data;
126  size_t m_size;
127  };
128 }
129 
130 
131 #endif //ARRAY_REF_OPENGL_UTILITY_H