CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
http_header_value_collection.h
1
2#pragma once
3
4#include <system/action.h>
5#include <system/collections/icollection.h>
6#include <system/constraints.h>
7#include <system/details/pointer_collection_helpers.h>
8#include <system/object.h>
9#include <system/shared_ptr.h>
10#include <system/special_casts.h>
11#include <system/string.h>
12
13#include <net/http/headers/http_headers.h>
14
15namespace System { namespace Net { namespace Http { namespace Headers {
16
22template <typename T>
23class ASPOSECPP_SHARED_CLASS HttpHeaderValueCollection final : public System::Collections::Generic::ICollection<T>
24{
26 assert_is_cs_class(T);
27
32
34 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
36 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
37
38public:
40 int32_t get_Count() const override
41 {
42 return GetCount();
43 }
44
48 {
49 return false;
50 }
51
55 {
56 // If this collection instance has a "special value", then check whether that value was already set.
57 if (_specialValue == nullptr)
58 {
59 return false;
60 }
61 return _store->ContainsParsedValue(_headerName, _specialValue);
62 }
63
68 : HttpHeaderValueCollection(headerName, store, nullptr,
69 static_cast<Action<System::SharedPtr<HttpHeaderValueCollection<T>>, T>>(nullptr))
70 {}
71
78 : HttpHeaderValueCollection(headerName, store, nullptr,
79 static_cast<Action<System::SharedPtr<HttpHeaderValueCollection<T>>, T>>(validator))
80 {}
81
87 : HttpHeaderValueCollection(headerName, store, specialValue,
88 static_cast<Action<System::SharedPtr<HttpHeaderValueCollection<T>>, T>>(nullptr))
89 {}
90
98 : _specialValue(T())
99 {
100 _store = store;
101 _headerName = headerName;
102 _specialValue = specialValue;
103 _validator = validator;
104 }
105
107 void Add(const T& item) override
108 {
109 CheckValue(item);
110 _store->AddParsedValue(_headerName, item);
111 }
112
115 void ParseAdd(String input)
116 {
117 _store->Add(_headerName, input);
118 }
119
123 bool TryParseAdd(String input)
124 {
125 return _store->TryParseAndAddValue(_headerName, input);
126 }
127
129 void Clear() override
130 {
131 _store->Remove(_headerName);
132 }
133
135 bool Contains(const T& item) const override
136 {
137 CheckValue(item);
138 return _store->ContainsParsedValue(_headerName, item);
139 }
140
142 void CopyTo(System::ArrayPtr<T> array, int32_t arrayIndex) override
143 {
144 if (array == nullptr)
145 {
146 throw ArgumentNullException(u"array");
147 }
148 // Allow arrayIndex == array.Length in case our own collection is empty
149 if ((arrayIndex < 0) || (arrayIndex > array->get_Length()))
150 {
151 throw ArgumentOutOfRangeException(u"arrayIndex");
152 }
153
154 System::SharedPtr<Object> storeValue = _store->GetParsedValues(_headerName);
155
156 if (storeValue == nullptr)
157 {
158 return;
159 }
160
163
164 if (storeValues == nullptr)
165 {
166 // We only have 1 value: If it is the "special value" just return, otherwise add the value to the
167 // array and return.
168 if (arrayIndex == array->get_Length())
169 {
170 throw ArgumentException(u"net_http_copyto_array_too_small");
171 }
172 array[arrayIndex] = nullptr;
173 }
174 else
175 {
176 storeValues->CopyTo(System::StaticCastArray<System::SharedPtr<Object>>(array), arrayIndex);
177 }
178 }
179
181 bool Remove(const T& item) override
182 {
183 CheckValue(item);
184 return _store->RemoveParsedValue(_headerName, item);
185 }
186
189 {
190 auto list = System::MakeObject<Collections::Generic::List<T>>();
191 while (true)
192 {
193
194 System::SharedPtr<Object> storeValue = _store->GetParsedValues(_headerName);
195
196 if (storeValue == nullptr)
197 {
198 // yield break;
199 break;
200 }
201
204
205 if (storeValues == nullptr)
206 {
207 CODEPORTING_DEBUG_ASSERT1(System::ObjectExt::Is<T>(storeValue));
208 // T result = storeValue;// System::ExplicitCast<T>(storeValue);
209 T result = System::ExplicitCast<typename T::Pointee_>(storeValue);
210 list->Add(result);
211 }
212 else
213 {
214 // We have multiple values. Iterate through the values and return them.
215 auto item_enumerator = (storeValues)->GetEnumerator();
216 while (item_enumerator->MoveNext())
217 {
218 auto&& item = item_enumerator->get_Current();
219 CODEPORTING_DEBUG_ASSERT1(System::ObjectExt::Is<T>(item));
220 // T result = item;// System::ExplicitCast<T>(item);
221 T result = System::ExplicitCast<typename T::Pointee_>(item);
222 // T result = System::AsCast<typename T::Pointee_>(item);
223 // yield return item as T;
224 list->Add(result);
225 }
226 }
227 break;
228 }
229
230 return list->GetEnumerator();
231 }
232
234 String ToString() const override
235 {
236 return _store->GetHeaderString(_headerName);
237 }
238
242 {
243 if (!get_IsSpecialValueSet())
244 {
245 return ToString();
246 }
247 return _store->GetHeaderString(_headerName, _specialValue);
248 }
249
252 {
253
254 if (!_store->ContainsParsedValue(_headerName, _specialValue))
255 {
256 _store->AddParsedValue(_headerName, _specialValue);
257 }
258 }
259
262 {
263
264 // We're not interested in the return value. It's OK if the "special value" wasn't in the store
265 // before calling RemoveParsedValue().
266 _store->RemoveParsedValue(_headerName, _specialValue);
267 }
268
270 void SetTemplateWeakPtr(uint32_t argument) override
271 {
272 switch (argument)
273 {
274 case 0:
275 System::Details::CollectionHelpers::SetWeakPointer(_specialValue);
276 System::Details::CollectionHelpers::SetWeakPointer(0, _validator);
277 break;
278 }
279 }
280
281private:
283 String _headerName;
287 T _specialValue;
290
294 void CheckValue(const T& item) const
295 {
296 if (item == nullptr)
297 {
298 throw ArgumentNullException(u"item");
299 }
300
301 // If this instance has a custom validator for validating arguments, call it now.
302 if (_validator != nullptr)
303 {
304 _validator(System::MakeSharedPtr(this), item);
305 }
306 }
307
310 int32_t GetCount() const
311 {
312 // This is an O(n) operation.
313
314 System::SharedPtr<Object> storeValue = _store->GetParsedValues(_headerName);
315
316 if (storeValue == nullptr)
317 {
318 return 0;
319 }
320
323
324 if (storeValues == nullptr)
325 {
326 return 1;
327 }
328 else
329 {
330 return storeValues->get_Count();
331 }
332 }
333};
334
339template <>
341{
344
345 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
346 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
347
348public:
349 int32_t get_Count() const override
350 {
351 return GetCount();
352 }
353
355 {
356 return false;
357 }
358
360 {
361 // If this collection instance has a "special value", then check whether that value was already set.
362 if (_specialValue == nullptr)
363 {
364 return false;
365 }
366 return _store->ContainsParsedValue(_headerName, _specialValue);
367 }
368
371 headerName, store, nullptr,
373 {}
374
378 headerName, store, nullptr,
379 static_cast<Action<System::SharedPtr<HttpHeaderValueCollection<String>>, String>>(validator))
380 {}
381
384 headerName, store, specialValue,
386 {}
387
390 //: _specialValue(nullptr)
391 {
392 _store = store;
393 _headerName = headerName;
394 _specialValue = ObjectExt::Box(specialValue);
395 _validator = validator;
396 }
397
398 void Add(const String& item) override
399 {
400 CheckValue(item);
401 _store->AddParsedValue(_headerName, ObjectExt::Box(item));
402 }
403
404 void ParseAdd(String input)
405 {
406 _store->Add(_headerName, input);
407 }
408
409 bool TryParseAdd(String input)
410 {
411 return _store->TryParseAndAddValue(_headerName, input);
412 }
413
414 void Clear() override
415 {
416 _store->Remove(_headerName);
417 }
418
419 bool Contains(const String& item) const override
420 {
421 CheckValue(item);
422 return _store->ContainsParsedValue(_headerName, ObjectExt::Box(item));
423 }
424
425 void CopyTo(System::ArrayPtr<String> array, int32_t arrayIndex) override
426 {
427 if (array == nullptr)
428 {
429 throw ArgumentNullException(u"array");
430 }
431 // Allow arrayIndex == array.Length in case our own collection is empty
432 if ((arrayIndex < 0) || (arrayIndex > array->get_Length()))
433 {
434 throw ArgumentOutOfRangeException(u"arrayIndex");
435 }
436
437 System::SharedPtr<Object> storeValue = _store->GetParsedValues(_headerName);
438
439 if (storeValue == nullptr)
440 {
441 return;
442 }
443
446
447 if (storeValues == nullptr)
448 {
449 // We only have 1 value: If it is the "special value" just return, otherwise add the value to the
450 // array and return.
451 if (arrayIndex == array->get_Length())
452 {
453 throw ArgumentException(u"net_http_copyto_array_too_small");
454 }
455 array[arrayIndex] = nullptr;
456 }
457 else
458 {
459 // storeValues->CopyTo(System::StaticCastArray<System::SharedPtr<Object>>(array), arrayIndex);
460 }
461 }
462
463 bool Remove(const String& item) override
464 {
465 CheckValue(item);
466 return _store->RemoveParsedValue(_headerName, ObjectExt::Box(item));
467 }
468
470 {
471 auto list = System::MakeObject<Collections::Generic::List<String>>();
472 while (true)
473 {
474
475 System::SharedPtr<Object> storeValue = _store->GetParsedValues(_headerName);
476
477 if (storeValue == nullptr)
478 {
479 // yield break;
480 break;
481 }
482
485
486 if (storeValues == nullptr)
487 {
488 CODEPORTING_DEBUG_ASSERT1(System::ObjectExt::Is<String>(storeValue));
489 String result = ObjectExt::Unbox<String>(storeValue);
490 // T result = System::ExplicitCast<T>(storeValue);
491 list->Add(result);
492 }
493 else
494 {
495 // We have multiple values. Iterate through the values and return them.
496 auto item_enumerator = (storeValues)->GetEnumerator();
497 while (item_enumerator->MoveNext())
498 {
499 auto&& item = item_enumerator->get_Current();
500 CODEPORTING_DEBUG_ASSERT1(System::ObjectExt::Is<String>(item));
501 // T result = System::ExplicitCast<T>(item);
502 String result = ObjectExt::Unbox<String>(item);
503 // yield return item as T;
504 list->Add(result);
505 }
506 }
507 break;
508 }
509
510 return list->GetEnumerator();
511 }
512
513 virtual String ToString() const override
514 {
515 return _store->GetHeaderString(_headerName);
516 }
517
519 {
521 {
522 return ToString();
523 }
524 return _store->GetHeaderString(_headerName, _specialValue);
525 }
526
528 {
529
530 if (!_store->ContainsParsedValue(_headerName, _specialValue))
531 {
532 _store->AddParsedValue(_headerName, _specialValue);
533 }
534 }
535
537 {
538
539 // We're not interested in the return value. It's OK if the "special value" wasn't in the store
540 // before calling RemoveParsedValue().
541 _store->RemoveParsedValue(_headerName, _specialValue);
542 }
543
544 void SetTemplateWeakPtr(uint32_t argument) override
545 {
546 switch (argument)
547 {
548 case 0:
549 System::Details::CollectionHelpers::SetWeakPointer(_specialValue);
550 System::Details::CollectionHelpers::SetWeakPointer(0, _validator);
551 break;
552 }
553 }
554
555private:
556 String _headerName;
558 System::SharedPtr<Object> _specialValue;
560
561 void CheckValue(String item) const
562 {
563 if (item == nullptr)
564 {
565 throw ArgumentNullException(u"item");
566 }
567
568 // If this instance has a custom validator for validating arguments, call it now.
569 if (_validator != nullptr)
570 {
571 _validator(System::MakeSharedPtr(this), item);
572 }
573 }
574
575 int32_t GetCount() const
576 {
577 // This is an O(n) operation.
578
579 System::SharedPtr<Object> storeValue = _store->GetParsedValues(_headerName);
580
581 if (storeValue == nullptr)
582 {
583 return 0;
584 }
585
588
589 if (storeValues == nullptr)
590 {
591 return 1;
592 }
593 else
594 {
595 return storeValues->get_Count();
596 }
597 }
598};
599
600}}}} // namespace System::Net::Http::Headers
Interface of collection of elements. Objects of this class should only be allocated using System::Mak...
Definition: icollection.h:20
String GetHeaderStringWithoutSpecial()
Definition: http_header_value_collection.h:518
void RemoveSpecialValue()
Definition: http_header_value_collection.h:536
int32_t get_Count() const override
Gets number of elements in collection.
Definition: http_header_value_collection.h:349
void ParseAdd(String input)
Definition: http_header_value_collection.h:404
void Add(const String &item) override
Adds element into collection.
Definition: http_header_value_collection.h:398
virtual String ToString() const override
Analog of C# Object.ToString() method. Enables converting custom objects to string.
Definition: http_header_value_collection.h:513
void Clear() override
Deletes all elements from collection.
Definition: http_header_value_collection.h:414
bool Contains(const String &item) const override
Checks if element is present in collection.
Definition: http_header_value_collection.h:419
HttpHeaderValueCollection(String headerName, System::SharedPtr< HttpHeaders > store)
Definition: http_header_value_collection.h:369
bool get_IsSpecialValueSet()
Definition: http_header_value_collection.h:359
bool get_IsReadOnly()
Definition: http_header_value_collection.h:354
bool Remove(const String &item) override
Deletes element from collection.
Definition: http_header_value_collection.h:463
void CopyTo(System::ArrayPtr< String > array, int32_t arrayIndex) override
Definition: http_header_value_collection.h:425
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: http_header_value_collection.h:544
void SetSpecialValue()
Definition: http_header_value_collection.h:527
HttpHeaderValueCollection(String headerName, System::SharedPtr< HttpHeaders > store, String specialValue)
Definition: http_header_value_collection.h:382
System::SharedPtr< Collections::Generic::IEnumerator< String > > GetEnumerator() override
Gets enumerator.
Definition: http_header_value_collection.h:469
bool TryParseAdd(String input)
Definition: http_header_value_collection.h:409
HttpHeaderValueCollection(String headerName, System::SharedPtr< HttpHeaders > store, Action< System::SharedPtr< HttpHeaderValueCollection< String > >, String > validator)
Definition: http_header_value_collection.h:375
HttpHeaderValueCollection(String headerName, System::SharedPtr< HttpHeaders > store, String specialValue, Action< System::SharedPtr< HttpHeaderValueCollection< String > >, String > validator)
Definition: http_header_value_collection.h:388
Represents the collection of the headers values. Objects of this class should only be allocated using...
Definition: http_header_value_collection.h:24
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: http_header_value_collection.h:270
HttpHeaderValueCollection(String headerName, System::SharedPtr< HttpHeaders > store)
Constructs a new instance.
Definition: http_header_value_collection.h:67
String GetHeaderStringWithoutSpecial()
Returns a string representation of the current collection without a "special value".
Definition: http_header_value_collection.h:241
void CopyTo(System::ArrayPtr< T > array, int32_t arrayIndex) override
Copies all collection elements to existing array elements.
Definition: http_header_value_collection.h:142
void SetSpecialValue()
Sets a "special value".
Definition: http_header_value_collection.h:251
int32_t get_Count() const override
Gets number of elements in collection.
Definition: http_header_value_collection.h:40
void ParseAdd(String input)
Parses a header string representation and adds it to the current collection.
Definition: http_header_value_collection.h:115
HttpHeaderValueCollection(String headerName, System::SharedPtr< HttpHeaders > store, T specialValue, Action< System::SharedPtr< HttpHeaderValueCollection< T > >, T > validator)
Constructs a new instance.
Definition: http_header_value_collection.h:96
HttpHeaderValueCollection(String headerName, System::SharedPtr< HttpHeaders > store, Action< System::SharedPtr< HttpHeaderValueCollection< T > >, T > validator)
Constructs a new instance.
Definition: http_header_value_collection.h:76
bool Remove(const T &item) override
Deletes element from collection.
Definition: http_header_value_collection.h:181
void Clear() override
Deletes all elements from collection.
Definition: http_header_value_collection.h:129
System::SharedPtr< Collections::Generic::IEnumerator< T > > GetEnumerator() override
Gets enumerator.
Definition: http_header_value_collection.h:188
bool Contains(const T &item) const override
Checks if element is present in collection.
Definition: http_header_value_collection.h:135
String ToString() const override
Analog of C# Object.ToString() method. Enables converting custom objects to string.
Definition: http_header_value_collection.h:234
bool get_IsReadOnly()
Gets a value that indicates if the current collection is read-only.
Definition: http_header_value_collection.h:47
HttpHeaderValueCollection(String headerName, System::SharedPtr< HttpHeaders > store, T specialValue)
Constructs a new instance.
Definition: http_header_value_collection.h:86
void RemoveSpecialValue()
Removes a "special value".
Definition: http_header_value_collection.h:261
void Add(const T &item) override
Adds element into collection.
Definition: http_header_value_collection.h:107
bool get_IsSpecialValueSet()
Gets a value that indicates if the current collection contains a "special value".
Definition: http_header_value_collection.h:54
bool TryParseAdd(String input)
Tries to parse a header string representation and add it to the current collection.
Definition: http_header_value_collection.h:123
static System::SharedPtr< System::Collections::Generic::List< System::SharedPtr< System::Object > > > ParsedValuesAsList(const System::SharedPtr< Object > parsedValues)
Converts parsed values to list.
static std::enable_if< std::is_enum< T >::value, System::SmartPtr< System::Object > >::type Box(const T &value)
Boxes value types for converting to Object. Implementation for enum types.
Definition: object_ext.h:198
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
Subclass of System::SmartPtr which sets itself to weak mode at construction. Please note that this cl...
Definition: weak_ptr.h:18
Definition: db_command.h:9
std::enable_if_t< System::IsSmartPtr< From >::value, System::SharedPtr< System::Array< To > > > StaticCastArray(const System::SharedPtr< System::Array< From > > &from)
Performs casting of elements of the specified array to different type. Override for cases then From i...
Definition: special_casts.h:67
MulticastDelegate< void(Args...)> Action
Delegate type that references methods that have no return value.
Definition: action.h:40
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1650