-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocator.hpp
64 lines (54 loc) · 2.51 KB
/
allocator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocator.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nmbabazi <nmbabazi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/27 11:20:28 by nmbabazi #+# #+# */
/* Updated: 2021/04/16 09:36:16 by nmbabazi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOCATOR_HPP
# define ALLOCATOR_HPP
#include <memory>
#include <new>
#include <iostream>
namespace ft
{
template <typename T>
class Allocator
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef ptrdiff_t difference_type;
typedef size_t size_type;
Allocator() throw(){}
Allocator (const Allocator& alloc) throw(){(void)alloc;}
template <class U>
Allocator (const Allocator<U>& alloc) throw(){(void)alloc;}
~Allocator () throw(){}
pointer address(reference x ) const{ return &x;}
const_pointer address(const_reference x ) const{ return &x;}
size_type max_size() const { return static_cast<size_type>(-1) / sizeof(value_type);}
pointer allocate(size_type n)
{
pointer ret;
if (!(ret = reinterpret_cast<pointer>(::operator new(n * (sizeof(value_type))))))
throw std::bad_alloc();
return ret;
}
void deallocate (pointer p, size_type n)
{
(void)n;
operator delete(p);
}
void construct ( pointer p, const_reference val ){new((void *)p) T(val);} //appelle au constructeur de T avec pour arg val
void destroy (pointer p){((T*)p)->~T();} //appelle au destructeur de T
};
}
#endif