CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
list_of_t_converter.h
1#pragma once
2
3#include <system/type_info.h>
4#include <system/object_ext.h>
5#include <system/nullable.h>
6#include <system/details/pointer_collection_helpers.h>
7#include <system/constraints.h>
8#include <system/collections/list.h>
9#include <cstdint>
10
11#include "system/text/json/serialization/converters/collection/i_enumerable_default_converter.h"
12#include "system/text/json/serialization/read_stack.h"
13#include "system/text/json/serialization/read_stack_frame.h"
14#include "system/text/json/reader/utf8_json_reader.h"
15#include "system/text/json/serialization/json_serializer_options.h"
16#include "system/text/json/throw_helper.h"
17#include "system/text/json/writer/utf8_json_writer.h"
18#include "system/text/json/serialization/write_stack.h"
19#include "system/text/json/serialization/write_stack_frame.h"
20#include "system/text/json/serialization/json_converter.h"
21#include "system/text/json/serialization/converters/collection/json_collection_converter.h"
22
23namespace System {
24
25namespace Text {
26
27namespace Json {
28
29namespace Serialization {
30
31namespace Converters {
32
33template<typename TCollection, typename TElement>
34class ListOfTConverter final : public IEnumerableDefaultConverter<TCollection, TElement>
35{
37 assert_is_base_of(BaseT_List, TCollection);
38
41
42 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
43 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
44
45 template<typename FT0, typename FT1> friend class ListOfTConverter;
46
47public:
48
49 bool get_CanPopulate() override
50 {
51 return true;
52 }
53
54 void SetTemplateWeakPtr(uint32_t argument) override
55 {
56 switch (argument)
57 {
58 case 0:
60 break;
61
62 case 1:
64 break;
65
66 }
67 }
68
69protected:
70
71 void Add(const TElement& value, ReadStack& state) override
72 {
73 (System::ExplicitCast<TCollection>(state.Current.ReturnValue))->Add(value);
74 }
75
77 {
78 ASPOSE_UNUSED(reader);
79 ASPOSE_UNUSED(options);
80 if (System::SafeInvoke(state.get_ParentProperty(), [&](auto&& expr){return expr->TryGetPrePopulatedValue(System::Ref(state));}) == true)
81 {
82 return;
83 }
84
85 if (state.Current.JsonTypeInfo->get_HasCreateObject() == false)
86 {
88 }
89
90 state.Current.ReturnValue = state.Current.JsonTypeInfo->CreateObjectInstance();
91 }
92
94 {
96
97 // Using an index is 2x faster than using an enumerator.
98 int32_t index = state.Current.EnumeratorIndex;
100
101 if (elementConverter->get_CanUseDirectReadOrWrite() && state.Current.NumberHandling == nullptr)
102 {
103 // Fast path that avoids validation and extra indirection.
104 for (; index < list->get_Count(); index++)
105 {
106 elementConverter->Write(writer, list->idx_get(index), options);
107 }
108 }
109 else
110 {
111 for (; index < list->get_Count(); index++)
112 {
113 TElement element = list->idx_get(index);
114 if (!elementConverter->TryWrite(writer, element, options, System::Ref(state)))
115 {
117 return false;
118 }
119
121
122 if (this->ShouldFlush(System::Ref(state), writer))
123 {
124 state.Current.EnumeratorIndex = ++index;
125 return false;
126 }
127 }
128 }
129
130 return true;
131 }
132
133};
134
135} // namespace Converters
136} // namespace Serialization
137} // namespace Json
138} // namespace Text
139} // namespace System
List forward declaration.
Definition: list.h:129
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
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonTypeInfoBase > JsonTypeInfo
Definition: read_stack_frame.h:60
Definition: read_stack.h:35
ReadStackFrame Current
Definition: read_stack.h:44
System::SharedPtr< Serialization::Metadata::JsonPropertyInfoBase > get_ParentProperty()
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: i_enumerable_default_converter.h:49
void Add(const TElement &value, ReadStack &state) override
Definition: list_of_t_converter.h:71
void CreateCollection(Utf8JsonReader &reader, ReadStack &state, System::SharedPtr< JsonSerializerOptions > options) override
Definition: list_of_t_converter.h:76
bool get_CanPopulate() override
Definition: list_of_t_converter.h:49
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: list_of_t_converter.h:54
bool OnWriteResume(System::SharedPtr< Utf8JsonWriter > writer, TCollection value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state) override
Definition: list_of_t_converter.h:93
Definition: json_collection_converter.h:43
static System::SharedPtr< JsonConverter< TElement > > GetElementConverter(System::SharedPtr< Metadata::JsonTypeInfoBase > elementTypeInfo)
Definition: json_collection_converter.h:358
static bool ShouldFlush(WriteStack &state, System::SharedPtr< Utf8JsonWriter > writer)
static void ThrowNotSupportedException_SerializationNotSupported(const TypeInfo &propertyType)
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
static auto SafeInvoke(T0 &&expr, T1 &&func)
Implementation of '?.' operator translation.
Definition: object_ext.h:1434