From e49353f457edbf03204de39cc1758db50696cb02 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 11 Jul 2014 23:10:05 +0000 Subject: [PATCH] base: use variadic templates to remove 300+ lines of redundant code. --- src/input/input.h | 4 +- src/lol/base/array.h | 428 +++-------------------------------------- src/lol/base/tuple.h | 28 +-- src/lol/math/array2d.h | 12 +- src/lol/math/array3d.h | 16 +- src/lol/math/arraynd.h | 8 +- 6 files changed, 66 insertions(+), 430 deletions(-) diff --git a/src/input/input.h b/src/input/input.h index 2a189338..9b2a3a0b 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -99,8 +99,8 @@ protected: private: static array devices; - template - int GetItemIndex(const char* name, const array& array) const + template + int GetItemIndex(const char* name, const array& array) const { for (int i = 0; i < array.Count(); ++i) { diff --git a/src/lol/base/array.h b/src/lol/base/array.h index 9e48c33e..9b42b4c2 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -476,419 +476,62 @@ protected: int m_count, m_reserved; }; -/* - * element_t types - */ - -template -class array_element -{ -public: - T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7; T8 m8; -}; - -template -class array_element -{ -public: - T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7; -}; - -template -class array_element -{ -public: - T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; -}; - -template -class array_element -{ -public: - T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; -}; - -template -class array_element -{ -public: - T1 m1; T2 m2; T3 m3; T4 m4; -}; - -template -class array_element -{ -public: - T1 m1; T2 m2; T3 m3; -}; - -template -class array_element -{ -public: - T1 m1; T2 m2; -}; - /* * array specialisations implementing specific setters */ -template -class array : public array_base, - array> -{ -#if LOL_FEATURE_CXX11_INHERIT_CONSTRUCTORS - using array_base, - array>::array_base; -#else -public: - typedef array_element element_t; - - inline array() - : array_base>::array_base() {} - - inline array(std::initializer_list const &list) - : array_base>::array_base(list) {} -#endif - -public: - inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4, - T5 const &m5, T6 const &m6, T7 const &m7, T8 const &m8) - { - if (this->m_count >= this->m_reserved) - { - T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4; - T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7; T8 tmp8 = m8; - this->Grow(); - new (&this->m_data[this->m_count].m1) T1(tmp1); - new (&this->m_data[this->m_count].m2) T2(tmp2); - new (&this->m_data[this->m_count].m3) T3(tmp3); - new (&this->m_data[this->m_count].m4) T4(tmp4); - new (&this->m_data[this->m_count].m5) T5(tmp5); - new (&this->m_data[this->m_count].m6) T6(tmp6); - new (&this->m_data[this->m_count].m7) T7(tmp7); - new (&this->m_data[this->m_count].m8) T8(tmp8); - } - else - { - new (&this->m_data[this->m_count].m1) T1(m1); - new (&this->m_data[this->m_count].m2) T2(m2); - new (&this->m_data[this->m_count].m3) T3(m3); - new (&this->m_data[this->m_count].m4) T4(m4); - new (&this->m_data[this->m_count].m5) T5(m5); - new (&this->m_data[this->m_count].m6) T6(m6); - new (&this->m_data[this->m_count].m7) T7(m7); - new (&this->m_data[this->m_count].m8) T8(m8); - } - ++this->m_count; - } -}; - -template -class array - : public array_base, - array> -{ -#if LOL_FEATURE_CXX11_INHERIT_CONSTRUCTORS - using array_base, - array>::array_base; -#else -public: - typedef array_element element_t; - - inline array() - : array_base>::array_base() {} - - inline array(std::initializer_list const &list) - : array_base>::array_base(list) {} -#endif - -public: - inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4, - T5 const &m5, T6 const &m6, T7 const &m7) - { - if (this->m_count >= this->m_reserved) - { - T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4; - T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7; - this->Grow(); - new (&this->m_data[this->m_count].m1) T1(tmp1); - new (&this->m_data[this->m_count].m2) T2(tmp2); - new (&this->m_data[this->m_count].m3) T3(tmp3); - new (&this->m_data[this->m_count].m4) T4(tmp4); - new (&this->m_data[this->m_count].m5) T5(tmp5); - new (&this->m_data[this->m_count].m6) T6(tmp6); - new (&this->m_data[this->m_count].m7) T7(tmp7); - } - else - { - new (&this->m_data[this->m_count].m1) T1(m1); - new (&this->m_data[this->m_count].m2) T2(m2); - new (&this->m_data[this->m_count].m3) T3(m3); - new (&this->m_data[this->m_count].m4) T4(m4); - new (&this->m_data[this->m_count].m5) T5(m5); - new (&this->m_data[this->m_count].m6) T6(m6); - new (&this->m_data[this->m_count].m7) T7(m7); - } - ++this->m_count; - } -}; - -template -class array - : public array_base, - array> -{ -#if LOL_FEATURE_CXX11_INHERIT_CONSTRUCTORS - using array_base, - array>::array_base; -#else -public: - typedef array_element element_t; - - inline array() - : array_base>::array_base() {} - - inline array(std::initializer_list const &list) - : array_base>::array_base(list) {} -#endif - -public: - inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4, - T5 const &m5, T6 const &m6) - { - if (this->m_count >= this->m_reserved) - { - T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4; - T5 tmp5 = m5; T6 tmp6 = m6; - this->Grow(); - new (&this->m_data[this->m_count].m1) T1(tmp1); - new (&this->m_data[this->m_count].m2) T2(tmp2); - new (&this->m_data[this->m_count].m3) T3(tmp3); - new (&this->m_data[this->m_count].m4) T4(tmp4); - new (&this->m_data[this->m_count].m5) T5(tmp5); - new (&this->m_data[this->m_count].m6) T6(tmp6); - } - else - { - new (&this->m_data[this->m_count].m1) T1(m1); - new (&this->m_data[this->m_count].m2) T2(m2); - new (&this->m_data[this->m_count].m3) T3(m3); - new (&this->m_data[this->m_count].m4) T4(m4); - new (&this->m_data[this->m_count].m5) T5(m5); - new (&this->m_data[this->m_count].m6) T6(m6); - } - ++this->m_count; - } -}; - -template -class array - : public array_base, - array> -{ -#if LOL_FEATURE_CXX11_INHERIT_CONSTRUCTORS - using array_base, - array>::array_base; -#else -public: - typedef array_element element_t; - - inline array() - : array_base>::array_base() {} - - inline array(std::initializer_list const &list) - : array_base>::array_base(list) {} -#endif - -public: - inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4, - T5 const &m5) - { - if (this->m_count >= this->m_reserved) - { - T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4; - T5 tmp5 = m5; - this->Grow(); - new (&this->m_data[this->m_count].m1) T1(tmp1); - new (&this->m_data[this->m_count].m2) T2(tmp2); - new (&this->m_data[this->m_count].m3) T3(tmp3); - new (&this->m_data[this->m_count].m4) T4(tmp4); - new (&this->m_data[this->m_count].m5) T5(tmp5); - } - else - { - new (&this->m_data[this->m_count].m1) T1(m1); - new (&this->m_data[this->m_count].m2) T2(m2); - new (&this->m_data[this->m_count].m3) T3(m3); - new (&this->m_data[this->m_count].m4) T4(m4); - new (&this->m_data[this->m_count].m5) T5(m5); - } - ++this->m_count; - } -}; - -template -class array - : public array_base, - array> -{ -#if LOL_FEATURE_CXX11_INHERIT_CONSTRUCTORS - using array_base, - array>::array_base; -#else -public: - typedef array_element element_t; - - inline array() - : array_base>::array_base() {} - - inline array(std::initializer_list const &list) - : array_base>::array_base(list) {} -#endif - -public: - inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4) - { - if (this->m_count >= this->m_reserved) - { - T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4; - this->Grow(); - new (&this->m_data[this->m_count].m1) T1(tmp1); - new (&this->m_data[this->m_count].m2) T2(tmp2); - new (&this->m_data[this->m_count].m3) T3(tmp3); - new (&this->m_data[this->m_count].m4) T4(tmp4); - } - else - { - new (&this->m_data[this->m_count].m1) T1(m1); - new (&this->m_data[this->m_count].m2) T2(m2); - new (&this->m_data[this->m_count].m3) T3(m3); - new (&this->m_data[this->m_count].m4) T4(m4); - } - ++this->m_count; - } -}; - -template -class array - : public array_base, - array> -{ -#if LOL_FEATURE_CXX11_INHERIT_CONSTRUCTORS - using array_base, - array>::array_base; -#else -public: - typedef array_element element_t; - - inline array() - : array_base>::array_base() {} - - inline array(std::initializer_list const &list) - : array_base>::array_base(list) {} -#endif - -public: - inline void Push(T1 const &m1, T2 const &m2, T3 const &m3) - { - if (this->m_count >= this->m_reserved) - { - T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; - this->Grow(); - new (&this->m_data[this->m_count].m1) T1(tmp1); - new (&this->m_data[this->m_count].m2) T2(tmp2); - new (&this->m_data[this->m_count].m3) T3(tmp3); - } - else - { - new (&this->m_data[this->m_count].m1) T1(m1); - new (&this->m_data[this->m_count].m2) T2(m2); - new (&this->m_data[this->m_count].m3) T3(m3); - } - ++this->m_count; - } -}; - -template -class array - : public array_base, - array> +template +class array : public array_base, array> { #if LOL_FEATURE_CXX11_INHERIT_CONSTRUCTORS - using array_base, - array>::array_base; + using array_base, array>::array_base; #else public: - typedef array_element element_t; + typedef tuple element_t; inline array() - : array_base>::array_base() {} + : array_base>::array_base() + {} inline array(std::initializer_list const &list) - : array_base>::array_base(list) {} + : array_base>::array_base(list) + {} #endif public: - inline void Push(T1 const &m1, T2 const &m2) + inline void Push(T... args) { if (this->m_count >= this->m_reserved) { - T1 tmp1 = m1; T2 tmp2 = m2; + tuple tmp = { args... }; this->Grow(); - new (&this->m_data[this->m_count].m1) T1(tmp1); - new (&this->m_data[this->m_count].m2) T2(tmp2); + new (&this->m_data[this->m_count].m1) tuple(tmp); } else { - new (&this->m_data[this->m_count].m1) T1(m1); - new (&this->m_data[this->m_count].m2) T2(m2); + new (&this->m_data[this->m_count]) tuple({ args... }); } ++this->m_count; } }; template -class array - : public array_base> +class array + : public array_base> { #if LOL_FEATURE_CXX11_INHERIT_CONSTRUCTORS - using array_base>::array_base; + using array_base>::array_base; #else public: typedef T element_t; inline array() - : array_base>::array_base() {} + : array_base>::array_base() + {} inline array(std::initializer_list const &list) - : array_base>::array_base(list) {} + : array_base>::array_base(list) + {} #endif }; @@ -896,32 +539,28 @@ public: * C++11 iterators */ -template -typename array::Iterator begin(array &a) +template +typename array::Iterator begin(array &a) { - return typename array::Iterator(&a, 0); + return typename array::Iterator(&a, 0); } -template -typename array::Iterator end(array &a) +template +typename array::Iterator end(array &a) { - return typename array::Iterator(&a, a.Count()); + return typename array::Iterator(&a, a.Count()); } -template -typename array::ConstIterator begin(array const &a) +template +typename array::ConstIterator begin(array const &a) { - return typename array::ConstIterator(&a, 0); + return typename array::ConstIterator(&a, 0); } -template -typename array::ConstIterator end(array const &a) +template +typename array::ConstIterator end(array const &a) { - return typename array::ConstIterator(&a, a.Count()); + return typename array::ConstIterator(&a, a.Count()); } /* @@ -929,10 +568,7 @@ typename array::ConstIterator end(array -using Array = array; +template using Array = array; #else # define Array array #endif diff --git a/src/lol/base/tuple.h b/src/lol/base/tuple.h index 3b3a67c1..90ca7425 100644 --- a/src/lol/base/tuple.h +++ b/src/lol/base/tuple.h @@ -18,15 +18,21 @@ #if !defined __LOL_BASE_TUPLE_H__ #define __LOL_BASE_TUPLE_H__ +#include + #include namespace lol { -template -class tuple +template +class tuple : public std::tuple +{ +}; + +template +class tuple { public: T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7; T8 m8; @@ -34,7 +40,7 @@ public: template -class tuple +class tuple { public: T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7; @@ -42,42 +48,42 @@ public: template -class tuple +class tuple { public: T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; }; template -class tuple +class tuple { public: T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; }; template -class tuple +class tuple { public: T1 m1; T2 m2; T3 m3; T4 m4; }; template -class tuple +class tuple { public: T1 m1; T2 m2; T3 m3; }; template -class tuple +class tuple { public: T1 m1; T2 m2; }; template -class tuple +class tuple { public: T1 m1; diff --git a/src/lol/math/array2d.h b/src/lol/math/array2d.h index 080d3100..7d51236e 100644 --- a/src/lol/math/array2d.h +++ b/src/lol/math/array2d.h @@ -30,13 +30,11 @@ namespace lol { -template -class array2d : protected array +template +class array2d : protected array { public: - typedef array super; + typedef array super; typedef typename super::element_t element_t; inline array2d() @@ -111,7 +109,7 @@ public: } private: - array2d &m_array; + array2d &m_array; int m_column; }; @@ -132,7 +130,7 @@ public: } private: - array2d const &m_array; + array2d const &m_array; int m_column; }; diff --git a/src/lol/math/array3d.h b/src/lol/math/array3d.h index 4b1fa22f..48917001 100644 --- a/src/lol/math/array3d.h +++ b/src/lol/math/array3d.h @@ -30,13 +30,11 @@ namespace lol { -template -class array3d : protected array +template +class array3d : protected array { public: - typedef array super; + typedef array super; typedef typename super::element_t element_t; inline array3d() @@ -128,7 +126,7 @@ public: } private: - array3d &m_array; + array3d &m_array; ivec2 m_line; }; @@ -140,7 +138,7 @@ public: } private: - array3d &m_array; + array3d &m_array; int m_slice; }; @@ -170,7 +168,7 @@ public: } private: - array3d const &m_array; + array3d const &m_array; ivec2 m_line; }; @@ -182,7 +180,7 @@ public: } private: - array3d const &m_array; + array3d const &m_array; int m_slice; }; diff --git a/src/lol/math/arraynd.h b/src/lol/math/arraynd.h index 35f2e89f..433dbc06 100644 --- a/src/lol/math/arraynd.h +++ b/src/lol/math/arraynd.h @@ -153,13 +153,11 @@ private: }; -template -class arraynd : protected array +template +class arraynd : protected array { public: - typedef array super; + typedef array super; typedef typename super::element_t element_t; typedef arraynd_proxy proxy;