All Classes Functions Variables Typedefs Friends Pages
IArray.h
1 //------------------------------------------------------------------------------
2 //
3 // Copyright (c) 2012 Glympse Inc. All rights reserved.
4 //
5 //------------------------------------------------------------------------------
6 
7 #ifndef IARRAY_H__GLYMPSE__
8 #define IARRAY_H__GLYMPSE__
9 
10 namespace Glympse
11 {
12 
13 /*C*/
14 template< class T > struct IArray;
16 template< class T > class GArray
17 {
18  private: GArray();
19  public: typedef O< IArray< T > > ptr;
20 };
21 
22 
26 /*C*/template< class T > struct IArray : public ICommon
27 /*J*public interface GArray<T> extends GCommon, Iterable<T>**/
28 /*S*public interface GArray<T> : GCommon, IReadOnlyList<T>**/
29 {
33  public: virtual int32 length() = 0;
34 
38  public: virtual T at(int32 index) = 0;
39 
43  public: virtual typename GEnumeration<T>::ptr elements() = 0;
44 
48  public: virtual typename GArray<T>::ptr clone() = 0;
49 
53  /*C*/public: typedef T Element;
54 };
55 
61 /*C*/
62 
64 template< typename T > struct RangeIterator
65 {
66  private: typename GEnumeration<T>::ptr _elements;
67  private: int32 _position;
68  private: T _current;
69 
70  public: RangeIterator(const typename GEnumeration<T>::ptr& elements, int32 position)
71  : _elements(elements)
72  , _position(position)
73  , _current()
74  {
75  if ( _elements->hasMoreElements() )
76  {
77  _current = _elements->nextElement();
78  }
79  }
80 
81  public: const RangeIterator<T>& operator++()
82  {
83  ++_position;
84  if ( _elements->hasMoreElements() )
85  {
86  _current = _elements->nextElement();
87  }
88  return *this;
89  }
90 
91  public: T operator*() const
92  {
93  return _current;
94  }
95 
96  public: bool operator!=(const RangeIterator& other) const
97  {
98  return _position != other._position;
99  }
100 };
101 
102 // Provides standalone begin() function for ADL.
103 template< typename T > RangeIterator< typename T::Element > begin(const O<T>& container)
104 {
105  return RangeIterator< typename T::Element >(container->elements(), 0);
106 }
107 
108 // Provides standalone end() function for ADL.
109 template< typename T > RangeIterator< typename T::Element > end(const O<T>& container)
110 {
111  return RangeIterator< typename T::Element >(container->elements(), container->length());
112 }
113 
114 
115 }
116 
117 #endif // !IARRAY_H__GLYMPSE__