CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
span.h
1
2#pragma once
3
4#include <algorithm>
5#include <type_traits>
6
7#include <system/array.h>
8#include <system/details/stack_array.h>
9#include <system/collections/ienumerator.h>
10#include <system/convert.h>
11#include <system/smart_ptr.h>
12#include <system/string.h>
13#include <system/exceptions.h>
14#include <system/make_const_ref.h>
15
16namespace System {
17
18namespace Details {
19
22template <typename T, typename Span, typename MutableSpan>
23class SpanCore
24{
25protected:
26 using MutableT = std::remove_const_t<T>;
27 using RefrerenceT = std::conditional_t<std::is_const<T>::value, MakeConstRef_t<MutableT>, T&>;
28 using ArrayPtrT = System::ArrayPtr<MutableT>;
29
30public:
33 class Enumerator
34 {
35 friend SpanCore;
36
37 SpanCore m_span;
38 int32_t m_idx = -1;
39
42 explicit Enumerator(const SpanCore& span) : m_span(span)
43 {}
44
45 public:
49 RefrerenceT get_Current()
50 {
51 if (m_idx < 0 || m_idx >= m_span.get_Length())
52 {
53 throw IndexOutOfRangeException(u"index");
54 }
55
56 return m_span.get(m_idx);
57 }
58
61 bool MoveNext()
62 {
63 if (m_idx < m_span.get_Length())
64 {
65 m_idx++;
66 }
67
68 return m_idx < m_span.get_Length();
69 }
70
72 void Reset()
73 {
74 m_idx = -1;
75 }
76 };
77
78public:
80 SpanCore() : SpanCore(nullptr, nullptr, 0)
81 {}
82
85 SpanCore(const ArrayPtrT& array) : SpanCore(array, array == nullptr ? nullptr : array->data_ptr(), array == nullptr ? 0 : array->get_Length())
86 {}
87
91 template <int32_t Size>
92 SpanCore(Details::StackArray<MutableT, Size>& stack_array) : SpanCore(stack_array.data(), Size)
93 {}
94
99 SpanCore(void* pointer, int32_t length) : SpanCore(nullptr, (T*)pointer, length)
100 {
101 if (length < 0)
102 {
103 throw ArgumentOutOfRangeException(u"length");
104 }
105 }
106
112 SpanCore(const ArrayPtrT& array, int32_t start, int32_t length) : SpanCore(array, array->data_ptr() + start, length)
113 {
114 if (array == nullptr)
115 {
116 if (start != 0)
117 {
118 throw ArgumentOutOfRangeException(u"start");
119 }
120 if (length != 0)
121 {
122 throw ArgumentOutOfRangeException(u"length");
123 }
124 }
125 else
126 {
127 if (start < 0 || start >= array->get_Length())
128 {
129 throw ArgumentOutOfRangeException(u"start");
130 }
131 if (start + length > array->get_Length())
132 {
133 throw ArgumentOutOfRangeException(u"length");
134 }
135 }
136 }
137
140 static Span get_Empty()
141 {
142 return Span();
143 }
144
147 Enumerator GetEnumerator() const
148 {
149 return Enumerator(*this);
150 }
151
154 int32_t GetHashCode() const
155 {
156 throw NotSupportedException();
157 }
158
161 bool get_IsEmpty() const
162 {
163 return m_length == 0;
164 }
165
168 int32_t get_Length() const
169 {
170 return m_length;
171 }
172
177 Span Slice(int32_t start) const
178 {
179 return Slice(start, m_length - start);
180 }
181
187 Span Slice(int32_t start, int32_t length) const
188 {
189 if (start < 0)
190 {
191 throw ArgumentOutOfRangeException(u"start");
192 }
193 else if (length < 0 || start + length > m_length)
194 {
195 throw ArgumentOutOfRangeException(u"length");
196 }
197
198 return Span(m_array, m_pointer + start, length);
199 }
200
204 void CopyTo(MutableSpan destination) const
205 {
206 if (m_length > destination.m_length)
207 {
208 throw ArgumentException(u"destination");
209 }
210
211 std::copy(cbegin(), cend(), destination.begin());
212 }
213
217 bool TryCopyTo(MutableSpan destination) const
218 {
219 if (m_length > destination.get_Length())
220 {
221 return false;
222 }
223
224 std::copy(cbegin(), cend(), destination.begin());
225
226 return true;
227 }
228
231 ArrayPtrT ToArray() const
232 {
233 auto result = System::MakeArray<MutableT>(m_length);
234
235 std::copy(cbegin(), cend(), result->begin());
236
237 return result;
238 }
239
243 template <typename T1 = MutableT>
244 typename std::enable_if<std::is_same<T1, char16_t>::value, String>::type ToString() const
245 {
246 return System::String(ToArray());
247 }
248
252 template <typename T1 = MutableT>
253 typename std::enable_if<!std::is_same<T1, char16_t>::value, String>::type ToString() const
254 {
255 return u"System::Span<T>[" + System::Convert::ToString(m_length) + u"]";
256 }
257
258public:
259 // Iterator support
260 T* begin() const noexcept
261 {
262 return m_pointer;
263 }
264
265 T* end() const noexcept
266 {
267 return m_pointer + m_length;
268 }
269
270 const T* cbegin() const noexcept
271 {
272 return m_pointer;
273 }
274
275 const T* cend() const noexcept
276 {
277 return m_pointer + m_length;
278 }
279
280 T* rbegin() const noexcept
281 {
282 return m_pointer + m_length;
283 }
284
285 T* rend() const noexcept
286 {
287 return m_pointer;
288 }
289
290 const T* crbegin() const noexcept
291 {
292 return m_pointer + m_length;
293 }
294
295 const T* crend() const noexcept
296 {
297 return m_pointer;
298 }
299
304 RefrerenceT operator[](int32_t index) const
305 {
306 if (index < 0 || index >= m_length)
307 {
308 throw IndexOutOfRangeException(u"index");
309 }
310
311 return *(m_pointer + index);
312 }
313
316 RefrerenceT get(int32_t index) const
317 {
318 return *(m_pointer + index);
319 }
320
324 bool operator==(const SpanCore& right) const
325 {
326 return m_pointer == right.m_pointer && m_length == right.m_length;
327 }
328
329protected:
334 SpanCore(const ArrayPtrT& array, T* pointer, int32_t length)
335 : m_array(array), m_pointer(pointer), m_length(length)
336 {}
337
338 T* m_pointer;
339 int32_t m_length;
340 ArrayPtrT m_array;
341};
342
343} // namespace Details
344
346template <typename T>
347class ReadOnlySpan;
348
354template <typename T>
355class Span : public Details::SpanCore<T, Span<T>, Span<T>>
356{
357 typedef Span<T> ThisType;
358 typedef typename Details::SpanCore<T, Span<T>, Span<T>> BaseType;
359
360 friend BaseType;
361 friend ReadOnlySpan<T>;
362
363public:
364 using Details::SpanCore<T, Span<T>, Span<T>>::SpanCore;
365
367 void Clear() const
368 {
369 std::fill(this->begin(), this->end(), System::Default<T>());
370 }
371
374 void Fill(const T& value) const
375 {
376 std::fill(this->begin(), this->end(), value);
377 }
378
382 static ThisType to_Span(const typename BaseType::ArrayPtrT& array)
383 {
384 return ThisType(array);
385 }
386};
387
393template <typename T>
394class ReadOnlySpan : public Details::SpanCore<const T, ReadOnlySpan<T>, Span<T>>
395{
397 typedef typename Details::SpanCore<const T, ReadOnlySpan<T>, Span<T>> BaseType;
398
399 friend BaseType;
400
401public:
402 using Details::SpanCore<const T, ReadOnlySpan<T>, Span<T>>::SpanCore;
403
406 ReadOnlySpan(const Span<T>& span) : BaseType(span.m_array, span.m_pointer, span.m_length)
407 {}
408
412 static ThisType to_ReadOnlySpan(const typename BaseType::ArrayPtrT& array)
413 {
414 return ThisType(array);
415 }
416};
417
418} // namespace System
Forward to use within Span class.
Definition: span.h:395
static ThisType to_ReadOnlySpan(const typename BaseType::ArrayPtrT &array)
Converts an array to a ReadOnlySpan.
Definition: span.h:412
ReadOnlySpan(const Span< T > &span)
Constructs a read-only span from a regular span.
Definition: span.h:406
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Represents a contiguous region of arbitrary memory similar to C++20's std::span.
Definition: span.h:356
void Clear() const
Clears the contents of the span by setting all elements to default value.
Definition: span.h:367
static ThisType to_Span(const typename BaseType::ArrayPtrT &array)
Converts an array to a Span.
Definition: span.h:382
void Fill(const T &value) const
Fills the span with the specified value.
Definition: span.h:374
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
Definition: db_command.h:9
std::enable_if< std::is_scalar< T >::value, int >::type GetHashCode(const T &obj)
Returns a hash code for the specified scalar value.
Definition: get_hash_code.h:21
bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:150
static String ToString(int8_t value)
Converts the specified value to its string representation.