Collection.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using namespace System::Collections;
  2. #ifndef __COLLECTION
  3. #define __COLLECTION
  4. namespace MuPDF {
  5. #pragma once
  6. generic<class T>
  7. private ref class EmptyCollection {
  8. public:
  9. static initonly array<T>^ Instance = gcnew array<T>(0);
  10. static Generic::IEnumerator<T>^ GetEnumerator() {
  11. return ((Generic::IEnumerable<T>^)Instance)->GetEnumerator();
  12. }
  13. };
  14. generic<class T>
  15. public interface class IIndexableCollection {
  16. property int Count {
  17. int get();
  18. }
  19. property T default[int] {
  20. T get(int index);
  21. }
  22. };
  23. generic<class TCollection, typename TItem>
  24. where TCollection : IIndexableCollection<TItem>
  25. ref class IndexableEnumerator : System::Collections::Generic::IEnumerator<TItem> {
  26. public:
  27. IndexableEnumerator(TCollection collection) : _collection(collection), _count(collection->Count), _index(-1) {}
  28. ~IndexableEnumerator() {}
  29. property TItem Current {
  30. virtual TItem get() {
  31. return _current;
  32. }
  33. };
  34. property Object^ CurrentBase {
  35. virtual Object^ get() sealed = System::Collections::IEnumerator::Current::get{
  36. return Current;
  37. }
  38. };
  39. virtual bool MoveNext() {
  40. if (++_index < _count) {
  41. _current = _collection->default[_index];
  42. return true;
  43. }
  44. return false;
  45. }
  46. virtual void Reset() {
  47. _index = -1;
  48. }
  49. private:
  50. TCollection _collection;
  51. TItem _current;
  52. int _index;
  53. const int _count;
  54. };
  55. template<class TManaged, typename TUnmanaged>
  56. private value struct Enumerator : Generic::IEnumerator<TManaged^> {
  57. public:
  58. Enumerator(TUnmanaged* first, TUnmanaged* last) : _first(first), _last(last) {}
  59. property TManaged^ Current {
  60. virtual TManaged^ get() {
  61. return gcnew TManaged(_current);
  62. }
  63. };
  64. property Object^ CurrentBase {
  65. virtual Object^ get() sealed = System::Collections::IEnumerator::Current::get{
  66. return Current;
  67. }
  68. };
  69. virtual bool MoveNext() {
  70. if (!_current) {
  71. _current = _first;
  72. return _first != NULL;
  73. }
  74. if (_current->next && _current != _last) {
  75. _current = _current->next;
  76. return true;
  77. }
  78. return false;
  79. }
  80. virtual void Reset() {
  81. _current = NULL;
  82. }
  83. private:
  84. TUnmanaged* _first;
  85. TUnmanaged* _last;
  86. TUnmanaged* _current;
  87. };
  88. };
  89. #endif // !__COLLECTION