mn_memory.hpp
Go to the documentation of this file.
1 
19 #ifndef __MINLIB_DEFAULT_ALLOCATOR_H__
20 #define __MINLIB_DEFAULT_ALLOCATOR_H__
21 
22 #include "mn_config.hpp"
23 
25 #include "mn_default_allocator.hpp"
26 #include "utils/mn_addressof.hpp"
27 
28 namespace mn {
29  namespace memory {
30 
31  template<class TAllocator>
32  struct pointer_traits {
33  template<class U>
34  struct rebind_to { using type = typename TAllocator::template rebind<U>; };
35  };
36  template<class T>
37  struct pointer_traits<T*> {
38  template<class U>
39  struct rebind_to { using type = U*; };
40  };
41 
42  namespace detail {
43  template<class> struct ptr_void { using type = void; };
44  template<class T> struct ptr_first;
45 
46  template<template<class, class...> class T, class U, class... Args>
47  struct ptr_first<T<U, Args...> > {
48  using type = U;
49  };
50 
51  template<class T, class = void>
52  struct ptr_element {
53  using type = typename ptr_first<T>::type;
54  };
55  template<class T>
56  struct ptr_element<T, typename ptr_void<typename T::element_type>::type> {
57  using type = typename T::element_type
58  };
59 
60  template<class, class = void>
61  struct ptr_difference { using type = mn::ptrdiff_t; }
62 
63  template<class T>
64  struct ptr_difference<T, typename ptr_void<typename T::difference_type>::type> {
65  using type = typename T::difference_type;
66  };
67 
68  template<class T, class V> struct ptr_transform;
69 
70  template<template<class, class...> class T, class U, class... Args, class V>
71  struct ptr_transform<T<U, Args...>, V> { using type = T<V, Args...>; };
72 
73  template<class T, class U, class = void>
74  struct ptr_rebind { using type = typename ptr_transform<T, U>::type; };
75 
76  template<class T, class U>
77  struct ptr_rebind<T, U, typename ptr_void<typename T::template rebind<U> >::type> {
78  using type = typename T::template rebind<U>;
79  };
80 
81  template<class T>
82  struct ptr_value { using type = T; };
83 
84  template<>
85  struct ptr_value<void> { typedef struct { } type; };
86 
87  } // detail
88 
89  template<class T>
90  struct pointer_traits {
91  using pointer = T;
94 
95  template<class U>
96  struct rebind_to {
97  using = typename detail::ptr_rebind<T, U>::type;
98  };
99 
100  template<class U>
102 
103  static T* pointer_to(typename detail::ptr_value<element_type>::type& v) noexcept {
104  return pointer::pointer_to(v);
105  }
106  };
107  template<class T>
108  struct pointer_traits<T*> {
109  using pointer = T*;
110  using element_type = T;
112 
113  template<class U>
114  struct rebind_to {
115  using = U*;
116  };
117  template<class U>
118  using rebind = U*;
119 
120  static T* pointer_to(typename detail::ptr_value<T>::type& v) noexcept {
121  return mn::addressof(v);
122  }
123  };
124  template<class T> constexpr inline T* to_address(T* v) noexcept {
125  return v;
126  }
127 
128 
129 
130  namespace detail {
131  template<class T>
132  inline T* ptr_address(T* v, int) noexcept {
133  return v;
134  }
135 
136  template<class T>
137  inline auto ptr_address(const T& v, int) noexcept -> decltype(to_address(v)) {
139  }
140  template<class T>
141  inline auto ptr_address(const T& v, long) noexcept {
142  return ptr_address(v.operator->(), 0);
143  }
144  }
145 
146  template<class T>
147  inline auto to_address(const T& v) noexcept {
148  return detail::ptr_address(v, 0);
149  }
150  }
151 }
typename ptr_first< T >::type type
Definition: mn_memory.hpp:53
T * ptr_address(T *v, int) noexcept
Definition: mn_memory.hpp:132
typename ptr_transform< T, U >::type type
Definition: mn_memory.hpp:74
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
struct mn::memory::detail::ptr_difference type
mn::ptrdiff_t type
Definition: mn_memory.hpp:61
void type
Definition: mn_memory.hpp:43
T type
Definition: mn_memory.hpp:82
T< V, Args... > type
Definition: mn_memory.hpp:71
Definition: mn_memory.hpp:61
Definition: mn_memory.hpp:52
Definition: mn_memory.hpp:44
Definition: mn_memory.hpp:74
Definition: mn_memory.hpp:68
Definition: mn_memory.hpp:82
Definition: mn_memory.hpp:43
auto to_address(const T &v) noexcept
Definition: mn_memory.hpp:147
constexpr T * to_address(T *v) noexcept
Definition: mn_memory.hpp:124
Definition: mn_allocator_typetraits.hpp:25
constexpr T * addressof(T &o) noexcept
Definition: mn_addressof.hpp:27
long ptrdiff_t
Definition: mn_def.hpp:49
T element_type
Definition: mn_memory.hpp:110
U * rebind
Definition: mn_memory.hpp:118
(typename detail::ptr_value< T >::type &v) noexcept U * pointer_to
Definition: mn_memory.hpp:115
mn::ptrdiff_t difference_type
Definition: mn_memory.hpp:111
T * pointer
Definition: mn_memory.hpp:109
U * type
Definition: mn_memory.hpp:39
Definition: mn_memory.hpp:32
(typename detail::ptr_value< element_type >::type &v) noexcept typename detail::ptr_rebind< T, U >::type pointer_to
Definition: mn_memory.hpp:97
typename detail::ptr_rebind< T, U >::type rebind
Definition: mn_memory.hpp:101
T pointer
Definition: mn_memory.hpp:91
typename TAllocator::template rebind< U > type
Definition: mn_memory.hpp:34
typename detail::ptr_element< T >::type element_type
Definition: mn_memory.hpp:92
typename detail::ptr_difference< T >::type difference_type
Definition: mn_memory.hpp:93
Definition: mn_memory.hpp:34