From d7778cf2594dac66f8c6b656c04f4523c53d09a1 Mon Sep 17 00:00:00 2001 From: q66 Date: Mon, 27 Apr 2015 19:38:34 +0100 Subject: [PATCH] ditch the separate "type" struct --- octa/algorithm.h | 4 +- octa/array.h | 24 ++++----- octa/functional.h | 84 ++++++++++++----------------- octa/initializer_list.h | 16 +++--- octa/memory.h | 116 ++++++++++++++++++---------------------- octa/range.h | 48 +++++++++-------- octa/vector.h | 24 ++++----- 7 files changed, 142 insertions(+), 174 deletions(-) diff --git a/octa/algorithm.h b/octa/algorithm.h index 2e82e46..1144522 100644 --- a/octa/algorithm.h +++ b/octa/algorithm.h @@ -53,7 +53,7 @@ namespace octa { template void insertion_sort(R range) { - insertion_sort(range, Less::value>()); + insertion_sort(range, Less::value_type>()); } /* sort (introsort) */ @@ -130,7 +130,7 @@ namespace octa { template void sort(R range) { - sort(range, Less::value>()); + sort(range, Less::value_type>()); } /* min/max(_element) */ diff --git a/octa/array.h b/octa/array.h index 3a665b4..d8e125b 100644 --- a/octa/array.h +++ b/octa/array.h @@ -13,17 +13,15 @@ namespace octa { template struct Array { - struct type { - typedef T value; - typedef T &reference; - typedef const T &const_reference; - typedef T *pointer; - typedef const T *const_pointer; - typedef size_t size; - typedef ptrdiff_t difference; - typedef PointerRange< T> range; - typedef PointerRange const_range; - }; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T value_type; + typedef T &reference; + typedef const T &const_reference; + typedef T *pointer; + typedef const T *const_pointer; + typedef PointerRange< T> range; + typedef PointerRange const_range; T &operator[](size_t i) { return p_buf[i]; } const T &operator[](size_t i) const { return p_buf[i]; } @@ -47,10 +45,10 @@ namespace octa { swap(p_buf, v.p_buf); } - typename type::range each() { + range each() { return PointerRange(p_buf, p_buf + N); } - typename type::const_range each() const { + const_range each() const { return PointerRange(p_buf, p_buf + N); } diff --git a/octa/functional.h b/octa/functional.h index 5f52db1..d27a6be 100644 --- a/octa/functional.h +++ b/octa/functional.h @@ -17,11 +17,9 @@ namespace octa { #define __OCTA_DEFINE_BINARY_OP(name, op, rettype) \ template struct name { \ bool operator()(const T &x, const T &y) const { return x op y; } \ - struct type { \ - typedef T first; \ - typedef T second; \ - typedef rettype result; \ - }; \ + typedef T first_argument_type; \ + typedef T second_argument_type; \ + typedef rettype result_type; \ }; __OCTA_DEFINE_BINARY_OP(Less, <, bool) @@ -45,29 +43,24 @@ namespace octa { template struct LogicalNot { bool operator()(const T &x) const { return !x; } - struct type { - typedef T argument; - typedef bool result; - }; + typedef T argument_type; + typedef bool result_type; }; template struct Negate { bool operator()(const T &x) const { return -x; } - struct type { - typedef T argument; - typedef T result; - }; + typedef T argument_type; + typedef T result_type; }; template struct BinaryNegate { - struct type { - typedef typename T::type::first first; - typedef typename T::type::second second; - typedef bool result; - }; + typedef typename T::first_argument_type first_argument_type; + typedef typename T::second_argument_type second_argument_type; + typedef bool result_type; + explicit BinaryNegate(const T &f): p_fn(f) {} - bool operator()(const typename type::first &x, - const typename type::second &y) { + bool operator()(const first_argument_type &x, + const second_argument_type &y) { return !p_fn(x, y); } private: @@ -75,12 +68,11 @@ namespace octa { }; template struct UnaryNegate { - struct type { - typedef typename T::type::argument argument; - typedef bool result; - }; + typedef typename T::argument_type argument_type; + typedef bool result_type; + explicit UnaryNegate(const T &f): p_fn(f) {} - bool operator()(const typename type::argument &x) { + bool operator()(const argument_type &x) { return !p_fn(x); } private: @@ -135,33 +127,31 @@ namespace octa { template struct __OctaMemTypes; template struct __OctaMemTypes { - typedef R result; - typedef T argument; + typedef R result_type; + typedef T argument_type; }; template struct __OctaMemTypes { - typedef R result; - typedef T first; - typedef A second; + typedef R result_type; + typedef T first_argument_type; + typedef A second_argument_type; }; template struct __OctaMemTypes { - typedef R result; - typedef const T argument; + typedef R result_type; + typedef const T argument_type; }; template struct __OctaMemTypes { - typedef R result; - typedef const T first; - typedef A second; + typedef R result_type; + typedef const T first_argument_type; + typedef A second_argument_type; }; template - class __OctaMemFn { + class __OctaMemFn: __OctaMemTypes { R T::*p_ptr; public: - struct type: __OctaMemTypes {}; - __OctaMemFn(R T::*ptr): p_ptr(ptr) {} template auto operator()(T &obj, A &&...args) -> @@ -326,26 +316,20 @@ namespace octa { template struct __OctaFunction { - struct type { - typedef R result; - }; + typedef R result_type; }; template struct __OctaFunction { - struct type { - typedef R result; - typedef T argument; - }; + typedef R result_type; + typedef T argument_type; }; template struct __OctaFunction { - struct type { - typedef R result; - typedef T first; - typedef U second; - }; + typedef R result_type; + typedef T first_argument_type; + typedef U second_argument_type; }; template diff --git a/octa/initializer_list.h b/octa/initializer_list.h index fba8570..374d84a 100644 --- a/octa/initializer_list.h +++ b/octa/initializer_list.h @@ -19,15 +19,13 @@ namespace std { initializer_list(const T *v, size_t n): p_buf(v), p_len(n) {} public: - struct type { - typedef T value; - typedef T &reference; - typedef const T &const_reference; - typedef T *pointer; - typedef const T *const_pointer; - typedef size_t size; - typedef ptrdiff_t difference; - }; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T value_type; + typedef T &reference; + typedef const T &const_reference; + typedef T *pointer; + typedef const T *const_pointer; initializer_list(): p_buf(nullptr), p_len(0) {} diff --git a/octa/memory.h b/octa/memory.h index f599d10..fbd3f71 100644 --- a/octa/memory.h +++ b/octa/memory.h @@ -24,7 +24,7 @@ namespace octa { template static int __octa_test(...); template - static char __octa_test(typename U::type::element * = 0); + static char __octa_test(typename U::element_type * = 0); static constexpr bool value = (sizeof(__octa_test(0)) == 1); }; @@ -33,12 +33,12 @@ namespace octa { struct __OctaPtrTraitsElementType; template struct __OctaPtrTraitsElementType { - typedef typename T::type::element type; + typedef typename T::element_type type; }; template class T, typename U, typename ...A> struct __OctaPtrTraitsElementType, true> { - typedef typename T::type::element type; + typedef typename T::element_type type; }; template class T, typename U, typename ...A> @@ -51,7 +51,7 @@ namespace octa { template static int __octa_test(...); template - static char __octa_test(typename U::type::difference * = 0); + static char __octa_test(typename U::difference_type * = 0); static constexpr bool value = (sizeof(__octa_test(0)) == 1); }; @@ -62,7 +62,7 @@ namespace octa { }; template struct __OctaPtrTraitsDifferenceType { - typedef typename T::type::difference type; + typedef typename T::difference_type type; }; template @@ -70,7 +70,7 @@ namespace octa { template static int __octa_test(...); template - static char __octa_test(typename V::type::template rebind * = 0); + static char __octa_test(typename V::template rebind * = 0); static constexpr bool value = (sizeof(__octa_test(0)) == 1); }; @@ -84,7 +84,7 @@ namespace octa { typename ...A, typename V > struct __OctaPtrTraitsRebind, V, true> { - typedef typename T::type::template rebind type; + typedef typename T::template rebind type; }; template class T, typename U, @@ -98,8 +98,8 @@ namespace octa { struct PointerTraits { typedef T pointer; - typedef typename __OctaPtrTraitsElementType ::type element; - typedef typename __OctaPtrTraitsDifferenceType::type difference; + typedef typename __OctaPtrTraitsElementType ::type element_type; + typedef typename __OctaPtrTraitsDifferenceType::type difference_type; template using rebind = typename __OctaPtrTraitsRebind::type; @@ -108,8 +108,8 @@ namespace octa { struct __OctaNAT {}; public: - static T pointer_to(Conditional::value, - __OctaNAT, element + static T pointer_to(Conditional::value, + __OctaNAT, element_type > &r) { return T::pointer_to(r); } @@ -118,9 +118,9 @@ namespace octa { template struct PointerTraits { typedef T *pointer; - typedef T element; + typedef T element_type; - typedef ptrdiff_t difference; + typedef ptrdiff_t difference_type; template using rebind = U *; @@ -128,8 +128,8 @@ namespace octa { struct __OctaNAT {}; public: - static T pointer_to(Conditional::value, - __OctaNAT, element + static T pointer_to(Conditional::value, + __OctaNAT, element_type > &r) { return octa::address_of(r); } @@ -165,7 +165,7 @@ namespace octa { template static int __octa_ptr_test(...); template - static char __octa_ptr_test(typename T::type::pointer * = 0); + static char __octa_ptr_test(typename T::pointer * = 0); template struct __OctaHasPtr: IntegralConstant(0)) == 1) @@ -173,7 +173,7 @@ namespace octa { template::value> struct __OctaPtrTypeBase { - typedef typename D::type::pointer type; + typedef typename D::pointer type; }; template struct __OctaPtrTypeBase { @@ -186,11 +186,9 @@ namespace octa { template> struct Box { - struct type { - typedef T element; - typedef D deleter; - typedef typename __OctaPtrType::type pointer; - }; + typedef T element_type; + typedef D deleter_type; + typedef typename __OctaPtrType::type pointer; private: struct __OctaNAT { int x; }; @@ -208,17 +206,16 @@ namespace octa { "Box constructed with null fptr deleter"); } - explicit Box(typename type::pointer p): p_ptr(p), p_del() { + explicit Box(pointer p): p_ptr(p), p_del() { static_assert(!IsPointer::value, "Box constructed with null fptr deleter"); } - Box(typename type::pointer p, Conditional::value, + Box(pointer p, Conditional::value, D, AddLvalueReference > d): p_ptr(p), p_del(d) {} - Box(typename type::pointer p, RemoveReference &&d): p_ptr(p), - p_del(move(d)) { + Box(pointer p, RemoveReference &&d): p_ptr(p), p_del(move(d)) { static_assert(!IsReference::value, "rvalue deleter cannot be a ref"); } @@ -227,9 +224,7 @@ namespace octa { template Box(Box &&u, EnableIf::value - && IsConvertible::type::pointer, - typename type::pointer - >::value + && IsConvertible::pointer, pointer>::value && IsConvertible::value && (!IsReference::value || IsSame::value) > = __OctaNAT()): p_ptr(u.release()), @@ -243,8 +238,7 @@ namespace octa { template EnableIf::value - && IsConvertible::type::pointer, - typename type::pointer>::value + && IsConvertible::pointer, pointer>::value && IsAssignable::value, Box & > operator=(Box &&u) { @@ -261,23 +255,23 @@ namespace octa { ~Box() { reset(); } AddLvalueReference operator*() const { return *p_ptr; } - typename type::pointer operator->() const { return p_ptr; } + pointer operator->() const { return p_ptr; } explicit operator bool() const { return p_ptr != nullptr; } - typename type::pointer get() const { return p_ptr; } + pointer get() const { return p_ptr; } D_ref get_deleter() { return p_del; } D_cref get_deleter() const { return p_del; } - typename type::pointer release() { - typename type::pointer p = p_ptr; + pointer release() { + pointer p = p_ptr; p_ptr = nullptr; return p; } - void reset(typename type::pointer p = nullptr) { - typename type::pointer tmp = p_ptr; + void reset(pointer p = nullptr) { + pointer tmp = p_ptr; p_ptr = p; if (tmp) p_del(tmp); } @@ -294,8 +288,8 @@ namespace octa { }; template::element>, - RemoveCV::element> + RemoveCV::element_type>, + RemoveCV::element_type> >::value> struct __OctaSameOrLessCVQualifiedBase: IsConvertible {}; template @@ -310,11 +304,9 @@ namespace octa { template struct Box { - struct type { - typedef T element; - typedef D deleter; - typedef typename __OctaPtrType::type pointer; - }; + typedef T element_type; + typedef D deleter_type; + typedef typename __OctaPtrType::type pointer; private: struct __OctaNAT { int x; }; @@ -332,28 +324,24 @@ namespace octa { "Box constructed with null fptr deleter"); } - template - explicit Box(U p, EnableIf<__OctaSameOrLessCVQualified::value, __OctaNAT + template explicit Box(U p, EnableIf< + __OctaSameOrLessCVQualified::value, __OctaNAT > = __OctaNAT()): p_ptr(p), p_del() { static_assert(!IsPointer::value, "Box constructed with null fptr deleter"); } - template - Box(U p, Conditional::value, + template Box(U p, Conditional::value, D, AddLvalueReference - > d, EnableIf<__OctaSameOrLessCVQualified::value, __OctaNAT + > d, EnableIf<__OctaSameOrLessCVQualified::value, __OctaNAT > = __OctaNAT()): p_ptr(p), p_del(d) {} Box(nullptr_t, Conditional::value, D, AddLvalueReference > d): p_ptr(), p_del(d) {} - template - Box(U p, RemoveReference &&d, EnableIf<__OctaSameOrLessCVQualified::value, __OctaNAT + template Box(U p, RemoveReference &&d, EnableIf< + __OctaSameOrLessCVQualified::value, __OctaNAT > = __OctaNAT()): p_ptr(p), p_del(move(d)) { static_assert(!IsReference::value, "rvalue deleter cannot be a ref"); @@ -368,8 +356,8 @@ namespace octa { template Box(Box &&u, EnableIf::value - && __OctaSameOrLessCVQualified::type::pointer, - typename type::pointer>::value + && __OctaSameOrLessCVQualified::pointer, + pointer>::value && IsConvertible::value && (!IsReference::value || IsSame::value)> = __OctaNAT() ): p_ptr(u.release()), p_del(forward
(u.get_deleter())) {} @@ -382,8 +370,8 @@ namespace octa { template EnableIf::value - && __OctaSameOrLessCVQualified::type::pointer, - typename type::pointer>::value + && __OctaSameOrLessCVQualified::pointer, + pointer>::value && IsAssignable::value, Box & > operator=(Box &&u) { @@ -405,27 +393,27 @@ namespace octa { explicit operator bool() const { return p_ptr != nullptr; } - typename type::pointer get() const { return p_ptr; } + pointer get() const { return p_ptr; } D_ref get_deleter() { return p_del; } D_cref get_deleter() const { return p_del; } - typename type::pointer release() { - typename type::pointer p = p_ptr; + pointer release() { + pointer p = p_ptr; p_ptr = nullptr; return p; } template EnableIf< - __OctaSameOrLessCVQualified::value, void + __OctaSameOrLessCVQualified::value, void > reset(U p) { - typename type::pointer tmp = p_ptr; + pointer tmp = p_ptr; p_ptr = p; if (tmp) p_del(tmp); } void reset(nullptr_t) { - typename type::pointer tmp = p_ptr; + pointer tmp = p_ptr; p_ptr = nullptr; if (tmp) p_del(tmp); } diff --git a/octa/range.h b/octa/range.h index 26eb3e2..a07ba40 100644 --- a/octa/range.h +++ b/octa/range.h @@ -20,9 +20,9 @@ namespace octa { template struct RangeTraits { - typedef typename T::type::category category; - typedef typename T::type::value value; - typedef typename T::type::reference reference; + typedef typename T::range_category range_category; + typedef typename T::value_type value_type; + typedef typename T::reference reference; }; template @@ -46,11 +46,9 @@ namespace octa { template struct InputRangeBase { - struct type { - typedef C category; - typedef V value; - typedef R reference; - }; + typedef C range_category; + typedef V value_type; + typedef R reference; __OctaRangeIterator begin() { return __OctaRangeIterator((const B &)*this); @@ -62,17 +60,15 @@ namespace octa { template struct OutputRangeBase { - struct type { - typedef OutputRange category; - typedef V value; - typedef R reference; - }; + typedef OutputRange range_category; + typedef V value_type; + typedef R reference; }; template struct ReverseRange: InputRangeBase, - typename RangeTraits::category, - typename RangeTraits::value, + typename RangeTraits::range_category, + typename RangeTraits::value_type, typename RangeTraits::reference > { ReverseRange(): p_range() {} @@ -139,9 +135,9 @@ namespace octa { template struct MoveRange: InputRangeBase, - typename RangeTraits::category, - typename RangeTraits::value, - typename RangeTraits::value && + typename RangeTraits::range_category, + typename RangeTraits::value_type, + typename RangeTraits::value_type && > { MoveRange(): p_range() {} MoveRange(const T &range): p_range(range) {} @@ -178,10 +174,14 @@ namespace octa { return p_range != v.p_range; } - typename RangeTraits::value &&first() { return move(p_range.first()); } - typename RangeTraits::value &&last () { return move(p_range.last ()); } + typename RangeTraits::value_type &&first() { + return move(p_range.first()); + } + typename RangeTraits::value_type &&last () { + return move(p_range.last()); + } - typename RangeTraits::value &&operator[](size_t i) { + typename RangeTraits::value_type &&operator[](size_t i) { return move(p_range[i]); } @@ -189,7 +189,9 @@ namespace octa { return MoveRange(p_range.slice(start, end)); } - void put(const typename RangeTraits::value &v) { p_range.put(v); } + void put(const typename RangeTraits::value_type &v) { + p_range.put(v); + } private: T p_range; @@ -292,7 +294,7 @@ namespace octa { template struct EnumeratedRange: InputRangeBase, - InputRange, typename RangeTraits::value, + InputRange, typename RangeTraits::value_type, EnumeratedValue::reference> > { EnumeratedRange(): p_range(), p_index(0) {} diff --git a/octa/vector.h b/octa/vector.h index 583d860..9381e5a 100644 --- a/octa/vector.h +++ b/octa/vector.h @@ -33,17 +33,15 @@ namespace octa { public: enum { MIN_SIZE = 8 }; - struct type { - typedef T value; - typedef T &reference; - typedef const T &const_reference; - typedef T *pointer; - typedef const T *const_pointer; - typedef size_t size; - typedef ptrdiff_t difference; - typedef PointerRange< T> range; - typedef PointerRange const_range; - }; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T value_type; + typedef T &reference; + typedef const T &const_reference; + typedef T *pointer; + typedef const T *const_pointer; + typedef PointerRange< T> range; + typedef PointerRange const_range; Vector(): p_buf(nullptr), p_len(0), p_cap(0) {} @@ -268,10 +266,10 @@ namespace octa { return insert_range(idx, il.range()); } - typename type::range each() { + range each() { return PointerRange(p_buf, p_buf + p_len); } - typename type::const_range each() const { + const_range each() const { return PointerRange(p_buf, p_buf + p_len); }