Astra SDK  v2.1.3
Array.hpp
1 // This file is part of the Orbbec Astra SDK [https://orbbec3d.com]
2 // Copyright (c) 2015-2017 Orbbec 3D
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // Be excellent to each other.
17 #ifndef ASTRA_ARRAY_HPP
18 #define ASTRA_ARRAY_HPP
19 
20 namespace astra {
29  template<typename T>
30  class Array
31  {
32  public:
34  inline Array() : ptr_(nullptr), size_(0) {}
36  inline Array(decltype(nullptr)) : ptr_(nullptr), size_(0) {}
38  inline Array(T* ptr, size_t size) : ptr_(ptr), size_(size) {}
40  inline Array(T* begin, T* end) : ptr_(begin), size_(end - begin) {}
41 
43  inline size_t size() const { return size_; }
45  inline bool empty() const { return size_ == 0; }
46 
48  inline T* data() { return ptr_; }
50  inline const T* data() const { return ptr_; }
51 
53  inline T* begin() { return ptr_; }
55  inline const T* begin() const { return ptr_; }
57  inline T* end() { return ptr_ + size_; }
59  inline const T* end() const { return ptr_ + size_; }
60 
62  inline T& front() { return *ptr_; }
64  inline const T& front() const { return *ptr_; }
66  inline T& back() { return ptr_[size_ - 1]; }
68  inline const T& back() const { return ptr_[size_ - 1]; }
69 
74  inline T& operator[](size_t index)
75  {
76  return ptr_[index];
77  }
78 
83  inline const T& operator[](size_t index) const
84  {
85  return ptr_[index];
86  }
87 
93  inline Array<T> slice(size_t start, size_t end)
94  {
95  return Array<T>(ptr_ + start, end - start);
96  }
97 
104  inline Array<const T> slice(size_t start, size_t end) const
105  {
106  return Array<const T>(ptr_ + start, end - start);
107  }
108 
110  inline bool operator==(const Array& other) const
111  {
112  if (this == &other) { return true; }
113  return this->ptr_ == other.ptr_;
114  }
115 
117  inline bool operator!=(const Array& other) const { return !(*this == other); }
118 
119  private:
120  T* ptr_;
121  size_t size_;
122  };
123 
131  template<typename T>
132  inline Array<T> make_array(T* ptr, std::size_t size)
133  {
134  return Array<T>(ptr, size);
135  }
136 
137 }
138 
139 #endif // ASTRA_ARRAY_HPP
Simple wrapper around primitive arrays
Definition: Array.hpp:31
Array(T *ptr, size_t size)
Constructs array from pointer and size
Definition: Array.hpp:38
const T & back() const
Gets immutable reference to last array element
Definition: Array.hpp:68
T * end()
Gets mutable iterator pointing to one past the end of array
Definition: Array.hpp:57
T * begin()
Gets mutable iterator pointing to beginning of array
Definition: Array.hpp:53
Array(T *begin, T *end)
Constructs array from begin and end iterators
Definition: Array.hpp:40
Array< T > slice(size_t start, size_t end)
Creates a mutable sub-array of the same type based on a start and end index
Definition: Array.hpp:93
const T * begin() const
Gets immutable iterator pointing to beginning of array
Definition: Array.hpp:55
Array< const T > slice(size_t start, size_t end) const
Creates an immutable sub-array of the same type based on a start and end index
Definition: Array.hpp:104
Array(decltype(nullptr))
Implicit conversion constructor from nullptr to a zero-length array
Definition: Array.hpp:36
bool operator!=(const Array &other) const
Identity based inequality operator
Definition: Array.hpp:117
const T & front() const
Gets immutable reference to first array element
Definition: Array.hpp:64
size_t size() const
Gets size of array
Definition: Array.hpp:43
const T * data() const
Gets immutable raw pointer to array data
Definition: Array.hpp:50
T * data()
Gets mutable raw pointer to array data
Definition: Array.hpp:48
T & back()
Gets mutable reference to last array element
Definition: Array.hpp:66
bool operator==(const Array &other) const
Identity based equality operator
Definition: Array.hpp:110
bool empty() const
Returns true if array is zero length
Definition: Array.hpp:45
const T & operator[](size_t index) const
Immutable index operator into array
Definition: Array.hpp:83
T & front()
Gets mutable reference to first array element
Definition: Array.hpp:62
Array()
Default constructs zero-length array
Definition: Array.hpp:34
const T * end() const
Gets iterator pointing to one past the end of array
Definition: Array.hpp:59
T & operator[](size_t index)
Mutable index operator into array
Definition: Array.hpp:74
Array< T > make_array(T *ptr, std::size_t size)
Simplifies Array construction by leveraging function template type deduction
Definition: Array.hpp:132