CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
value_tuple.h
1#pragma once
2#include <tuple>
3#include <system/object_ext.h>
4#include <system/tuple.h>
5#include <system/details/apply.h>
6#include <system/runtime/compiler_services/ituple.h>
7
8namespace System {
9
12{
15 : TypeInfoPtr(u"System::ValueTuple")
16 {}
17};
18
20template<typename ... Args>
21class ValueTuple : public Details::BoxableObjectBase
22{
24 using tuple_t = std::tuple<Args...>;
26 static constexpr std::size_t tuple_size = sizeof...(Args);
27
28private:
29 template <typename... OtherArgs> friend class ValueTuple;
30 friend class BoxedValue<ValueTuple<Args...>>;
32
33public:
35 {
36 }
37
40 template <typename = std::enable_if_t<(tuple_size > 0)>>
41 ValueTuple(Args... args) : m_tuple(std::forward<Args>(args)...)
42 {
43 }
44
46 static const TypeInfo& Type()
47 {
48 return *static_holder<ThisTypeInfo>();
49 }
50
53 template <std::size_t Index>
54 std::tuple_element_t<Index, tuple_t>& Item()
55 {
56 static_assert(Index < tuple_size, "Index out of ragnge.");
57
58 return std::get<Index>(m_tuple);
59 }
60
63 template <std::size_t Index>
64 const std::tuple_element_t<Index, tuple_t>& Item() const
65 {
66 static_assert(Index < tuple_size, "Index out of ragnge.");
67
68 return std::get<Index>(m_tuple);
69 }
70
75 {
76 return System::ObjectExt::Is<ValueTuple>(obj) && Equals<0, Args...>(m_tuple, ObjectExt::Unbox<ValueTuple>(obj).m_tuple);
77 }
78
79 bool Equals(const ValueTuple& other)
80 {
81 return *this == other;
82 }
83
85 {
86 System::String result = u"(";
87 ToString(m_tuple, result);
88 result += u")";
89 return result;
90 }
91
92 int32_t GetHashCode() const
93 {
94 int32_t result = 0;
95 GetHashCode(m_tuple, result);
96 return result;
97 }
98
99 const TypeInfo& GetType() const
100 {
101 return Type();
102 }
103
104 bool operator == (const ValueTuple& other) const
105 {
106 return Equals<0, Args...>(m_tuple, other.m_tuple);
107 }
108
109 template<typename ... OtherArgs>
111 {
112 m_tuple = otherTuple.m_tuple;
113 return *this;
114 }
115
118 template <typename T>
119 ValueTuple& operator = (const SharedPtr<T>& deconstructiblePtr)
120 {
121 System::Details::apply([&](auto&... args) { deconstructiblePtr->Deconstruct(args...); }, m_tuple);
122 return *this;
123 }
124
125 tuple_t& tuple() {return m_tuple;}
126 const tuple_t& tuple() const {return m_tuple;}
127
128private:
130 template<std::size_t Index = 0, typename... Types>
131 static typename std::enable_if<Index == sizeof...(Types), bool>::type
132 Equals(const std::tuple<Types...>& a, const std::tuple<Types...>& b)
133 {
134 return true;
135 }
136
140 template<std::size_t Index = 0, typename... Types>
141 static typename std::enable_if<Index < sizeof...(Types), bool>::type
142 Equals(const std::tuple<Types...>& a, const std::tuple<Types...>& b)
143 {
144 if (!ObjectExt::Equals(std::get<Index>(a), std::get<Index>(b)))
145 {
146 return false;
147 }
148
149 return Equals<Index + 1, Types...>(a, b);
150 }
151
153 template<std::size_t Index = 0>
154 static typename std::enable_if<Index == tuple_size, void>::type ToString(const tuple_t& tuple, System::String& result) {}
155 template<std::size_t Index = 0>
156 static typename std::enable_if<Index < tuple_size, void>::type ToString(const tuple_t& tuple, System::String& result)
157 {
158 if (Index > 0)
159 {
160 result += u", ";
161 }
162 result += ObjectExt::ToString(std::get<Index>(tuple));
163 ToString<Index + 1>(tuple, result);
164 }
165
167 template<std::size_t Index = 0>
168 static typename std::enable_if<Index == tuple_size, void>::type GetHashCode(const tuple_t& tuple, int32_t& result) {}
169 template<std::size_t Index = 0>
170 static typename std::enable_if<Index < tuple_size, void>::type GetHashCode(const tuple_t& tuple, int32_t& result)
171 {
172 result ^= (ObjectExt::GetHashCode(std::get<Index>(tuple)) << (Index * 2));
173 GetHashCode<Index + 1>(tuple, result);
174 }
175
176private:
178 tuple_t m_tuple;
179};
180
183template <typename... Args>
185{
186 using ValueT = ValueTuple<Args...>;
187
188 typedef System::BoxedValue<ValueTuple<Args...>> thisType;
189 typedef System::BaseTypesInfo<System::Object, Runtime::CompilerServices::ITuple> baseTypes;
190
192
193 RTTI_INFO_BOXED_CUSTOM(thisType, baseTypes);
194
195public:
198 BoxedValue(const ValueT& value) : m_type_hash(typeid(ValueT).hash_code()), m_value(value)
199 {}
200
203 const System::TypeInfo& GetType() const override
204 {
205 return System::ObjectType::GetType<ValueT>();
206 }
207
212 bool Equals(ptr obj) override
213 {
214 if (!obj || !obj->Is(Type()))
215 return false;
216
217 auto boxed = obj.Cast<BoxedValue<ValueT>>();
218 return m_value == boxed->m_value;
219 }
220
222 int GetHashCode() const override
223 {
224 return m_value.GetHashCode();
225 }
226
228 String ToString() const override
229 {
230 return m_value.ToString();
231 }
232
235 const ValueT& unbox() const
236 {
237 return m_value;
238 }
239
244 template <class V>
245 bool is() const
246 {
247 return typeid(V).hash_code() == m_type_hash;
248 }
249
250protected:
254 SharedPtr<Object> idx_get(int index) const override
255 {
256 if (index >= get_Length())
257 {
258 throw IndexOutOfRangeException(u"ValueTuple");
259 }
260
261 Details::ReturnValueHolder return_value_holder;
262 Details::FindItem(index, m_value.m_tuple, return_value_holder);
263
264 return return_value_holder.value;
265 }
266
268 int32_t get_Length() const override
269 {
270 return m_value.tuple_size;
271 }
272
273private:
274 size_t m_type_hash;
275 ValueT m_value;
276};
277
282template <typename... Args>
283ValueTuple<Args...> MakeTuple(Args... args)
284{
285 return ValueTuple<Args...>(args...);
286}
287
292template <typename... Args>
293ValueTuple<Args...> TieTuple(Args&&... args)
294{
295 return ValueTuple<Args&&...>(std::forward<Args>(args)...);
296}
297
303template <std::size_t N, typename... Args>
304auto Get(const ValueTuple<Args...>& tuple)
305{
306 return tuple.template Item<N>();
307}
308
309} // namespace System
Boxed version of value tuple.
Definition: value_tuple.h:185
bool is() const
Determines if the type of the boxed value represented by the current object is V.
Definition: value_tuple.h:245
const System::TypeInfo & GetType() const override
Gets actual type of object.
Definition: value_tuple.h:203
int GetHashCode() const override
Returns a hash code for the current object.
Definition: value_tuple.h:222
const ValueT & unbox() const
Unboxes the boxed value.
Definition: value_tuple.h:235
bool Equals(ptr obj) override
Determines the equality of the boxed values represented by the current and specified objects.
Definition: value_tuple.h:212
String ToString() const override
Returns the string representation of the boxed value.
Definition: value_tuple.h:228
SharedPtr< Object > idx_get(int index) const override
Returns the element at position index.
Definition: value_tuple.h:254
BoxedValue(const ValueT &value)
Constructs a BoxedValue object that represents the specified value boxed.
Definition: value_tuple.h:198
int32_t get_Length() const override
Returns the number of elemens in this tuple.
Definition: value_tuple.h:268
Represents a boxed value. Objects of this class should only be allocated using System::MakeObject() f...
Definition: boxed_value.h:172
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
virtual bool Is(const TypeInfo &targetType) const
Check if object represents an instance of type described by targetType. Analog of C# 'is' operator.
static const TypeInfo & Type()
Implements C# typeof(System.Object) construct.
Definition: object.h:172
Defines a general-purpose Tuple implementation that allows access to Tuple instance members without k...
Definition: ituple.h:11
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
std::enable_if_t< std::is_same< Y, T >::value, SmartPtr< Y > > Cast() const
Casts pointer to its type itself.
Definition: smart_ptr.h:742
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
String ToString() const
Wrapper for handling String class in contexts where ToString() is being called on value type objects.
Definition: string.h:504
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:22
bool operator==(const ValueTuple &other) const
Definition: value_tuple.h:104
static const TypeInfo & Type()
Returns a reference to the TypeInfo object representing the ValueTuple class type information.
Definition: value_tuple.h:46
ValueTuple(Args... args)
Constructs a tuple object.
Definition: value_tuple.h:41
std::tuple_element_t< Index, tuple_t > & Item()
Gets the reference to value of the ValueTuple object's component.
Definition: value_tuple.h:54
bool Equals(SharedPtr< Object > obj)
Determines if the current and the specified objects are identical.
Definition: value_tuple.h:74
const std::tuple_element_t< Index, tuple_t > & Item() const
Gets the value of the ValueTuple object's component.
Definition: value_tuple.h:64
const tuple_t & tuple() const
Definition: value_tuple.h:126
System::String ToString() const
Definition: value_tuple.h:84
int32_t GetHashCode() const
Definition: value_tuple.h:92
ValueTuple()
Definition: value_tuple.h:34
ValueTuple & operator=(const ValueTuple< OtherArgs... > &otherTuple)
Definition: value_tuple.h:110
const TypeInfo & GetType() const
Definition: value_tuple.h:99
bool Equals(const ValueTuple &other)
Definition: value_tuple.h:79
tuple_t & tuple()
Definition: value_tuple.h:125
Definition: db_command.h:9
ValueTuple< Args... > MakeTuple(Args... args)
Creates tuple on stack.
Definition: value_tuple.h:283
ValueTuple< Args... > TieTuple(Args &&... args)
Creates tuple bound to some values.
Definition: value_tuple.h:293
auto Get(const ValueTuple< Args... > &tuple)
Gets N-th element of value tuple.
Definition: value_tuple.h:304
TypeInfo structure for BoxedValue class.
Definition: type_info.h:367
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:12
ValueTupleTypeInfo()
Constructs an instance of MulticastDelegateTypeInfo class.
Definition: value_tuple.h:14