00001 #ifndef ELEMENT_H 00002 #define ELEMENT_H 00003 00004 #include <sstream> 00005 00006 #include "exc/aflexc.h" 00007 00008 #define WRONG_CONSTRUCTOR(A) \ 00009 checkFor4();\ 00010 std::ostringstream oss;\ 00011 oss << D;\ 00012 throw exc::InvalidArgumentException("Can't construct a " + oss.str() + "D element with " + #A + " elements"); 00013 00014 namespace afl 00015 { 00023 template <size_t D> 00024 class Element 00025 { 00026 public: 00030 Element( ) { checkFor4(); for (size_t i = 0 ; i < D+1 ; ++i) _indices[i] = 0; } 00031 00036 Element( const Element<D>& toCopy ) { for (size_t i = 0 ; i <= D ; ++i) _indices[i] = toCopy._indices[i]; } 00037 00043 Element<D>& operator=( const Element<D>& toCopy ) { for (size_t i = 0 ; i <= D ; ++i) _indices[i] = toCopy._indices[i]; return *this; } 00044 00052 Element( size_t u1, size_t u2 ); 00053 00062 Element( size_t u1, size_t u2, size_t u3 ); 00063 00073 Element( size_t u1, size_t u2, size_t u3, size_t u4 ); 00074 00085 Element( size_t u1, size_t u2, size_t u3, size_t u4, size_t u5 ); 00086 00093 size_t& operator[]( size_t i ) { if (i <= D) return _indices[i]; throw exc::OutOfBoundsException("Element access outside the element dimensions"); } 00094 00101 size_t operator[]( size_t i ) const { if (i <= D) return _indices[i]; throw exc::OutOfBoundsException("Element access outside the element dimensions"); } 00102 00103 private: 00108 void checkFor4( ) { if ( D > 4) throw exc::InvalidArgumentException("Elements can only be of 1, 2, 3, or 4 dimensions"); } 00112 size_t _indices[D+1]; 00113 }; 00114 00115 template <size_t D> 00116 Element<D>::Element( size_t, size_t ) { WRONG_CONSTRUCTOR(2); } 00117 00118 template <size_t D> 00119 Element<D>::Element( size_t, size_t, size_t ) { WRONG_CONSTRUCTOR(3); } 00120 00121 template <size_t D> 00122 Element<D>::Element( size_t, size_t, size_t, size_t ) { WRONG_CONSTRUCTOR(4); } 00123 00124 template <size_t D> 00125 Element<D>::Element( size_t, size_t, size_t, size_t, size_t) { WRONG_CONSTRUCTOR(5); } 00126 } 00127 00128 #undef WRONG_CONSTRUCTOR 00129 #endif