CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
i_list_converter.h
1#pragma once
2
3#include <system/type_info.h>
4#include <system/object_ext.h>
5#include <system/func.h>
6#include <system/details/pointer_collection_helpers.h>
7#include <system/constraints.h>
8#include <system/collections/list.h>
9#include <system/collections/ilist_ng.h>
10#include <functional>
11#include <cstdint>
12
13#include "system/text/json/serialization/converters/collection/json_collection_converter.h"
14#include "system/text/json/serialization/json_serializer_options.h"
15#include "system/text/json/serialization/read_stack.h"
16#include "system/text/json/serialization/read_stack_frame.h"
17#include "system/text/json/reader/utf8_json_reader.h"
18#include "system/text/json/throw_helper.h"
19#include "system/text/json/writer/utf8_json_writer.h"
20#include "system/text/json/serialization/write_stack.h"
21#include "system/text/json/serialization/write_stack_frame.h"
22#include "system/text/json/serialization/json_converter.h"
23
24namespace System {
25
26namespace Text {
27
28namespace Json {
29
30namespace Serialization {
31
32namespace Converters {
33
34template<typename TCollection>
35class IListConverter final : public JsonCollectionConverter<TCollection, System::SharedPtr<Object>>
36{
38 assert_is_base_of(BaseT_IList, TCollection);
39
42
43 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
44 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
45
46 template<typename FT0> friend class IListConverter;
47
48public:
49
50 bool get_CanPopulate() override
51 {
52 return true;
53 }
54
56 {
57 ASPOSE_UNUSED(options);
58 // Deserialize as List<object?> for interface types that support it.
59 if (jsonTypeInfo->get_HasCreateObject() == false && JsonConverter<TCollection>::get_Type().IsAssignableFrom(System::ObjectExt::GetType<Collections::Generic::List<System::SharedPtr<Object>>>()))
60 {
61
62 jsonTypeInfo->set_CreateObject(static_cast<Func<System::SharedPtr<Object>>>(static_cast<std::function<System::SharedPtr<System::Object>()>>([]() -> System::SharedPtr<System::Object>
63 {
64 return System::MakeObject<Collections::Generic::List<System::SharedPtr<Object>>>();
65 })));
66 }
67 }
68
69 void SetTemplateWeakPtr(uint32_t argument) override
70 {
71 switch (argument)
72 {
73 case 0:
75 break;
76
77 }
78 }
79
80protected:
81
82 void Add(const System::SharedPtr<Object>& value, ReadStack& state) override
83 {
84 TCollection collection = System::ExplicitCast<TCollection>(state.Current.ReturnValue);
85 collection->Add(value);
86 if (this->get_IsValueType())
87 {
88 state.Current.ReturnValue = System::ExplicitCast<System::Object>(collection);
89 }
90 }
91
93 {
95 TCollection returnValue = System::ExplicitCast<TCollection>(state.Current.ReturnValue);
96 if (returnValue->get_IsReadOnly())
97 {
98 state.Current.ReturnValue.reset();
99 // clear out for more accurate JsonPath reporting.
101 }
102 }
103
105 {
107
108 // Using an index is 2x faster than using an enumerator.
109 int32_t index = state.Current.EnumeratorIndex;
111
112 if (elementConverter->get_CanUseDirectReadOrWrite() && state.Current.NumberHandling == nullptr)
113 {
114 // Fast path that avoids validation and extra indirection.
115 for (; index < list->get_Count(); index++)
116 {
117 elementConverter->Write(writer, list->idx_get(index), options);
118 }
119 }
120 else
121 {
122 for (; index < list->get_Count(); index++)
123 {
124 System::SharedPtr<Object> element = list->idx_get(index);
125 if (!elementConverter->TryWrite(writer, element, options, System::Ref(state)))
126 {
128 return false;
129 }
130
132
133 if (this->ShouldFlush(System::Ref(state), writer))
134 {
135 state.Current.EnumeratorIndex = ++index;
136 return false;
137 }
138 }
139 }
140
141 return true;
142 }
143
144};
145
146} // namespace Converters
147} // namespace Serialization
148} // namespace Json
149} // namespace Text
150} // namespace System
List forward declaration.
Definition: list.h:129
IList Represents a non-generic collection of objects that can be individually accessed by index.
Definition: ilist_ng.h:9
Function delegate. This type should be allocated on stack and passed to functions by value or by refe...
Definition: func.h:82
static std::enable_if< std::is_fundamental< T >::value &&!std::is_enum< T >::value, constSystem::TypeInfo & >::type GetType()
Implements typeof() translation. Overload for primitive types.
Definition: object_type.h:94
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
System::SharedPtr< Object > ReturnValue
Definition: read_stack_frame.h:59
Definition: read_stack.h:35
ReadStackFrame Current
Definition: read_stack.h:44
void CreateCollection(Utf8JsonReader &reader, ReadStack &state, System::SharedPtr< JsonSerializerOptions > options) override
Definition: i_list_converter.h:92
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: i_list_converter.h:69
bool OnWriteResume(System::SharedPtr< Utf8JsonWriter > writer, TCollection value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state) override
Definition: i_list_converter.h:104
bool get_CanPopulate() override
Definition: i_list_converter.h:50
void Add(const System::SharedPtr< Object > &value, ReadStack &state) override
Definition: i_list_converter.h:82
void ConfigureJsonTypeInfo(System::SharedPtr< Metadata::JsonTypeInfoBase > jsonTypeInfo, System::SharedPtr< JsonSerializerOptions > options) override
Definition: i_list_converter.h:55
Definition: json_collection_converter.h:43
static System::SharedPtr< JsonConverter< System::SharedPtr< Object > > > GetElementConverter(System::SharedPtr< Metadata::JsonTypeInfoBase > elementTypeInfo)
Definition: json_collection_converter.h:358
void SetTemplateWeakPtr(uint32_t argument) override
Definition: json_collection_converter.h:316
static bool ShouldFlush(WriteStack &state, System::SharedPtr< Utf8JsonWriter > writer)
Definition: json_converter_of_t.h:80
static void ThrowNotSupportedException_CannotPopulateCollection(const TypeInfo &type, Utf8JsonReader &reader, ReadStack &state)
Definition: utf8_json_reader.h:78
Nullable< Serialization::JsonNumberHandling > NumberHandling
Definition: write_stack_frame.h:66
int32_t EnumeratorIndex
Definition: write_stack_frame.h:61
Definition: write_stack.h:34
WriteStackFrame Current
Definition: write_stack.h:45
@ Text
Defines color adjustment information for text.
Definition: db_command.h:9
decltype(Ref(std::declval< T & >())) Ref(const std::reference_wrapper< T > &wrapper)
Wrapper to make sure Ref(std::ref(DynamicWeakPtr)) works.
Definition: dynamic_weak_ptr.h:207