From d9366475b516d7d598da4e15b4eacfa138608c7f Mon Sep 17 00:00:00 2001 From: q66 Date: Thu, 4 Jun 2015 02:20:20 +0100 Subject: [PATCH] fully cleaned up memory.h --- octa/memory.h | 250 +++++++++++++++++++++++++------------------------- 1 file changed, 125 insertions(+), 125 deletions(-) diff --git a/octa/memory.h b/octa/memory.h index 0d36ba8..5218c27 100644 --- a/octa/memory.h +++ b/octa/memory.h @@ -13,198 +13,198 @@ #include "octa/type_traits.h" namespace octa { - /* address of */ +/* address of */ - template constexpr _T *address_of(_T &v) { - return reinterpret_cast<_T *>(&const_cast - (reinterpret_cast(v))); - } +template constexpr _T *address_of(_T &v) { + return reinterpret_cast<_T *>(&const_cast + (reinterpret_cast(v))); +} - /* pointer traits */ +/* pointer traits */ +namespace detail { template - struct __OctaHasElement { - template - static int __test(...); - template - static char __test(typename _U::Element * = 0); + struct HasElement { + template static int test(...); + template static char test(typename _U::Element * = 0); - static constexpr bool value = (sizeof(__test<_T>(0)) == 1); + static constexpr bool value = (sizeof(test<_T>(0)) == 1); }; - template::value> - struct __OctaPtrTraitsElementType; + template::value> + struct PointerElementBase; - template struct __OctaPtrTraitsElementType<_T, true> { + template struct PointerElementBase<_T, true> { typedef typename _T::Element Type; }; template class _T, typename _U, typename ..._A> - struct __OctaPtrTraitsElementType<_T<_U, _A...>, true> { + struct PointerElementBase<_T<_U, _A...>, true> { typedef typename _T<_U, _A...>::Element Type; }; template class _T, typename _U, typename ..._A> - struct __OctaPtrTraitsElementType<_T<_U, _A...>, false> { + struct PointerElementBase<_T<_U, _A...>, false> { typedef _U Type; }; template - struct __OctaHasDifference { - template - static int __test(...); - template - static char __test(typename _U::Difference * = 0); - - static constexpr bool value = (sizeof(__test<_T>(0)) == 1); + struct PointerElementType { + typedef typename PointerElementBase<_T>::Type Type; }; - template::value> - struct __OctaPtrTraitsDifferenceType { + template + struct PointerElementType<_T *> { + typedef _T Type; + }; + + template + struct HasDifference { + template static int test(...); + template static char test(typename _U::Difference * = 0); + + static constexpr bool value = (sizeof(test<_T>(0)) == 1); + }; + + template::value> + struct PointerDifferenceBase { typedef ptrdiff_t Type; }; - template struct __OctaPtrTraitsDifferenceType<_T, true> { + template struct PointerDifferenceBase<_T, true> { typedef typename _T::Difference Type; }; - template - struct __OctaHasRebind { - template - static int __test(...); - template - static char __test(typename _V::template Rebind<_U> * = 0); - - static constexpr bool value = (sizeof(__test<_T>(0)) == 1); + template + struct PointerDifferenceType { + typedef typename PointerDifferenceBase<_T>::Type Type; }; - template::value> - struct __OctaPtrTraitsRebindType { + template + struct PointerDifferenceType<_T *> { + typedef ptrdiff_t Type; + }; + + template + struct HasRebind { + template static int test(...); + template static char test( + typename _V::template Rebind<_U> * = 0); + + static constexpr bool value = (sizeof(test<_T>(0)) == 1); + }; + + template::value> + struct PointerRebindBase { typedef typename _T::template Rebind<_U> Type; }; template class _T, typename _U, typename ..._A, typename _V > - struct __OctaPtrTraitsRebindType<_T<_U, _A...>, _V, true> { + struct PointerRebindBase<_T<_U, _A...>, _V, true> { typedef typename _T<_U, _A...>::template Rebind<_V> Type; }; template class _T, typename _U, typename ..._A, typename _V > - struct __OctaPtrTraitsRebindType<_T<_U, _A...>, _V, false> { + struct PointerRebindBase<_T<_U, _A...>, _V, false> { typedef _T<_V, _A...> Type; }; - template - struct __OctaPtrTraitsPointer { - typedef _T Type; - }; - - template - struct __OctaPtrTraitsPointer<_T *> { - typedef _T *Type; - }; - - template - using Pointer = typename __OctaPtrTraitsPointer<_T>::Type; - - template - struct __OctaPtrTraitsElement { - typedef typename __OctaPtrTraitsElementType<_T>::Type Type; - }; - - template - struct __OctaPtrTraitsElement<_T *> { - typedef _T Type; - }; - - template - using PointerElement = typename __OctaPtrTraitsElement<_T>::Type; - - template - struct __OctaPtrTraitsDifference { - typedef typename __OctaPtrTraitsDifferenceType<_T>::Type Type; - }; - - - template - struct __OctaPtrTraitsDifference<_T *> { - typedef ptrdiff_t Type; - }; - - template - using PointerDifference = typename __OctaPtrTraitsDifference<_T>::Type; - template - struct __OctaPtrTraitsRebind { - using type = typename __OctaPtrTraitsRebindType<_T, _U>::Type; + struct PointerRebindType { + using type = typename PointerRebindBase<_T, _U>::Type; }; template - struct __OctaPtrTraitsRebind<_T *, _U> { + struct PointerRebindType<_T *, _U> { using type = _U *; }; - template - using PointerRebind = typename __OctaPtrTraitsRebind<_T, _U>::Type; - - struct __OctaPtrTraitsNat {}; + template + struct PointerPointer { + typedef _T Type; + }; template - struct __OctaPtrTraitsPointerTo { + struct PointerPointer<_T *> { + typedef _T *Type; + }; +} /*namespace detail */ + +template +using Pointer = typename octa::detail::PointerPointer<_T>::Type; + +template +using PointerElement = typename octa::detail::PointerElementType<_T>::Type; + +template +using PointerDifference = typename octa::detail::PointerDifferenceType<_T>::Type; + +template +using PointerRebind = typename octa::detail::PointerRebindType<_T, _U>::Type; + +/* pointer to */ + +namespace detail { + struct PointerToNat {}; + + template + struct PointerTo { static _T pointer_to(octa::Conditional< octa::IsVoid>::value, - __OctaPtrTraitsNat, PointerElement<_T> - > &__r) { - return _T::pointer_to(__r); + PointerToNat, PointerElement<_T> + > &r) { + return _T::pointer_to(r); } }; template - struct __OctaPtrTraitsPointerTo<_T *> { + struct PointerTo<_T *> { static _T pointer_to(octa::Conditional< - octa::IsVoid<_T>::value, __OctaPtrTraitsNat, _T - > &__r) { - return octa::address_of(__r); + octa::IsVoid<_T>::value, PointerToNat, _T + > &r) { + return octa::address_of(r); } }; +} - template - static _T pointer_to(octa::Conditional< - octa::IsVoid>::value, - __OctaPtrTraitsNat, PointerElement<_T> - > &__r) { - return __OctaPtrTraitsPointerTo<_T>::pointer_to(__r); +template +static _T pointer_to(octa::Conditional< + octa::IsVoid>::value, + octa::detail::PointerToNat, PointerElement<_T> +> &r) { + return octa::detail::PointerTo<_T>::pointer_to(r); +} + +/* default deleter */ + +template +struct DefaultDelete { + constexpr DefaultDelete() = default; + + template DefaultDelete(const DefaultDelete<_U> &) {}; + + void operator()(_T *p) const { + delete p; } +}; - /* default deleter */ +template +struct DefaultDelete<_T[]> { + constexpr DefaultDelete() = default; - template - struct DefaultDelete { - constexpr DefaultDelete() = default; + template DefaultDelete(const DefaultDelete<_U[]> &) {}; - template DefaultDelete(const DefaultDelete<_U> &) {}; + void operator()(_T *p) const { + delete[] p; + } + template void operator()(_U *) const = delete; +}; - void operator()(_T *p) const { - delete p; - } - }; - - template - struct DefaultDelete<_T[]> { - constexpr DefaultDelete() = default; - - template DefaultDelete(const DefaultDelete<_U[]> &) {}; - - void operator()(_T *p) const { - delete[] p; - } - template void operator()(_U *) const = delete; - }; - - /* box */ +/* box */ namespace detail { template::value> @@ -379,7 +379,7 @@ namespace detail { struct SameOrLessCvQualifiedBase<_T, _U, false>: octa::False {}; template::value - || octa::IsSame<_T, _U>::value || __OctaHasElement<_T>::value + || octa::IsSame<_T, _U>::value || octa::detail::HasElement<_T>::value > struct SameOrLessCvQualified: SameOrLessCvQualifiedBase<_T, _U> {}; template @@ -782,7 +782,7 @@ using AllocatorSize = typename octa::detail::SizeBase< /* allocator rebind */ namespace detail { - template::value> + template::value> struct AllocTraitsRebindType { typedef typename _T::template Rebind<_U> Type; };