00001 #ifndef TAG_INC_ARRAY_H
00002 #define TAG_INC_ARRAY_H
00003 #include <tag/tuple.h>
00004
00005 namespace tag
00006 {
00007
00008 template<class C=int, int I=-1> class array;
00009
00010 template<> struct array<int, -1>
00011 {
00012 struct Underfill{};
00013 };
00014
00015
00016 template<class C, int I> class array
00017 {
00018 public:
00019
00020 typedef C* iterator;
00021 typedef const C* const_iterator;
00022
00023 iterator begin(){return data;}
00024 const_iterator begin()const {return data;}
00025
00026 iterator end(){return data+I;}
00027 const_iterator end()const {return data+I;}
00028
00029 operator C*() { return data; }
00030 operator const C*() const { return data; }
00031
00032 C& operator*(){return *data;}
00033 const C& operator*() const {return *data;}
00034
00035 C& operator[](int i){return data[i];}
00036 const C& operator[](int i)const {return data[i];}
00037
00038 C* operator+(int i){return data+i;}
00039 const C* operator+(int i)const{return data+i;}
00040
00041
00042 int size()const
00043 {
00044 return I;
00045 }
00046
00047 array(){}
00048
00049 template<class D, class E> array(const T_list<D, E>& l)
00050 {
00051
00052
00053
00054 typedef typename sizetoolong<array<>, (T_list<D,E>::elements > I) >::dummy foo;
00055 typedef typename sizetooshort<array<>, (T_list<D,E>::elements < I) >::dummy bar;
00056 array_filler<D, E, T_list<D,E>::elements -1>::fill(data, l);
00057 }
00058
00059 template<class D, class E> array(const T_list<typename array<int,-1>::Underfill, T_list<D,E> >& l)
00060 {
00061 typedef typename sizetoolong<array<>, (T_list<D,E>::elements > I) >::dummy foo;
00062 array_filler<D, E, T_list<D,E>::elements -1>::fill(data, l.next);
00063 }
00064
00065 private:
00066 C data[I];
00067
00068
00069
00070 template<class D, class E, int i> struct array_filler
00071 {
00072 static void fill(C* data, const T_list<D, E>& l)
00073 {
00074 data[i] = l.val;
00075 array_filler<typename E::val_type, typename E::next_type, i-1>::fill(data, l.next);
00076 }
00077 };
00078
00079 template<class D, class E> struct array_filler<D,E,-1>
00080 {
00081 static void fill(C* data, const T_list<D, E>& l){}
00082 };
00083
00084 template<class D, int i> struct sizetoolong
00085 {
00086 typedef typename D::Error_initializer_list_is_too_long dummy;
00087 };
00088
00089 template<class D> struct sizetoolong<D, 0>
00090 {
00091 typedef int dummy;
00092 };
00093
00094 template<class D, int i> struct sizetooshort
00095 {
00096 typedef typename D::Error_initializer_list_is_too_short dummy;
00097 };
00098
00099 template<class D> struct sizetooshort<D, 0>
00100 {
00101 typedef int dummy;
00102 };
00103 };
00104 }
00105 #endif