/* */ #ifndef _D_ARRAY_FUN_H_ #define _D_ARRAY_FUN_H_ #include #include namespace aria2 { // calculate length of array template char (&char_array_ref(T (&)[N]))[N]; template size_t arrayLength(T (&a)[N]) { return sizeof(char_array_ref(a)); } template size_t arrayLength(T (&a)[0u]) { return 0; } template class array_ptr { private: T* _array; // Copies are not allowed. Let's make them private. array_ptr(const array_ptr& s); array_ptr& operator=(const array_ptr& s); template array_ptr& operator=(const array_ptr& s); public: array_ptr():_array(0) {} explicit array_ptr(T* array):_array(array) {} ~array_ptr() { delete [] _array; } operator T*() { return _array; } operator const T*() const { return _array; } }; template class array_wrapper { private: T _array[N]; public: array_wrapper() {} operator T*() { return _array; } operator const T*() const { return _array; } size_t size() const { return N; } }; // Expression Template for array namespace expr { template struct BinExpr { BinExpr(const L& l, const R& r):_l(l), _r(r) {} typedef typename OpTag::returnType returnType; returnType operator[](size_t index) const { return OpTag::apply(_l[index], _r[index]); } const L& _l; const R& _r; }; template struct UnExpr { UnExpr(const A& a):_a(a) {} typedef typename OpTag::returnType returnType; returnType operator[](size_t index) const { return OpTag::apply(_a[index]); } const A& _a; }; template struct And { typedef T returnType; static inline returnType apply(T lhs, T rhs) { return lhs&rhs; } }; template struct Negate { typedef T returnType; static inline returnType apply(T a) { return ~a; } }; template struct Array { typedef T returnType; Array(const T* t):_t(t) {} const T* _t; returnType operator[](size_t index) const { return _t[index]; } }; template Array array(const T* t) { return Array(t); } template BinExpr, R> operator&(const L& l, const R& r) { return BinExpr, R>(l, r); } template UnExpr, A> operator~(const A& a) { return UnExpr, A>(a); } } // namespace expr } // namespace aria2 #endif // _D_ARRAY_FUN_H_