00001
00002
00003 #ifndef _CARRAY_H
00004 #define _CARRAY_H
00005 #include <cstddef>
00006 #include <cassert>
00007 #include <algorithm>
00008 #include <iterator>
00009 #include <boost/type_traits.hpp>
00010 namespace osl
00011 {
00012 namespace misc
00013 {
00020 template <typename T>
00021 struct CArrayIterator
00022 {
00023 typedef std::random_access_iterator_tag iterator_category;
00024 typedef T value_type;
00025 typedef int difference_type;
00026 typedef T* pointer;
00027 typedef T& reference;
00028
00029 T *ptr;
00030 CArrayIterator(T *p) : ptr(p) {}
00031 T& operator*() const { return *ptr; }
00032 T* operator->() const { return ptr; }
00033 CArrayIterator& operator+=(int diff)
00034 {
00035 ptr += diff;
00036 return *this;
00037 }
00038 CArrayIterator& operator-=(int diff) { return operator+=(-diff); }
00039 CArrayIterator& operator++() { return operator+=(1); }
00040 CArrayIterator operator++(int)
00041 {
00042 const CArrayIterator result = *this;
00043 operator++();
00044 return result;
00045 }
00046 CArrayIterator& operator--() { return operator+=(-1); }
00047 CArrayIterator operator--(int)
00048 {
00049 const CArrayIterator result = *this;
00050 operator--();
00051 return result;
00052 }
00053 operator CArrayIterator<const T>() const
00054 {
00055 return CArrayIterator<const T>(ptr);
00056 }
00057 private:
00058 operator bool();
00059 };
00060 template <class T> inline
00061 const CArrayIterator<T> operator+(const CArrayIterator<T>& iter, int diff)
00062 {
00063 CArrayIterator<T> result(iter);
00064 result += diff;
00065 return result;
00066 }
00067 template <class T> inline
00068 const CArrayIterator<T> operator-(const CArrayIterator<T>& iter, int diff) {
00069 return iter + (-diff);
00070 }
00071
00072 template <class T, class T2>
00073 inline int operator-(CArrayIterator<T> l, CArrayIterator<T2> r)
00074 {
00075 return l.ptr - r.ptr;
00076 };
00077 template <class T, class T2>
00078 inline bool operator==(CArrayIterator<T> l, CArrayIterator<T2> r)
00079 {
00080 return l.ptr == r.ptr;
00081 };
00082 template <class T, class T2>
00083 inline bool operator!=(CArrayIterator<T> l, CArrayIterator<T2> r)
00084 {
00085 return l.ptr != r.ptr;
00086 };
00087 template <class T, class T2>
00088 inline bool operator<(CArrayIterator<T> l, CArrayIterator<T2> r)
00089 {
00090 return l.ptr < r.ptr;
00091 };
00092 template <class T, class T2>
00093 inline bool operator>(CArrayIterator<T> l, CArrayIterator<T2> r)
00094 {
00095 return l.ptr > r.ptr;
00096 };
00097
00101 template <typename T, size_t Capacity>
00102 class CArray
00103 {
00104 public:
00106 T elements[Capacity];
00107 typedef typename boost::remove_cv<T>::type T_simple;
00108 public:
00109 typedef CArrayIterator<T> iterator;
00110 iterator begin() { return &elements[0]; }
00111 iterator end() { return &elements[Capacity]; }
00112
00113 void fill(T_simple value=T_simple())
00114 {
00115 std::fill(begin(), end(), value);
00116 }
00117 T& operator[] (size_t i)
00118 {
00119 assert(i < Capacity);
00120 return elements[i];
00121 }
00122
00123 static size_t size() { return Capacity; }
00124
00125 T const& operator[] (size_t i) const
00126 {
00127 assert(i < Capacity);
00128 return elements[i];
00129 }
00130
00131 typedef CArrayIterator<const T> const_iterator;
00132 const_iterator begin() const { return &elements[0]; }
00133 const_iterator end() const { return &elements[Capacity]; }
00134
00135 bool operator==(const CArray& other) const
00136 {
00137 return std::equal(begin(), end(), other.begin());
00138 }
00139 };
00140 }
00141 using misc::CArray;
00142 }
00143
00144
00145 #endif
00146
00147
00148
00149