-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCustomSTLAllocatorTest.cpp
147 lines (127 loc) · 4.15 KB
/
CustomSTLAllocatorTest.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cstdint>
#include <string>
#include <iostream>
template <class T>
class myallocator
{
public:
using value_type = T;
/*
// Boilerplate the compiler will fill out for you
using pointer = value_type*;
using const_pointer = typename std::pointer_traits<pointer>::template
rebind<value_type const>;
using void_pointer = typename std::pointer_traits<pointer>::template
rebind<void>;
using const_void_pointer = typename std::pointer_traits<pointer>::template
rebind<const void>;
using difference_type = typename std::pointer_traits<pointer>::difference_type;
using size_type = std::make_unsigned_t<difference_type>;
template <class U> struct rebind {typedef myallocator<U> other;};
*/
myallocator() noexcept {} // not required, unless used
template <class U> myallocator(myallocator<U> const&) noexcept {}
value_type* // Use pointer if pointer is not a value_type*
allocate(std::size_t n)
{
value_type* res = static_cast<value_type*>(::operator new (n*sizeof(value_type)));
std::cout << "myallocator(" << this << ")::allocate called " << res << std::endl;
return res;
}
void
deallocate(value_type* p, std::size_t) noexcept // Use pointer if pointer is not a value_type*
{
std::cout << "myallocator("<< this << ")::deallocate called " << p << std::endl;
::operator delete(p);
}
/*
// More boilerplate the compiler will fill out for you
value_type* allocate(std::size_t n, const_void_pointer) {
return allocate(n);
}
template <class U, class ...Args>
void construct(U* p, Args&& ...args) {
::new(p) U(std::forward<Args>(args)...);
}
template <class U>
void destroy(U* p) noexcept {
p->~U();
}
std::size_t max_size() const noexcept {
return std::numeric_limits<size_type>::max();
}
myallocator select_on_container_copy_construction() const {
return *this;
}
using propagate_on_container_copy_assignment = std::false_type;
using propagate_on_container_move_assignment = std::false_type;
using propagate_on_container_swap = std::false_type;
using is_always_equal = std::is_empty<myallocator>;
*/
private:
};
template <class T, class U>
bool
operator==(myallocator<T> const&, myallocator<U> const&) noexcept
{
return true;
}
template <class T, class U>
bool
operator!=(myallocator<T> const& x, myallocator<U> const& y) noexcept
{
return !(x == y);
}
class Test {
public:
Test() {
std::cout << "Trivial constructor called " << this << std::endl;
}
Test( const std::string& s ) {
std::cout << "String constructor called " << this << std::endl;
val = s;
}
template< unsigned N >
Test( const char (&s)[N] ) {
std::cout << "Const char reference constructor called "
<< this << std::endl;
val = s;
}
Test( const Test& t ) {
val = t.val;
std::cout << "Copy constructor called " << this << std::endl;
}
Test( const Test&& t ) {
val = t.val;
std::cout << "Universal reference constructor called "
<< this << " " << &t << std::endl;
}
~Test() {
std::cout << "Destructor called " << this << std::endl;
}
private:
std::string val;
};
#define EXEC(x) { \
std::cout << "----> " << #x << std::endl; \
x; \
std::cout << "<--- "<< #x << std::endl; \
}
#include <vector>
template< template<typename> typename Allocator>
void test()
{
using AllocatorType = Allocator<Test>;
AllocatorType a1, a2;
using VecType = std::vector<Test,AllocatorType>;
VecType v1(a1);
VecType v2(a2);
EXEC( v1.emplace_back( "string1" ); );
EXEC( v2.emplace_back( v1.front() ); );
}
int main()
{
EXEC( test<std::allocator>(); );
EXEC( test<myallocator>(); );
return 0;
}