emlabcpp
modern opinionated embedded C++ library
allocator.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "./memory_resource.h"
27 #include "./throw_bad_alloc.h"
28 
29 #include <functional>
30 
31 namespace emlabcpp::pmr
32 {
33 
34 template < typename T >
35 class allocator
36 {
37 public:
38  using value_type = T;
39 
40  template < typename U >
41  allocator( allocator< U > const& other )
42  : resource_( other.resource_ )
43  {
44  }
45 
47  : resource_( src )
48  {
49  }
50 
51  T* allocate( std::size_t const n )
52  {
53  void* const res = resource_.get().allocate( n * sizeof( T ), alignof( T ) );
54  if ( res == nullptr )
56  return reinterpret_cast< T* >( res );
57  }
58 
59  void deallocate( T* const p, std::size_t const n ) const
60  {
61  result const res = resource_.get().deallocate(
62  reinterpret_cast< void* >( p ), n * sizeof( T ), alignof( T ) );
63  if ( res == result::ERROR )
65  }
66 
67  friend constexpr bool operator==( allocator const& lh, allocator const& rh )
68  {
69  return lh.resource_.get().is_equal( rh.resource_.get() );
70  }
71 
73  {
74  return resource_;
75  }
76 
77  template < typename U >
78  friend class allocator;
79 
80 private:
81  std::reference_wrapper< pmr::memory_resource > resource_;
82 };
83 
84 } // namespace emlabcpp::pmr
Definition: allocator.h:36
allocator(allocator< U > const &other)
Definition: allocator.h:41
constexpr friend bool operator==(allocator const &lh, allocator const &rh)
Definition: allocator.h:67
T * allocate(std::size_t const n)
Definition: allocator.h:51
allocator(pmr::memory_resource &src)
Definition: allocator.h:46
void deallocate(T *const p, std::size_t const n) const
Definition: allocator.h:59
pmr::memory_resource & get_resource()
Definition: allocator.h:72
T value_type
Definition: allocator.h:38
Definition: memory_resource.h:33
MIT License.
Definition: aliases.h:36
void throw_bad_alloc()
Definition: throw_bad_alloc.h:32
T res
Definition: algorithm.h:505
result represents an result of some operation, as an alternative to returning just bool with true/fal...
Definition: result.h:42