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 <std::size_t N = tuple_size, typename = std::enable_if_t<(N > 0)>>
41 ValueTuple(Args... args) : m_tuple(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) const
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 template<typename ... OtherArgs>
105 bool operator == (const ValueTuple<OtherArgs...>& other) const
106 {
107 return Equals<0, OtherArgs...>(m_tuple, other.m_tuple);
108 }
109
110 bool operator < (const ValueTuple& other) const
111 {
112 return m_tuple < other.m_tuple;
113 }
114 bool operator > (const ValueTuple& other) const
115 {
116 return other.m_tuple < m_tuple;
117 }
118 bool operator <= (const ValueTuple& other) const
119 {
120 return !(other.m_tuple < m_tuple);
121 }
122 bool operator >= (const ValueTuple& other) const
123 {
124 return !(m_tuple < other.m_tuple);
125 }
126 template<typename ... OtherArgs>
128 {
129 m_tuple = otherTuple.m_tuple;
130 return *this;
131 }
132
135 template <typename T>
136 ValueTuple& operator = (const SharedPtr<T>& deconstructiblePtr)
137 {
138 System::Details::apply([&](auto&... args) { deconstructiblePtr->Deconstruct(args...); }, m_tuple);
139 return *this;
140 }
141
142 tuple_t& tuple() {return m_tuple;}
143 const tuple_t& tuple() const {return m_tuple;}
144
145private:
147 template<std::size_t Index = 0, typename... OtherArgs>
148 static typename std::enable_if<Index == sizeof...(OtherArgs), bool>::type
149 Equals(const std::tuple<Args...>& a, const std::tuple<OtherArgs...>& b)
150 {
151 return true;
152 }
153
157 template<std::size_t Index = 0, typename... OtherArgs>
158 static typename std::enable_if<Index < sizeof...(OtherArgs), bool>::type
159 Equals(const std::tuple<Args...>& a, const std::tuple<OtherArgs...>& b)
160 {
161 if (!ObjectExt::Equals(std::get<Index>(a), std::get<Index>(b)))
162 {
163 return false;
164 }
165
166 return Equals<Index + 1, OtherArgs...>(a, b);
167 }
168
170 template<std::size_t Index = 0>
171 static typename std::enable_if<Index == tuple_size, void>::type ToString(const tuple_t& tuple, System::String& result) {}
172 template<std::size_t Index = 0>
173 static typename std::enable_if<Index < tuple_size, void>::type ToString(const tuple_t& tuple, System::String& result)
174 {
175 if (Index > 0)
176 {
177 result += u", ";
178 }
179 result += ObjectExt::ToString(std::get<Index>(tuple));
180 ToString<Index + 1>(tuple, result);
181 }
182
184 template<std::size_t Index = 0>
185 static typename std::enable_if<Index == tuple_size, void>::type GetHashCode(const tuple_t& tuple, int32_t& result) {}
186 template<std::size_t Index = 0>
187 static typename std::enable_if<Index < tuple_size, void>::type GetHashCode(const tuple_t& tuple, int32_t& result)
188 {
189 result ^= (ObjectExt::GetHashCode(std::get<Index>(tuple)) << (Index * 2));
190 GetHashCode<Index + 1>(tuple, result);
191 }
192
193private:
195 tuple_t m_tuple;
196};
197
200template <typename... Args>
202{
203 using ValueT = ValueTuple<Args...>;
204
205 typedef System::BoxedValue<ValueTuple<Args...>> thisType;
206 typedef System::BaseTypesInfo<System::Object, Runtime::CompilerServices::ITuple> baseTypes;
207
209
210 RTTI_INFO_BOXED_CUSTOM(thisType, baseTypes);
211
212public:
215 BoxedValue(const ValueT& value) : m_type_hash(typeid(ValueT).hash_code()), m_value(value)
216 {}
217
220 const System::TypeInfo& GetType() const override
221 {
222 return System::ObjectType::GetType<ValueT>();
223 }
224
229 bool Equals(ptr obj) override
230 {
231 if (!obj || !obj->Is(Type()))
232 return false;
233
234 auto boxed = obj.Cast<BoxedValue<ValueT>>();
235 return m_value == boxed->m_value;
236 }
237
239 int GetHashCode() const override
240 {
241 return m_value.GetHashCode();
242 }
243
245 String ToString() const override
246 {
247 return m_value.ToString();
248 }
249
252 const ValueT& unbox() const
253 {
254 return m_value;
255 }
256
261 template <class V>
262 bool is() const
263 {
264 return typeid(V).hash_code() == m_type_hash;
265 }
266
267protected:
271 SharedPtr<Object> idx_get(int index) const override
272 {
273 if (index >= get_Length())
274 {
275 throw IndexOutOfRangeException(u"ValueTuple");
276 }
277
278 Details::ReturnValueHolder return_value_holder;
279 Details::FindItem(index, m_value.m_tuple, return_value_holder);
280
281 return return_value_holder.value;
282 }
283
285 int32_t get_Length() const override
286 {
287 return m_value.tuple_size;
288 }
289
290private:
291 size_t m_type_hash;
292 ValueT m_value;
293};
294
299template <typename... Args>
300ValueTuple<Args...> MakeTuple(Args... args)
301{
302 return ValueTuple<Args...>(args...);
303}
304
309template <typename... Args>
310ValueTuple<Args...> TieTuple(Args&&... args)
311{
312 return ValueTuple<Args&&...>(std::forward<Args>(args)...);
313}
314
320template <std::size_t N, typename... Args>
321auto Get(const ValueTuple<Args...>& tuple)
322{
323 return tuple.template Item<N>();
324}
325
326} // namespace System
Boxed version of value tuple.
Definition: value_tuple.h:202
bool is() const
Determines if the type of the boxed value represented by the current object is V.
Definition: value_tuple.h:262
const System::TypeInfo & GetType() const override
Gets actual type of object.
Definition: value_tuple.h:220
int GetHashCode() const override
Returns a hash code for the current object.
Definition: value_tuple.h:239
const ValueT & unbox() const
Unboxes the boxed value.
Definition: value_tuple.h:252
bool Equals(ptr obj) override
Determines the equality of the boxed values represented by the current and specified objects.
Definition: value_tuple.h:229
String ToString() const override
Returns the string representation of the boxed value.
Definition: value_tuple.h:245
SharedPtr< Object > idx_get(int index) const override
Returns the element at position index.
Definition: value_tuple.h:271
BoxedValue(const ValueT &value)
Constructs a BoxedValue object that represents the specified value boxed.
Definition: value_tuple.h:215
int32_t get_Length() const override
Returns the number of elemens in this tuple.
Definition: value_tuple.h:285
Represents a boxed value. Objects of this class should only be allocated using System::MakeObject() f...
Definition: boxed_value.h:218
Represents an index into a collection. The index can be from the start or from the end....
Definition: index.h:16
static String ToString(const char_t *obj)
Substitution for C# ToString method to work on any C++ type.
Definition: object_ext.h:103
static int GetHashCode(const T &obj)
Implements GetHashCode() calls; works on both Object subclasses and unrelated types.
Definition: object_ext.h:30
static std::enable_if< IsExceptionWrapper< T >::value, bool >::type Equals(const T &obj, const T2 &another)
Definition: object_ext.h:36
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:161
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:732
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:126
String ToString() const
Wrapper for handling String class in contexts where ToString() is being called on value type objects.
Definition: string.h:511
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
static const TypeInfo & Type()
Returns a reference to the TypeInfo object representing the ValueTuple class type information.
Definition: value_tuple.h:46
bool operator<(const ValueTuple &other) const
Definition: value_tuple.h:110
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
bool operator>(const ValueTuple &other) const
Definition: value_tuple.h:114
const std::tuple_element_t< Index, tuple_t > & Item() const
Gets the value of the ValueTuple object's component.
Definition: value_tuple.h:64
bool operator>=(const ValueTuple &other) const
Definition: value_tuple.h:122
const tuple_t & tuple() const
Definition: value_tuple.h:143
System::String ToString() const
Definition: value_tuple.h:84
int32_t GetHashCode() const
Definition: value_tuple.h:92
bool Equals(const ValueTuple &other) const
Definition: value_tuple.h:79
ValueTuple(Args... args)
Constructs a tuple object.
Definition: value_tuple.h:41
bool operator<=(const ValueTuple &other) const
Definition: value_tuple.h:118
ValueTuple()
Definition: value_tuple.h:34
bool operator==(const ValueTuple< OtherArgs... > &other) const
Definition: value_tuple.h:105
ValueTuple & operator=(const ValueTuple< OtherArgs... > &otherTuple)
Definition: value_tuple.h:127
const TypeInfo & GetType() const
Definition: value_tuple.h:99
tuple_t & tuple()
Definition: value_tuple.h:142
Definition: db_command.h:9
ValueTuple< Args... > MakeTuple(Args... args)
Creates tuple on stack.
Definition: value_tuple.h:300
ValueTuple< Args... > TieTuple(Args &&... args)
Creates tuple bound to some values.
Definition: value_tuple.h:310
auto & Get(T &collection, const Index &index)
Implementation for collection[index] expressions.
Definition: index.h:98
TypeInfo structure for BoxedValue class.
Definition: type_info.h:384
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