CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
value_tuple.h
1#pragma once
2#include <tuple>
3#include <system/object_ext.h>
4
5namespace System {
6
9{
12 : TypeInfoPtr(u"System::ValueTuple")
13 {}
14};
15
17template<typename ... Args>
18class ValueTuple : public Details::BoxableObjectBase
19{
21 using tuple_t = std::tuple<Args...>;
23 static constexpr std::size_t tuple_size = sizeof...(Args);
24
25private:
26 template <typename... OtherArgs> friend class ValueTuple;
28
29public:
31 {
32 }
33
36 template <typename = std::enable_if_t<(tuple_size > 0)>>
37 ValueTuple(Args... args) : m_tuple(std::forward<Args>(args)...)
38 {
39 }
40
42 static const TypeInfo& Type()
43 {
44 return *static_holder<ThisTypeInfo>();
45 }
46
49 template <std::size_t Index>
50 std::tuple_element_t<Index, tuple_t>& Item()
51 {
52 static_assert(Index < tuple_size, "Index out of ragnge.");
53
54 return std::get<Index>(m_tuple);
55 }
56
59 template <std::size_t Index>
60 const std::tuple_element_t<Index, tuple_t>& Item() const
61 {
62 static_assert(Index < tuple_size, "Index out of ragnge.");
63
64 return std::get<Index>(m_tuple);
65 }
66
71 {
72 return System::ObjectExt::Is<ValueTuple>(obj) && Equals<0, Args...>(m_tuple, ObjectExt::Unbox<ValueTuple>(obj).m_tuple);
73 }
74
76 {
77 System::String result = u"(";
78 ToString(m_tuple, result);
79 result += u")";
80 return result;
81 }
82
83 int32_t GetHashCode() const
84 {
85 int32_t result = 0;
86 GetHashCode(m_tuple, result);
87 return result;
88 }
89
90 const TypeInfo& GetType() const
91 {
92 return Type();
93 }
94
95 bool operator == (const ValueTuple& other) const
96 {
97 return Equals<0, Args...>(m_tuple, other.m_tuple);
98 }
99
100 template<typename ... OtherArgs>
102 {
103 m_tuple = otherTuple.m_tuple;
104 return *this;
105 }
106
107private:
109 template<std::size_t Index = 0, typename... Types>
110 static typename std::enable_if<Index == sizeof...(Types), bool>::type
111 Equals(const std::tuple<Types...>& a, const std::tuple<Types...>& b)
112 {
113 return true;
114 }
115
119 template<std::size_t Index = 0, typename... Types>
120 static typename std::enable_if<Index < sizeof...(Types), bool>::type
121 Equals(const std::tuple<Types...>& a, const std::tuple<Types...>& b)
122 {
123 if (!ObjectExt::Equals(std::get<Index>(a), std::get<Index>(b)))
124 {
125 return false;
126 }
127
128 return Equals<Index + 1, Types...>(a, b);
129 }
130
132 template<std::size_t Index = 0>
133 static typename std::enable_if<Index == tuple_size, void>::type ToString(const tuple_t& tuple, System::String& result) {}
134 template<std::size_t Index = 0>
135 static typename std::enable_if<Index < tuple_size, void>::type ToString(const tuple_t& tuple, System::String& result)
136 {
137 if (Index > 0)
138 {
139 result += u", ";
140 }
141 result += ObjectExt::ToString(std::get<Index>(tuple));
142 ToString<Index + 1>(tuple, result);
143 }
144
146 template<std::size_t Index = 0>
147 static typename std::enable_if<Index == tuple_size, void>::type GetHashCode(const tuple_t& tuple, int32_t& result) {}
148 template<std::size_t Index = 0>
149 static typename std::enable_if<Index < tuple_size, void>::type GetHashCode(const tuple_t& tuple, int32_t& result)
150 {
151 result ^= (ObjectExt::GetHashCode(std::get<Index>(tuple)) << (Index * 2));
152 GetHashCode<Index + 1>(tuple, result);
153 }
154
155private:
157 tuple_t m_tuple;
158};
159
164template <typename... Args>
165static ValueTuple<Args...> MakeTuple(Args... args)
166{
167 return ValueTuple<Args...>(args...);
168}
169
174template <typename... Args>
175static ValueTuple<Args...> TieTuple(Args&&... args)
176{
177 return ValueTuple<Args&&...>(std::forward<Args>(args)...);
178}
179
180} // namespace System
static String ToString(const char_t *obj)
Substitution for C# ToString method to work on any C++ type.
Definition: object_ext.h:96
static int GetHashCode(const T &obj)
Implements GetHashCode() calls; works on both Object subclasses and unrelated types.
Definition: object_ext.h:28
static std::enable_if< IsExceptionWrapper< T >::value, bool >::type Equals(const T &obj, const T2 &another)
Definition: object_ext.h:34
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
Represents a particular type and provides information about it.
Definition: type_info.h:109
Class that represents a ValueTuple data structure.
Definition: value_tuple.h:19
bool operator==(const ValueTuple &other) const
Definition: value_tuple.h:95
static const TypeInfo & Type()
Returns a reference to the TypeInfo object representing the ValueTuple class type information.
Definition: value_tuple.h:42
ValueTuple(Args... args)
Constructs a tuple object.
Definition: value_tuple.h:37
std::tuple_element_t< Index, tuple_t > & Item()
Gets the reference to value of the ValueTuple object's component.
Definition: value_tuple.h:50
bool Equals(SharedPtr< Object > obj)
Determines if the current and the specified objects are identical.
Definition: value_tuple.h:70
const std::tuple_element_t< Index, tuple_t > & Item() const
Gets the value of the ValueTuple object's component.
Definition: value_tuple.h:60
System::String ToString() const
Definition: value_tuple.h:75
int32_t GetHashCode() const
Definition: value_tuple.h:83
ValueTuple()
Definition: value_tuple.h:30
ValueTuple & operator=(const ValueTuple< OtherArgs... > &otherTuple)
Definition: value_tuple.h:101
const TypeInfo & GetType() const
Definition: value_tuple.h:90
Definition: db_command.h:9
static ValueTuple< Args... > MakeTuple(Args... args)
Creates tuple on stack.
Definition: value_tuple.h:165
static ValueTuple< Args... > TieTuple(Args &&... args)
Creates tuple bound to some values.
Definition: value_tuple.h:175
Wrapper for a pointer to an instance of TypeInfo class. This type should be allocated on stack and pa...
Definition: type_info.h:72
Represents a pointer to TypeInfo object that contains information about ValueTuple class.
Definition: value_tuple.h:9
ValueTupleTypeInfo()
Constructs an instance of MulticastDelegateTypeInfo class.
Definition: value_tuple.h:11