Changeset 08c1df0 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:24Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f31ea60
Parents:
ca8d393
git-author:
Dzejrou <dzejrou@…> (2018-05-16 17:20:56)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: fixed typedefs in allocator_traits, finished type getters and implemented conditional calls in allocator_traits

Location:
uspace/lib/cpp/include
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/memory.hpp

    rca8d393 r08c1df0  
    3636#include <internal/memory/type_getters.hpp>
    3737#include <iterator>
     38#include <limits>
    3839#include <new>
    3940#include <type_traits>
     
    131132        using allocator_type = Alloc;
    132133
    133         using value_type = typename Alloc::value_type;
    134         using pointer = typename aux::get_pointer<Alloc>::type;
    135         using const_pointer = typename aux::get_const_pointer<Alloc, pointer>::type;
    136         // TODO: fix void pointer typedefs
    137         /* using void_pointer = typename aux::get_void_pointer<Alloc, pointer>::type; */
    138         /* using const_void_pointer = typename aux::get_const_void_pointer<Alloc, pointer>::type; */
    139         using void_pointer = void*;
    140         using const_void_pointer = const void*;
    141         using difference_type = typename aux::get_difference_type<Alloc, pointer>::type;
    142         using size_type = typename aux::get_size_type<Alloc, difference_type>::type;
    143 
    144         using propagate_on_container_copy_assignment = typename aux::get_copy_propagate<Alloc>::type;
    145         using propagate_on_container_move_assignment = typename aux::get_move_propagate<Alloc>::type;
    146         using propagate_on_container_swap = typename aux::get_swap_propagate<Alloc>::type;
    147         using is_always_equal = typename aux::get_always_equal<Alloc>::type;
     134        using value_type         = typename Alloc::value_type;
     135        using pointer            = typename aux::alloc_get_pointer<Alloc>::type;
     136        using const_pointer      = typename aux::alloc_get_const_pointer<Alloc, pointer>::type;
     137        using void_pointer       = typename aux::alloc_get_void_pointer<Alloc, pointer>::type;
     138        using const_void_pointer = typename aux::alloc_get_const_void_pointer<Alloc, pointer>::type;
     139        using difference_type    = typename aux::alloc_get_difference_type<Alloc, pointer>::type;
     140        using size_type          = typename aux::alloc_get_size_type<Alloc, difference_type>::type;
     141
     142        using propagate_on_container_copy_assignment = typename aux::alloc_get_copy_propagate<Alloc>::type;
     143        using propagate_on_container_move_assignment = typename aux::alloc_get_move_propagate<Alloc>::type;
     144        using propagate_on_container_swap            = typename aux::alloc_get_swap_propagate<Alloc>::type;
     145        using is_always_equal                        = typename aux::alloc_get_always_equal<Alloc>::type;
    148146
    149147        template<class T>
    150         using rebind_alloc = typename aux::get_rebind_args<Alloc, T>;
     148        using rebind_alloc = typename aux::alloc_get_rebind_alloc<Alloc, T>;
    151149
    152150        template<class T>
     
    159157
    160158        static pointer allocate(Alloc& alloc, size_type n, const_void_pointer hint)
    161         { // TODO: this when it's well formed, otherwise alloc.allocate(n)
    162             return alloc.allocate(n, hint);
     159        {
     160            if constexpr (aux::alloc_has_hint_allocate<Alloc, size_type, const_void_pointer>::value)
     161                return alloc.allocate(n, hint);
     162            else
     163                return alloc.allocate(n);
    163164        }
    164165
     
    171172        static void construct(Alloc& alloc, T* ptr, Args&&... args)
    172173        {
    173             // TODO: why wasn't this implemented? check standard for remarks
    174             alloc.construct(ptr, forward<Args>(args)...);
     174            if constexpr (aux::alloc_has_construct<Alloc, T, Args...>::value)
     175                alloc.construct(ptr, forward<Args>(args)...);
     176            else
     177                ::new(static_cast<void*>(ptr)) T(forward<Args>(args)...);
    175178        }
    176179
     
    178181        static void destroy(Alloc& alloc, T* ptr)
    179182        {
    180             // TODO: implement
     183            if constexpr (aux::alloc_has_destroy<Alloc, T>::value)
     184                alloc.destroy(ptr);
     185            else
     186                ptr->~T();
    181187        }
    182188
    183189        static size_type max_size(const Alloc& alloc) noexcept
    184190        {
    185             // TODO: implement
    186             return 0;
     191            if constexpr (aux::alloc_has_max_size<Alloc>::value)
     192                return alloc.max_size();
     193            else
     194                return numeric_limits<size_type>::max();
    187195        }
    188196
    189197        static Alloc select_on_container_copy_construction(const Alloc& alloc)
    190198        {
    191             // TODO: implement
    192             return Alloc{};
     199            if constexpr (aux::alloc_has_select<Alloc>::value)
     200                return alloc.select_on_container_copy_construction();
     201            else
     202                return alloc;
    193203        }
    194204    };
     
    246256            template<class U>
    247257            allocator(const allocator<U>&) noexcept
    248             {
    249                 // TODO: implement
    250             }
     258            { /* DUMMY BODY */ }
    251259
    252260            ~allocator() = default;
     
    262270            }
    263271
    264             pointer allocate(size_type n, allocator<void>::const_pointer hint = 0)
    265             {
    266                 /**
    267                  * Note: The usage of hint is unspecified.
    268                  *       TODO: Check HelenOS hint allocation capabilities.
    269                  * TODO: assert that n < max_size()
    270                  */
     272            pointer allocate(size_type n, allocator<void>::const_pointer = 0)
     273            {
    271274                return static_cast<pointer>(::operator new(n * sizeof(value_type)));
    272275            }
     
    278281
    279282            size_type max_size() const noexcept
    280             { // TODO: implement, max argument to allocate
    281                 return 0xFFFFFFFF;
     283            {
     284                return numeric_limits<size_type>::max();
    282285            }
    283286
  • uspace/lib/cpp/include/internal/memory/type_getters.hpp

    rca8d393 r08c1df0  
    9898
    9999    template<class T, class = void>
    100     struct get_pointer: aux::type_is<typename T::value_type*>
    101     { /* DUMMY BODY */ };
    102 
    103     template<class T>
    104     struct get_pointer<T, void_t<typename T::pointer>>
     100    struct alloc_get_pointer: aux::type_is<typename T::value_type*>
     101    { /* DUMMY BODY */ };
     102
     103    template<class T>
     104    struct alloc_get_pointer<T, void_t<typename T::pointer>>
    105105        : aux::type_is<typename T::pointer>
    106106    { /* DUMMY BODY */ };
    107107
    108108    template<class T, class Ptr, class = void>
    109     struct get_const_pointer
     109    struct alloc_get_const_pointer
    110110        : aux::type_is<typename pointer_traits<Ptr>::template rebind<const typename T::value_type>>
    111111    { /* DUMMY BODY */ };
    112112
    113113    template<class T, class Ptr>
    114     struct get_const_pointer<T, Ptr, void_t<typename T::const_pointer>>
     114    struct alloc_get_const_pointer<T, Ptr, void_t<typename T::const_pointer>>
    115115        : aux::type_is<typename T::const_pointer>
    116116    { /* DUMMY BODY */ };
    117117
    118118    template<class T, class Ptr, class = void>
    119     struct get_void_pointer
     119    struct alloc_get_void_pointer
    120120        : aux::type_is<typename pointer_traits<Ptr>::template rebind<void>>
    121121    { /* DUMMY BODY */ };
    122122
    123123    template<class T, class Ptr>
    124     struct get_void_pointer<T, Ptr, void_t<typename T::void_pointer>>
     124    struct alloc_get_void_pointer<T, Ptr, void_t<typename T::void_pointer>>
    125125        : aux::type_is<typename T::void_pointer>
    126126    { /* DUMMY BODY */ };
    127127
    128128    template<class T, class Ptr, class = void>
    129     struct get_const_void_pointer
     129    struct alloc_get_const_void_pointer
    130130        : aux::type_is<typename pointer_traits<Ptr>::template rebind<const void>>
    131131    { /* DUMMY BODY */ };
    132132
    133133    template<class T, class Ptr>
    134     struct get_const_void_pointer<T, Ptr, void_t<typename T::const_void_pointer>>
     134    struct alloc_get_const_void_pointer<T, Ptr, void_t<typename T::const_void_pointer>>
    135135        : aux::type_is<typename T::const_void_pointer>
    136136    { /* DUMMY BODY */ };
    137137
    138138    template<class T, class Ptr, class = void>
    139     struct get_difference_type
     139    struct alloc_get_difference_type
    140140        : aux::type_is<typename pointer_traits<Ptr>::difference_type>
    141141    { /* DUMMY BODY */ };
    142142
    143143    template<class T, class Ptr>
    144     struct get_difference_type<T, Ptr, void_t<typename T::difference_type>>
     144    struct alloc_get_difference_type<T, Ptr, void_t<typename T::difference_type>>
    145145        : aux::type_is<typename T::difference_type>
    146146    { /* DUMMY BODY */ };
    147147
    148148    template<class T, class Difference, class = void>
    149     struct get_size_type: aux::type_is<make_unsigned_t<Difference>>
     149    struct alloc_get_size_type: aux::type_is<make_unsigned_t<Difference>>
    150150    { /* DUMMY BODY */ };
    151151
    152152    template<class T, class Difference>
    153     struct get_size_type<T, Difference, void_t<typename T::size_type>>
     153    struct alloc_get_size_type<T, Difference, void_t<typename T::size_type>>
    154154        : aux::type_is<typename T::size_type>
    155155    { /* DUMMY BODY */ };
    156156
    157157    template<class T, class = void>
    158     struct get_copy_propagate: aux::type_is<false_type>
    159     { /* DUMMY BODY */ };
    160 
    161     template<class T>
    162     struct get_copy_propagate<T, void_t<typename T::propagate_on_container_copy_assignment>>
     158    struct alloc_get_copy_propagate: aux::type_is<false_type>
     159    { /* DUMMY BODY */ };
     160
     161    template<class T>
     162    struct alloc_get_copy_propagate<T, void_t<typename T::propagate_on_container_copy_assignment>>
    163163        : aux::type_is<typename T::propagate_on_container_copy_assignment>
    164164    { /* DUMMY BODY */ };
    165165
    166166    template<class T, class = void>
    167     struct get_move_propagate: aux::type_is<false_type>
    168     { /* DUMMY BODY */ };
    169 
    170     template<class T>
    171     struct get_move_propagate<T, void_t<typename T::propagate_on_container_move_assignment>>
     167    struct alloc_get_move_propagate: aux::type_is<false_type>
     168    { /* DUMMY BODY */ };
     169
     170    template<class T>
     171    struct alloc_get_move_propagate<T, void_t<typename T::propagate_on_container_move_assignment>>
    172172        : aux::type_is<typename T::propagate_on_container_move_assignment>
    173173    { /* DUMMY BODY */ };
    174174
    175175    template<class T, class = void>
    176     struct get_swap_propagate: aux::type_is<false_type>
    177     { /* DUMMY BODY */ };
    178 
    179     template<class T>
    180     struct get_swap_propagate<T, void_t<typename T::propagate_on_container_swap>>
     176    struct alloc_get_swap_propagate: aux::type_is<false_type>
     177    { /* DUMMY BODY */ };
     178
     179    template<class T>
     180    struct alloc_get_swap_propagate<T, void_t<typename T::propagate_on_container_swap>>
    181181        : aux::type_is<typename T::propagate_on_container_swap>
    182182    { /* DUMMY BODY */ };
    183183
    184184    template<class T, class = void>
    185     struct get_always_equal: aux::type_is<typename is_empty<T>::type>
    186     { /* DUMMY BODY */ };
    187 
    188     template<class T>
    189     struct get_always_equal<T, void_t<typename T::is_always_equal>>
     185    struct alloc_get_always_equal: aux::type_is<typename is_empty<T>::type>
     186    { /* DUMMY BODY */ };
     187
     188    template<class T>
     189    struct alloc_get_always_equal<T, void_t<typename T::is_always_equal>>
    190190        : aux::type_is<typename T::is_always_equal>
    191191    { /* DUMMY BODY */ };
    192192
    193193    template<class Alloc, class T, class = void>
    194     struct get_rebind_other
     194    struct alloc_get_rebind_alloc
    195195    { /* DUMMY BODY */ };
    196196
    197197    template<class Alloc, class T>
    198     struct get_rebind_other<Alloc, T, void_t<typename Alloc::template rebind<T>::other>>
     198    struct alloc_get_rebind_alloc<Alloc, T, void_t<typename Alloc::template rebind<T>::other>>
    199199        : aux::type_is<typename Alloc::template rebind<T>::other>
    200200    { /* DUMMY BODY */ };
    201201
    202     /* TODO: How am I suppose to do this?!
    203     template<template<class T, class... Args> class Alloc>
    204     struct get_rebind_args;
    205     */
     202    template<template <class, class...> class Alloc, class U, class... Args, class T>
     203    struct alloc_get_rebind_alloc<Alloc<U, Args...>, T>
     204        : aux::type_is<Alloc<T, Args...>>
     205    { /* DUMMY BODY */ };
     206
     207    /**
     208     * These metafunctions are used to check whether an expression
     209     * is well-formed for the static functions of allocator_traits:
     210     */
     211
     212    template<class Alloc, class Size, class ConstVoidPointer, class = void>
     213    struct alloc_has_hint_allocate: false_type
     214    { /* DUMMY BODY */ };
     215
     216    template<class Alloc, class Size, class ConstVoidPointer>
     217    struct alloc_has_hint_allocate<
     218        Alloc, Size, ConstVoidPointer, void_t<
     219            decltype(declval<Alloc>().alloc(declval<Size>(), declval<ConstVoidPointer>()))
     220        >
     221    >: true_type
     222    { /* DUMMY BODY */ };
     223
     224    template<class, class Alloc, class T, class... Args>
     225    struct alloc_has_construct_impl: false_type
     226    { /* DUMMY BODY */ };
     227
     228    template<class Alloc, class T, class... Args>
     229    struct alloc_has_construct_impl<
     230        void_t<decltype(declval<Alloc>().construct(declval<T*>(), forward<Args>(declval<Args>())...))>,
     231        Alloc, T, Args...
     232    >: true_type
     233    { /* DUMMY BODY */ };
     234
     235    template<class Alloc, class T, class = void>
     236    struct alloc_has_destroy: false_type
     237    { /* DUMMY BODY */ };
    206238
    207239    template<class Alloc, class T>
    208     struct get_rebind_args: aux::type_is<typename get_rebind_other<Alloc, T>::type>
     240    struct alloc_has_destroy<Alloc, T, void_t<decltype(declval<Alloc>().destroy(declval<T>()))>>
     241        : true_type
     242    { /* DUMMY BODY */ };
     243
     244    template<class Alloc, class T, class... Args>
     245    struct alloc_has_construct
     246        : alloc_has_construct_impl<void_t<>, Alloc, T, Args...>
     247    { /* DUMMY BODY */ };
     248
     249    template<class Alloc, class = void>
     250    struct alloc_has_max_size: false_type
     251    { /* DUMMY BODY */ };
     252
     253    template<class Alloc>
     254    struct alloc_has_max_size<Alloc, void_t<decltype(declval<Alloc>().max_size())>>
     255        : true_type
     256    { /* DUMMY BODY */ };
     257
     258    template<class Alloc, class = void>
     259    struct alloc_has_select: false_type
     260    { /* DUMMY BODY */ };
     261
     262    template<class Alloc>
     263    struct alloc_has_select<
     264        Alloc, void_t<
     265            decltype(declval<Alloc>().select_on_container_copy_construction())
     266        >
     267    >: true_type
    209268    { /* DUMMY BODY */ };
    210269}
Note: See TracChangeset for help on using the changeset viewer.