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 private:
243
244 CollectionBase<T>* const _base;
245
246 };
247
248
249public:
250
253 int32_t get_Capacity()
254 {
255 return get_InnerList()->get_Capacity();
256 }
257
262 void set_Capacity(int32_t value)
263 {
264 get_InnerList()->set_Capacity(value);
265 }
266
271 int32_t get_Count()
272 {
273 return _list->get_Count();
274 }
275
277 void Clear()
278 {
279 OnClear();
280 get_InnerList()->Clear();
282 }
283
289 void RemoveAt(int32_t index)
290 {
291 if (index < 0 || index >= get_Count())
292 {
293 throw ArgumentOutOfRangeException(u"index");
294 }
295
296 T temp = get_InnerList()->idx_get(index);
297 OnValidate(temp);
298 OnRemove(index, temp);
299 get_InnerList()->RemoveAt(index);
300 try
301 {
302 OnRemoveComplete(index, temp);
303 }
304 catch (...)
305 {
306 get_InnerList()->Insert(index, temp);
307 throw;
308 }
309
310 }
311
315 {
316 return get_InnerList()->GetEnumerator();
317 }
318
320 void SetTemplateWeakPtr(uint32_t argument) override
321 {
322 switch (argument)
323 {
324 case 0:
325 System::Details::CollectionHelpers::SetWeakPointer(0, _list);
326 System::Details::CollectionHelpers::SetWeakPointer(0, _impl);
327 break;
328
329 }
330 }
331
332protected:
333
338 {
339 return _list;
340 }
341
345 {
346 return _impl;
347 }
348
351 {
352 System::Details::ThisProtector __local_self_ref(this);
353
354 _list = System::MakeObject<System::Collections::Generic::List<T>>();
355 _impl = ListImpl::MakeObject(MakeSharedPtr(this));
356 }
357
360 CollectionBase(int32_t capacity)
361 {
362 System::Details::ThisProtector __local_self_ref(this);
363
364 _list = System::MakeObject<System::Collections::Generic::List<T>>(capacity);
365 _impl = ListImpl::MakeObject(MakeSharedPtr(this));
366 }
367
372 virtual void OnSet(int32_t index, const T& oldValue, const T& newValue)
373 {
374 ASPOSE_UNUSED(index);
375 ASPOSE_UNUSED(oldValue);
376 ASPOSE_UNUSED(newValue);
377 }
378
382 virtual void OnInsert(int32_t index, const T& value)
383 {
384 ASPOSE_UNUSED(index);
385 ASPOSE_UNUSED(value);
386 }
387
389 virtual void OnClear()
390 {
391 }
392
396 virtual void OnRemove(int32_t index, const T& value)
397 {
398 ASPOSE_UNUSED(index);
399 ASPOSE_UNUSED(value);
400 }
401
405 virtual void OnValidate(const T& value)
406 {
407 if (value == nullptr)
408 {
409 throw ArgumentNullException(u"value");
410 }
411 }
412
417 virtual void OnSetComplete(int32_t index, const T& oldValue, const T& newValue)
418 {
419 ASPOSE_UNUSED(index);
420 ASPOSE_UNUSED(oldValue);
421 ASPOSE_UNUSED(newValue);
422 }
423
427 virtual void OnInsertComplete(int32_t index, const T& value)
428 {
429 ASPOSE_UNUSED(index);
430 ASPOSE_UNUSED(value);
431 }
432
434 virtual void OnClearComplete()
435 {
436 }
437
441 virtual void OnRemoveComplete(int32_t index, const T& value)
442 {
443 ASPOSE_UNUSED(index);
444 ASPOSE_UNUSED(value);
445 }
446
448 {
449 }
450
452 #ifdef ASPOSE_GET_SHARED_MEMBERS
453 void GetSharedMembers(System::Object::shared_members_type& result) const override
454 {
455 System::Object::GetSharedMembers(result);
456
457 result.Add("System::Collections::CollectionBase::_list", this->_list);
458 result.Add("System::Collections::CollectionBase::_impl", this->_impl);
459 }
460 #endif
462
463private:
464
467
468};
469
470} // namespace Collections
471} // namespace System
472
473
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:253
virtual void OnClearComplete()
Performs additional custom processes after clearing the contents of the collection instance.
Definition: collection_base.h:434
CollectionBase(int32_t capacity)
Initializes a new instance of the Collections::CollectionBase class with the specified capacity.
Definition: collection_base.h:360
virtual ~CollectionBase()
Definition: collection_base.h:447
int32_t get_Count()
Returns the number of elements contained in the collection instance. This method cannot be overridden...
Definition: collection_base.h:271
virtual void OnValidate(const T &value)
Performs additional custom processes when validating a value.
Definition: collection_base.h:405
SharedPtr< Generic::IEnumerator< T > > GetEnumerator() override
Returns an enumerator that iterates through the collection instance.
Definition: collection_base.h:314
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:382
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:289
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:417
void set_Capacity(int32_t value)
Sets the number of elements that the collection can contain.
Definition: collection_base.h:262
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:372
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:320
SharedPtr< System::Collections::Generic::List< T > > get_InnerList()
Returns a List containing the list of elements in the collection instance.
Definition: collection_base.h:337
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:396
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:441
SharedPtr< ListImpl > get_List()
Returns a ListImpl containing the list of elements in the collection instance.
Definition: collection_base.h:344
virtual void OnClear()
Performs additional custom processes when clearing the contents of the collection instance.
Definition: collection_base.h:389
void Clear()
Removes all objects from the collection instance. This method cannot be overridden.
Definition: collection_base.h:277
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:427
CollectionBase()
Initializes a new instance of the Collections::CollectionBase class with the default initial capacity...
Definition: collection_base.h:350
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