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
80 {
81 System::String result = u"(";
82 ToString(m_tuple, result);
83 result += u")";
84 return result;
85 }
86
87 int32_t GetHashCode() const
88 {
89 int32_t result = 0;
90 GetHashCode(m_tuple, result);
91 return result;
92 }
93
94 const TypeInfo& GetType() const
95 {
96 return Type();
97 }
98
99 bool operator == (const ValueTuple& other) const
100 {
101 return Equals<0, Args...>(m_tuple, other.m_tuple);
102 }
103
104 template<typename ... OtherArgs>
106 {
107 m_tuple = otherTuple.m_tuple;
108 return *this;
109 }
110
113 template <typename T>
114 ValueTuple& operator = (const SharedPtr<T>& deconstructiblePtr)
115 {
116 System::Details::apply([&](auto&... args) { deconstructiblePtr->Deconstruct(args...); }, m_tuple);
117 return *this;
118 }
119
120 tuple_t& tuple() {return m_tuple;}
121 const tuple_t& tuple() const {return m_tuple;}
122
123private:
125 template<std::size_t Index = 0, typename... Types>
126 static typename std::enable_if<Index == sizeof...(Types), bool>::type
127 Equals(const std::tuple<Types...>& a, const std::tuple<Types...>& b)
128 {
129 return true;
130 }
131
135 template<std::size_t Index = 0, typename... Types>
136 static typename std::enable_if<Index < sizeof...(Types), bool>::type
137 Equals(const std::tuple<Types...>& a, const std::tuple<Types...>& b)
138 {
139 if (!ObjectExt::Equals(std::get<Index>(a), std::get<Index>(b)))
140 {
141 return false;
142 }
143
144 return Equals<Index + 1, Types...>(a, b);
145 }
146
148 template<std::size_t Index = 0>
149 static typename std::enable_if<Index == tuple_size, void>::type ToString(const tuple_t& tuple, System::String& result) {}
150 template<std::size_t Index = 0>
151 static typename std::enable_if<Index < tuple_size, void>::type ToString(const tuple_t& tuple, System::String& result)
152 {
153 if (Index > 0)
154 {
155 result += u", ";
156 }
157 result += ObjectExt::ToString(std::get<Index>(tuple));
158 ToString<Index + 1>(tuple, result);
159 }
160
162 template<std::size_t Index = 0>
163 static typename std::enable_if<Index == tuple_size, void>::type GetHashCode(const tuple_t& tuple, int32_t& result) {}
164 template<std::size_t Index = 0>
165 static typename std::enable_if<Index < tuple_size, void>::type GetHashCode(const tuple_t& tuple, int32_t& result)
166 {
167 result ^= (ObjectExt::GetHashCode(std::get<Index>(tuple)) << (Index * 2));
168 GetHashCode<Index + 1>(tuple, result);
169 }
170
171private:
173 tuple_t m_tuple;
174};
175
178template <typename... Args>
180{
181 using ValueT = ValueTuple<Args...>;
182
183 typedef System::BoxedValue<ValueTuple<Args...>> thisType;
184 typedef System::BaseTypesInfo<System::Object, Runtime::CompilerServices::ITuple> baseTypes;
185
187
188 RTTI_INFO_BOXED_CUSTOM(thisType, baseTypes);
189
190public:
193 BoxedValue(const ValueT& value) : m_type_hash(typeid(ValueT).hash_code()), m_value(value)
194 {}
195
198 const System::TypeInfo& GetType() const override
199 {
200 return System::ObjectType::GetType<ValueT>();
201 }
202
207 bool Equals(ptr obj) override
208 {
209 if (!obj || !obj->Is(Type()))
210 return false;
211
212 auto boxed = obj.Cast<BoxedValue<ValueT>>();
213 return m_value == boxed->m_value;
214 }
215
217 int GetHashCode() const override
218 {
219 return m_value.GetHashCode();
220 }
221
223 String ToString() const override
224 {
225 return m_value.ToString();
226 }
227
230 const ValueT& unbox() const
231 {
232 return m_value;
233 }
234
239 template <class V>
240 bool is() const
241 {
242 return typeid(V).hash_code() == m_type_hash;
243 }
244
245protected:
249 SharedPtr<Object> idx_get(int index) const override
250 {
251 if (index >= get_Length())
252 {
253 throw IndexOutOfRangeException(u"ValueTuple");
254 }
255
256 Details::ReturnValueHolder return_value_holder;
257 Details::FindItem(index, m_value.m_tuple, return_value_holder);
258
259 return return_value_holder.value;
260 }
261
263 int32_t get_Length() const override
264 {
265 return m_value.tuple_size;
266 }
267
268private:
269 size_t m_type_hash;
270 ValueT m_value;
271};
272
277template <typename... Args>
278ValueTuple<Args...> MakeTuple(Args... args)
279{
280 return ValueTuple<Args...>(args...);
281}
282
287template <typename... Args>
288ValueTuple<Args...> TieTuple(Args&&... args)
289{
290 return ValueTuple<Args&&...>(std::forward<Args>(args)...);
291}
292
298template <std::size_t N, typename... Args>
299auto Get(const ValueTuple<Args...>& tuple)
300{
301 return tuple.template Item<N>();
302}
303
304} // namespace System
Boxed version of value tuple.
Definition: value_tuple.h:180
bool is() const
Determines if the type of the boxed value represented by the current object is V.
Definition: value_tuple.h:240
const System::TypeInfo & GetType() const override
Gets actual type of object.
Definition: value_tuple.h:198
int GetHashCode() const override
Returns a hash code for the current object.
Definition: value_tuple.h:217
const ValueT & unbox() const
Unboxes the boxed value.
Definition: value_tuple.h:230
bool Equals(ptr obj) override
Determines the equality of the boxed values represented by the current and specified objects.
Definition: value_tuple.h:207
String ToString() const override
Returns the string representation of the boxed value.
Definition: value_tuple.h:223
SharedPtr< Object > idx_get(int index) const override
Returns the element at position index.
Definition: value_tuple.h:249
BoxedValue(const ValueT &value)
Constructs a BoxedValue object that represents the specified value boxed.
Definition: value_tuple.h:193
int32_t get_Length() const override
Returns the number of elemens in this tuple.
Definition: value_tuple.h:263
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:99
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:121
System::String ToString() const
Definition: value_tuple.h:79
int32_t GetHashCode() const
Definition: value_tuple.h:87
ValueTuple()
Definition: value_tuple.h:34
ValueTuple & operator=(const ValueTuple< OtherArgs... > &otherTuple)
Definition: value_tuple.h:105
const TypeInfo & GetType() const
Definition: value_tuple.h:94
tuple_t & tuple()
Definition: value_tuple.h:120
Definition: db_command.h:9
ValueTuple< Args... > MakeTuple(Args... args)
Creates tuple on stack.
Definition: value_tuple.h:278
ValueTuple< Args... > TieTuple(Args &&... args)
Creates tuple bound to some values.
Definition: value_tuple.h:288
auto Get(const ValueTuple< Args... > &tuple)
Gets N-th element of value tuple.
Definition: value_tuple.h:299
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