CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
collection_base.h
1#pragma once
2
3#include <system/scope_guard.h>
4#include <system/exceptions.h>
5#include <system/details/pointer_collection_helpers.h>
6#include <system/collections/list.h>
7#include <system/collections/ienumerator.h>
8#include <system/collections/ienumerable.h>
9#include <system/array.h>
10#include <cstdint>
11
12
13namespace System {
14
15namespace Collections {
16
19template<typename T>
20class CollectionBase : public virtual Generic::IEnumerable<T>
21{
24
25 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
26 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
27
28public:
29
32
34 class ASPOSECPP_SHARED_CLASS ListImpl : public System::Object
35 {
36 typedef ListImpl ThisType;
38
39 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
40 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
41
42 public:
43
46
47 private:
49 template<typename FT0> friend class CollectionBase;
51
52 public:
53
65 ASPOSECPP_SHARED_API void CopyTo(const ArrayPtr<T>& array, int32_t index)
66 {
67 _base->get_InnerList()->CopyTo(array, index);
68 }
69
76 ASPOSECPP_SHARED_API T idx_get(int32_t index)
77 {
78 if (index < 0 || index >= _base->get_Count())
79 {
80 throw ArgumentOutOfRangeException(u"index");
81 }
82
83 return _base->get_InnerList()->idx_get(index);
84 }
85
92 ASPOSECPP_SHARED_API void idx_set(int32_t index, T value)
93 {
94 if (index < 0 || index >= _base->get_Count())
95 {
96 throw ArgumentOutOfRangeException(u"index");
97 }
98
99 _base->OnValidate(value);
100 T temp = _base->get_InnerList()->idx_get(index);
101 _base->OnSet(index, temp, value);
102 _base->get_InnerList()->idx_set(index, value);
103 try
104 {
105 _base->OnSetComplete(index, temp, value);
106 }
107 catch (...)
108 {
109 _base->get_InnerList()->idx_set(index, temp);
110 throw;
111 }
112
113 }
114
118 ASPOSECPP_SHARED_API bool Contains(const T& value)
119 {
120 return _base->get_InnerList()->Contains(value);
121 }
122
129 ASPOSECPP_SHARED_API int32_t Add(const T& value)
130 {
131 _base->OnValidate(value);
132 _base->OnInsert(_base->get_InnerList()->get_Count(), value);
133 _base->get_InnerList()->Add(value);
134 int32_t index = _base->get_InnerList()->get_Count() - 1;
135 try
136 {
137 _base->OnInsertComplete(index, value);
138 }
139 catch (...)
140 {
141 _base->get_InnerList()->RemoveAt(index);
142 throw;
143 }
144
145 return index;
146 }
147
154 ASPOSECPP_SHARED_API void Remove(const T& value)
155 {
156 _base->OnValidate(value);
157 int32_t index = _base->get_InnerList()->IndexOf(value);
158 if (index < 0)
159 {
160 throw ArgumentException();
161 }
162 _base->OnRemove(index, value);
163 _base->get_InnerList()->RemoveAt(index);
164 try
165 {
166 _base->OnRemoveComplete(index, value);
167 }
168 catch (...)
169 {
170 _base->get_InnerList()->Insert(index, value);
171 throw;
172 }
173
174 }
175
181 ASPOSECPP_SHARED_API int32_t IndexOf(const T& value)
182 {
183 return _base->get_InnerList()->IndexOf(value);
184 }
185
195 ASPOSECPP_SHARED_API void Insert(int32_t index, const T& value)
196 {
197 if (index < 0 || index > _base->get_Count())
198 {
199 throw ArgumentOutOfRangeException(u"index");
200 }
201
202 _base->OnValidate(value);
203 _base->OnInsert(index, value);
204 _base->get_InnerList()->Insert(index, value);
205 try
206 {
207 _base->OnInsertComplete(index, value);
208 }
209 catch (...)
210 {
211 _base->get_InnerList()->RemoveAt(index);
212 throw;
213 }
214
215 }
216
217
218 protected:
219
221 virtual ASPOSECPP_SHARED_API ~ListImpl()
222 {
223 }
224
225 ListImpl(const SharedPtr<CollectionBase<T>>& base_)
226 : _base(base_.get())
227 {
228 }
229
230 MEMBER_FUNCTION_MAKE_OBJECT(ListImpl, CODEPORTING_ARGS(const SharedPtr<CollectionBase<T>>& base_), CODEPORTING_ARGS(base_));
231
232 #ifdef ASPOSE_GET_SHARED_MEMBERS
233 void GetSharedMembers(System::Object::shared_members_type& result) const override
234 {
235 System::Object::GetSharedMembers(result);
236
237 result.Add("System::Collections::CollectionBase::ListImpl::_base", this->_base);
238 }
239 #endif
241
242 #ifdef __DBG_FOR_EACH_MEMBER
243 public:
246 ASPOSECPP_SHARED_API void DBG_for_each_member(DBG::for_each_member_visitor& visitor) const override
247 {
248 System::Object::DBG_for_each_member(visitor);
249
250 visitor.add_self(this);
251
252 visitor.add_member(this, _base, "_base");
253 }
256 const char* DBG_class_name() const override { return "ListImpl"; }
257 #endif
258
259 private:
260
261 CollectionBase<T>* const _base;
262
263 };
264
265
266public:
267
270 int32_t get_Capacity()
271 {
272 return get_InnerList()->get_Capacity();
273 }
274
279 void set_Capacity(int32_t value)
280 {
281 get_InnerList()->set_Capacity(value);
282 }
283
288 int32_t get_Count()
289 {
290 return _list->get_Count();
291 }
292
294 void Clear()
295 {
296 OnClear();
297 get_InnerList()->Clear();
299 }
300
306 void RemoveAt(int32_t index)
307 {
308 if (index < 0 || index >= get_Count())
309 {
310 throw ArgumentOutOfRangeException(u"index");
311 }
312
313 T temp = get_InnerList()->idx_get(index);
314 OnValidate(temp);
315 OnRemove(index, temp);
316 get_InnerList()->RemoveAt(index);
317 try
318 {
319 OnRemoveComplete(index, temp);
320 }
321 catch (...)
322 {
323 get_InnerList()->Insert(index, temp);
324 throw;
325 }
326
327 }
328
332 {
333 return get_InnerList()->GetEnumerator();
334 }
335
337 void SetTemplateWeakPtr(uint32_t argument) override
338 {
339 switch (argument)
340 {
341 case 0:
342 System::Details::CollectionHelpers::SetWeakPointer(0, _list);
343 System::Details::CollectionHelpers::SetWeakPointer(0, _impl);
344 break;
345
346 }
347 }
348
349protected:
350
355 {
356 return _list;
357 }
358
362 {
363 return _impl;
364 }
365
368 {
369 System::Details::ThisProtector __local_self_ref(this);
370
371 _list = System::MakeObject<System::Collections::Generic::List<T>>();
372 _impl = ListImpl::MakeObject(MakeSharedPtr(this));
373 }
374
377 CollectionBase(int32_t capacity)
378 {
379 System::Details::ThisProtector __local_self_ref(this);
380
381 _list = System::MakeObject<System::Collections::Generic::List<T>>(capacity);
382 _impl = ListImpl::MakeObject(MakeSharedPtr(this));
383 }
384
389 virtual void OnSet(int32_t index, const T& oldValue, const T& newValue)
390 {
391 ASPOSE_UNUSED(index);
392 ASPOSE_UNUSED(oldValue);
393 ASPOSE_UNUSED(newValue);
394 }
395
399 virtual void OnInsert(int32_t index, const T& value)
400 {
401 ASPOSE_UNUSED(index);
402 ASPOSE_UNUSED(value);
403 }
404
406 virtual void OnClear()
407 {
408 }
409
413 virtual void OnRemove(int32_t index, const T& value)
414 {
415 ASPOSE_UNUSED(index);
416 ASPOSE_UNUSED(value);
417 }
418
422 virtual void OnValidate(const T& value)
423 {
424 if (value == nullptr)
425 {
426 throw ArgumentNullException(u"value");
427 }
428 }
429
434 virtual void OnSetComplete(int32_t index, const T& oldValue, const T& newValue)
435 {
436 ASPOSE_UNUSED(index);
437 ASPOSE_UNUSED(oldValue);
438 ASPOSE_UNUSED(newValue);
439 }
440
444 virtual void OnInsertComplete(int32_t index, const T& value)
445 {
446 ASPOSE_UNUSED(index);
447 ASPOSE_UNUSED(value);
448 }
449
451 virtual void OnClearComplete()
452 {
453 }
454
458 virtual void OnRemoveComplete(int32_t index, const T& value)
459 {
460 ASPOSE_UNUSED(index);
461 ASPOSE_UNUSED(value);
462 }
463
465 {
466 }
467
469 #ifdef ASPOSE_GET_SHARED_MEMBERS
470 void GetSharedMembers(System::Object::shared_members_type& result) const override
471 {
472 System::Object::GetSharedMembers(result);
473
474 result.Add("System::Collections::CollectionBase::_list", this->_list);
475 result.Add("System::Collections::CollectionBase::_impl", this->_impl);
476 }
477 #endif
478
479 #ifdef __DBG_FOR_EACH_MEMBER
480public:
483 ASPOSECPP_SHARED_API void DBG_for_each_member(DBG::for_each_member_visitor& visitor) const override
484 {
485 System::Object::DBG_for_each_member(visitor);
486
487 visitor.add_self(this);
488
489 visitor.add_member(this, _list, "_list");
490 visitor.add_member(this, _impl, "_impl");
491 }
494 const char* DBG_class_name() const override { return "CollectionBase"; }
495 #endif
497private:
498
499 SharedPtr<System::Collections::Generic::List<T>> _list;
500 SharedPtr<ListImpl> _impl;
501
502};
503
504} // namespace Collections
505} // namespace System
506
507
Provides an interface for accessing the elements in the collection.
Definition: collection_base.h:35
void CopyTo(const ArrayPtr< T > &array, int32_t index)
Copies the entire collection to a compatible one-dimensional Array, starting at the specified index o...
Definition: collection_base.h:65
T idx_get(int32_t index)
Returns the element at the specified index.
Definition: collection_base.h:76
void Remove(const T &value)
Removes the first occurrence of a specific element from the collection.
Definition: collection_base.h:154
void idx_set(int32_t index, T value)
Sets the element at the specified index.
Definition: collection_base.h:92
int32_t Add(const T &value)
Adds a element to the end of the collection.
Definition: collection_base.h:129
bool Contains(const T &value)
Determines whether the collection contains a specific element.
Definition: collection_base.h:118
void Insert(int32_t index, const T &value)
Inserts an element into the collection at the specified index.
Definition: collection_base.h:195
int32_t IndexOf(const T &value)
Searches for the specified element and returns the zero-based index of the first occurrence within th...
Definition: collection_base.h:181
Provides an abstract base class for a strongly typed collection.
Definition: collection_base.h:21
int32_t get_Capacity()
Returns the number of elements that the collection can contain.
Definition: collection_base.h:270
virtual void OnClearComplete()
Performs additional custom processes after clearing the contents of the collection instance.
Definition: collection_base.h:451
CollectionBase(int32_t capacity)
Initializes a new instance of the Collections::CollectionBase class with the specified capacity.
Definition: collection_base.h:377
virtual ~CollectionBase()
Definition: collection_base.h:464
int32_t get_Count()
Returns the number of elements contained in the collection instance. This method cannot be overridden...
Definition: collection_base.h:288
virtual void OnValidate(const T &value)
Performs additional custom processes when validating a value.
Definition: collection_base.h:422
SharedPtr< Generic::IEnumerator< T > > GetEnumerator() override
Returns an enumerator that iterates through the collection instance.
Definition: collection_base.h:331
virtual void OnInsert(int32_t index, const T &value)
Performs additional custom processes before inserting a new element into the collection instance.
Definition: collection_base.h:399
void RemoveAt(int32_t index)
Removes the element at the specified index of the collection instance. This method is not overridable...
Definition: collection_base.h:306
virtual void OnSetComplete(int32_t index, const T &oldValue, const T &newValue)
Performs additional custom processes after setting a value in the collection instance.
Definition: collection_base.h:434
void set_Capacity(int32_t value)
Sets the number of elements that the collection can contain.
Definition: collection_base.h:279
virtual void OnSet(int32_t index, const T &oldValue, const T &newValue)
Performs additional custom processes before setting a value in the collection instance.
Definition: collection_base.h:389
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: collection_base.h:337
SharedPtr< System::Collections::Generic::List< T > > get_InnerList()
Returns a List containing the list of elements in the collection instance.
Definition: collection_base.h:354
virtual void OnRemove(int32_t index, const T &value)
Performs additional custom processes when removing an element from the collection instance.
Definition: collection_base.h:413
virtual void OnRemoveComplete(int32_t index, const T &value)
Performs additional custom processes after removing an element from the collection instance.
Definition: collection_base.h:458
SharedPtr< ListImpl > get_List()
Returns a ListImpl containing the list of elements in the collection instance.
Definition: collection_base.h:361
virtual void OnClear()
Performs additional custom processes when clearing the contents of the collection instance.
Definition: collection_base.h:406
void Clear()
Removes all objects from the collection instance. This method cannot be overridden.
Definition: collection_base.h:294
virtual void OnInsertComplete(int32_t index, const T &value)
Performs additional custom processes after inserting a new element into the collection instance.
Definition: collection_base.h:444
CollectionBase()
Initializes a new instance of the Collections::CollectionBase class with the default initial capacity...
Definition: collection_base.h:367
Interface of object providing enumerator on contained elements.
Definition: ienumerable.h:25
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Definition: db_command.h:9
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1650