SPH
ArrayRef.h
Go to the documentation of this file.
1 #pragma once
2 
4 
6 
7 enum class RefEnum {
10  WEAK,
11 
14  STRONG,
15 };
16 
17 template <typename T>
18 class ArrayRef {
19  friend class ArrayRef<std::remove_const_t<T>>;
20 
21 private:
23  ArrayView<T> ref;
24 
26  Array<std::remove_const_t<T>> holder;
27 
28 public:
29  ArrayRef() = default;
30 
31  ArrayRef(ArrayView<T> data, const RefEnum type) {
32  if (type == RefEnum::WEAK) {
33  ref = data;
34  } else {
35  holder.reserve(data.size());
36  for (Size i = 0; i < data.size(); ++i) {
37  holder.emplaceBack(data[i]);
38  }
39  ref = holder;
40  }
41  }
42 
43  ArrayRef(ArrayRef&& other)
44  : holder(std::move(other.holder)) {
45  this->reset(other);
46  }
47 
49  holder = std::move(other.holder);
50  this->reset(other);
51  return *this;
52  }
53 
54  INLINE T& operator[](const Size idx) {
55  return ref[idx];
56  }
57 
58  INLINE const T& operator[](const Size idx) const {
59  return ref[idx];
60  }
61 
62  INLINE Size size() const {
63  return ref.size();
64  }
65 
66  INLINE bool empty() const {
67  return size() == 0;
68  }
69 
71  INLINE bool owns() const {
72  return !holder.empty();
73  }
74 
79  void seize() {
80  if (!owns() && !empty()) {
81  holder.reserve(ref.size());
82  for (Size i = 0; i < ref.size(); ++i) {
83  holder.emplaceBack(ref[i]);
84  }
85  ref = holder;
86  }
87  }
88 
90  return ref.begin();
91  }
92 
94  return ref.begin();
95  }
96 
98  return ref.end();
99  }
100 
102  return ref.end();
103  }
104 
105  operator ArrayRef<const T>() && {
106  ArrayRef<const T> copy;
107  copy.ref = ref;
108  copy.holder = std::move(holder);
109  return copy;
110  }
111 
112  operator ArrayView<T>() {
113  return ref;
114  }
115 
116  operator ArrayView<const T>() const {
117  return ref;
118  }
119 
120 private:
124  void reset(ArrayRef& other) {
125  if (this->owns()) {
126  // set the reference to our buffer
127  ref = holder;
128  // we moved the buffer from other, so we must also reset the (invalidated) view
129  other.ref = nullptr;
130  } else {
131  // copy the reference (no need to change anything in the moved object)
132  ref = other.ref;
133  }
134  }
135 };
136 
137 template <typename T>
139  return ArrayRef<T>(data, type);
140 }
141 
142 template <typename T>
143 ArrayRef<const T> makeArrayRef(const Array<T>& data, const RefEnum type) {
144  return ArrayRef<const T>(data, type);
145 }
146 
147 template <typename T>
149  // we never want to hold a weak reference to temporary data
150  return ArrayRef<T>(std::move(data), RefEnum::STRONG);
151 }
152 
RefEnum
Definition: ArrayRef.h:7
ArrayRef< T > makeArrayRef(Array< T > &data, const RefEnum type)
Definition: ArrayRef.h:138
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE bool empty() const
Definition: ArrayRef.h:66
INLINE T & operator[](const Size idx)
Definition: ArrayRef.h:54
ArrayRef(ArrayRef &&other)
Definition: ArrayRef.h:43
INLINE Size size() const
Definition: ArrayRef.h:62
INLINE bool owns() const
Returns true if the referenced data are held by the object.
Definition: ArrayRef.h:71
Iterator< const T > end() const
Definition: ArrayRef.h:101
ArrayRef & operator=(ArrayRef &&other)
Definition: ArrayRef.h:48
INLINE const T & operator[](const Size idx) const
Definition: ArrayRef.h:58
Iterator< const T > begin() const
Definition: ArrayRef.h:93
void seize()
Copies the referenced buffer into the internal storage, if not already owning the buffer.
Definition: ArrayRef.h:79
Iterator< T > begin()
Definition: ArrayRef.h:89
Iterator< T > end()
Definition: ArrayRef.h:97
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
Generic dynamically allocated resizable storage.
Definition: Array.h:43
void reserve(const TCounter newMaxSize)
Allocates enough memory to store the given number of elements.
Definition: Array.h:279
StorageType & emplaceBack(TArgs &&... args)
Constructs a new element at the end of the array in place, using the provided arguments.
Definition: Array.h:332
INLINE bool empty() const noexcept
Definition: Array.h:201
Simple (forward) iterator over continuous array of objects of type T.
Definition: Iterator.h:18
Overload of std::swap for Sph::Array.
Definition: Array.h:578