MxEngine
ArrayView.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <array>
32 #include <vector>
33 #include "Core/Macro/Macro.h"
34 #include "Utilities/STL/MxVector.h"
35 
36 namespace MxEngine
37 {
43  template<typename T>
44  class array_view
45  {
46  T* _data;
47  size_t _size;
48  public:
49  array_view();
50  array_view(T* data, size_t size);
51  array_view(const array_view&) = default;
52  array_view(array_view&&) = default;
53  array_view& operator=(const array_view&) = default;
54  array_view& operator=(array_view&&) = default;
55  ~array_view() = default;
56  template<size_t N>
57  array_view(T(&array)[N]);
58  template<size_t N>
59  array_view(std::array<T, N>& array);
60  array_view(std::vector<T>& vec);
61  array_view(MxVector<T>& vec);
62  template<typename RandomIt>
63  array_view(RandomIt begin, RandomIt end);
64  size_t size() const;
65  bool empty() const;
66  T* data();
67  const T* data() const;
68  T& operator[](size_t idx);
69  const T& operator[](size_t idx) const;
70  T& front();
71  const T& front() const;
72  T& back();
73  const T& back() const;
74 
75  T* begin();
76  T* end();
77  const T* begin() const;
78  const T* end() const;
79  };
80 
81  template<typename T>
82  using ArrayView = array_view<T>;
83 
84  template<typename T>
86  {
87  this->_data = nullptr;
88  this->_size = 0;
89  }
90 
91  template<typename T>
92  inline array_view<T>::array_view(T* data, size_t size)
93  {
94  this->_data = data;
95  this->_size = size;
96  }
97 
98  template<typename T>
99  inline array_view<T>::array_view(std::vector<T>& vec)
100  {
101  this->_data = vec.data();
102  this->_size = vec.size();
103  }
104 
105  template<typename T>
106  inline array_view<T>::array_view(MxVector<T>& vec)
107  {
108  this->_data = vec.data();
109  this->_size = vec.size();
110  }
111 
112  template<typename T>
113  inline size_t array_view<T>::size() const
114  {
115  return this->_size;
116  }
117 
118  template<typename T>
119  inline bool array_view<T>::empty() const
120  {
121  return this->_size == 0;
122  }
123 
124  template<typename T>
125  inline T* array_view<T>::data()
126  {
127  return this->_data;
128  }
129 
130  template<typename T>
131  inline const T* array_view<T>::data() const
132  {
133  return this->_data;
134  }
135 
136  template<typename T>
137  inline T& array_view<T>::operator[](size_t idx)
138  {
139  MX_ASSERT(idx < this->_size);
140  return this->_data[idx];
141  }
142 
143  template<typename T>
144  inline const T& array_view<T>::operator[](size_t idx) const
145  {
146  MX_ASSERT(idx < this->_size);
147  return this->_data[idx];
148  }
149 
150  template<typename T>
151  inline T& array_view<T>::front()
152  {
153  MX_ASSERT(this->_size > 0);
154  return this->_data[0];
155  }
156 
157  template<typename T>
158  inline const T& array_view<T>::front() const
159  {
160  MX_ASSERT(this->_size > 0);
161  return this->_data[0];
162  }
163 
164  template<typename T>
165  inline T& array_view<T>::back()
166  {
167  MX_ASSERT(this->_size > 0);
168  return this->_data[this->_size - 1];
169  }
170 
171  template<typename T>
172  inline const T& array_view<T>::back() const
173  {
174  MX_ASSERT(this->_size > 0);
175  return this->_data[this->_size - 1];
176  }
177 
178  template<typename T>
179  inline T* array_view<T>::begin() //-V524
180  {
181  return this->_data;
182  }
183 
184  template<typename T>
185  inline T* array_view<T>::end()
186  {
187  return this->_data + this->_size;
188  }
189 
190  template<typename T>
191  inline const T* array_view<T>::begin() const //-V524
192  {
193  return this->_data;
194  }
195 
196  template<typename T>
197  inline const T* array_view<T>::end() const
198  {
199  return this->_data + this->_size;
200  }
201 
202  template<typename T>
203  template<size_t N>
204  inline array_view<T>::array_view(T(&array)[N])
205  {
206  this->_data = array;
207  this->_size = N;
208  }
209 
210  template<typename T>
211  template<size_t N>
212  inline array_view<T>::array_view(std::array<T, N>& array)
213  {
214  this->_data = array.data();
215  this->_size = N;
216  }
217 
218  template<typename T>
219  template<typename RandomIt>
220  inline array_view<T>::array_view(RandomIt begin, RandomIt end)
221  {
222  this->_data = begin;
223  this->_size = end - begin;
224  }
225 
231  template<typename T>
232  inline array_view<T> make_view(T& value)
233  {
234  return array_view<T>(&value, 1);
235  }
236 }
Definition: ArrayView.h:44
array_view< T > make_view(T &value)
Definition: ArrayView.h:232
Definition: Application.cpp:49