CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
index.h
1
2#pragma once
3
4#include <cstdint>
5#include <system/object.h>
6#include <system/exceptions.h>
7#include <system/details/dereference.h>
8#include <system/details/slicing.h>
9
10namespace System {
11
15class ASPOSECPP_SHARED_CLASS Index : public ::System::Details::BoxableObjectBase
16{
17 RTTI_INFO_VALUE_TYPE(Index)
18
19public:
21 constexpr Index() noexcept : m_value(0), m_is_from_end(false) {}
22
25 constexpr Index(int32_t value) noexcept : m_value(value), m_is_from_end(false) {}
26
30 constexpr Index(int32_t value, bool from_end) noexcept : m_value(value), m_is_from_end(from_end) {}
31
35 static constexpr Index FromEnd(int32_t value) noexcept
36 {
37 return Index(value, true);
38 }
39
41 static constexpr Index get_Start() noexcept
42 {
43 return Index(0);
44 }
45
47 static constexpr Index get_End() noexcept
48 {
49 return Index(0, true);
50 }
51
53 constexpr int32_t get_Value() const noexcept
54 {
55 return m_value;
56 }
57
59 constexpr bool get_IsFromEnd() const noexcept
60 {
61 return m_is_from_end;
62 }
63
67 int32_t GetOffset(int32_t length) const
68 {
69 return m_is_from_end ? length - m_value : m_value;
70 }
71
75 bool Equals(const Index& other) const
76 {
77 return m_value == other.m_value && m_is_from_end == other.m_is_from_end;
78 }
79
82 int32_t GetHashCode() const
83 {
84 return m_value;
85 }
86
87private:
88 int32_t m_value;
89 bool m_is_from_end;
90};
91
97template <typename T>
98auto& Get(T& collection, const Index& index)
99{
100 auto& ref = Details::Dereference(collection);
101 auto length = Details::GetCollectionSize(ref);
102 auto offset = index.GetOffset(length);
103 return ref[offset];
104}
105
106} // namespace System
Represents an index into a collection. The index can be from the start or from the end....
Definition: index.h:16
constexpr Index() noexcept
Constructs an instance that represents the start of a collection.
Definition: index.h:21
constexpr Index(int32_t value) noexcept
Constructs an instance that represents the specified position from the start of a collection.
Definition: index.h:25
int32_t GetHashCode() const
Returns a hash code for the current index.
Definition: index.h:82
constexpr bool get_IsFromEnd() const noexcept
Gets a value that indicates whether the index is from the end.
Definition: index.h:59
constexpr Index(int32_t value, bool from_end) noexcept
Constructs an instance that represents the specified index.
Definition: index.h:30
constexpr int32_t get_Value() const noexcept
Gets the index value.
Definition: index.h:53
int32_t GetOffset(int32_t length) const
Converts the current Index into an offset from the start of a collection with the specified length.
Definition: index.h:67
bool Equals(const Index &other) const
Determines whether the current instance and the specified Index represent the same position.
Definition: index.h:75
static constexpr Index FromEnd(int32_t value) noexcept
Creates an Index that is relative to the end of the collection.
Definition: index.h:35
static constexpr Index get_Start() noexcept
Gets an Index object representing the start of a collection.
Definition: index.h:41
static constexpr Index get_End() noexcept
Gets an Index object representing the end of a collection.
Definition: index.h:47
Definition: db_command.h:9
auto & Get(T &collection, const Index &index)
Implementation for collection[index] expressions.
Definition: index.h:98