-
Notifications
You must be signed in to change notification settings - Fork 68
/
temporary_vector.hpp
171 lines (145 loc) · 4.25 KB
/
temporary_vector.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// cmcstl2 - A concept-enabled C++ standard library
//
// Copyright Casey Carter 2015
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/caseycarter/cmcstl2
//
#ifndef STL2_DETAIL_TEMPORARY_VECTOR_HPP
#define STL2_DETAIL_TEMPORARY_VECTOR_HPP
#include <memory>
#include <stl2/type_traits.hpp>
#include <stl2/utility.hpp>
#include <stl2/detail/construct_destruct.hpp>
#include <stl2/detail/fwd.hpp>
#include <stl2/detail/algorithm/for_each.hpp>
#include <stl2/detail/concepts/object.hpp>
STL2_OPEN_NAMESPACE {
namespace detail {
struct temporary_buffer_deleter {
template<class T>
void operator()(T* ptr) const {
std::return_temporary_buffer(ptr);
}
};
template<class T>
class temporary_buffer {
std::unique_ptr<T, temporary_buffer_deleter> alloc_;
std::ptrdiff_t size_ = 0;
temporary_buffer(std::pair<T*, std::ptrdiff_t> buf)
: alloc_{buf.first}, size_{buf.second} {}
public:
temporary_buffer() = default;
temporary_buffer(std::ptrdiff_t n)
: temporary_buffer(std::get_temporary_buffer<T>(n)) {}
T* data() const {
return alloc_.get();
}
std::ptrdiff_t size() const {
return size_;
}
};
template<class T>
requires (alignof(T) > alignof(std::max_align_t))
class temporary_buffer<T> {
std::unique_ptr<unsigned char, temporary_buffer_deleter> alloc_;
T* aligned_ = nullptr;
std::ptrdiff_t size_ = 0;
static_assert((alignof(T) & (alignof(T) - 1)) == 0,
"Alignment must be a power of two.");
temporary_buffer(std::pair<unsigned char*, std::ptrdiff_t> buf)
: alloc_{buf.first}
{
if (buf.second > 0 && static_cast<std::size_t>(buf.second) >= sizeof(T)) {
void* ptr = buf.first;
std::size_t n = buf.second;
aligned_ = static_cast<T*>(std::align(alignof(T), sizeof(T), ptr, n));
if (aligned_) {
size_ = n / sizeof(T);
}
}
}
public:
temporary_buffer() = default;
temporary_buffer(std::ptrdiff_t n)
: temporary_buffer(std::get_temporary_buffer<unsigned char>(
n * sizeof(T) + alignof(T) - 1))
{}
T* data() const {
return aligned_;
}
std::ptrdiff_t size() const {
return size_;
}
};
template<ext::destructible_object T>
class temporary_vector {
T* begin_ = nullptr;
T* end_ = nullptr;
T* alloc_ = nullptr;
void destroy() noexcept {
for_each(begin_, end_, destruct);
}
public:
using value_type = T;
~temporary_vector() {
destroy();
}
temporary_vector() = default;
temporary_vector(temporary_buffer<T>& buf)
: begin_{buf.data()}, end_{begin_}
, alloc_{begin_ + buf.size()}
{}
temporary_vector(temporary_vector&&) = delete;
temporary_vector& operator=(temporary_vector&& that) = delete;
void clear() noexcept {
destroy();
end_ = begin_;
}
constexpr bool empty() const noexcept {
return begin_ == end_;
}
constexpr std::ptrdiff_t capacity() const noexcept {
return alloc_ - begin_;
}
constexpr std::ptrdiff_t size() const noexcept {
return end_ - begin_;
}
constexpr T* begin() noexcept { return begin_; }
constexpr T* end() noexcept { return end_; }
constexpr const T* begin() const noexcept { return begin_; }
constexpr const T* end() const noexcept { return end_; }
constexpr T& operator[](std::ptrdiff_t i) noexcept {
STL2_EXPECT(0 <= i);
STL2_EXPECT(i < end_ - begin_);
return begin_[i];
}
template<class... Args>
requires constructible_from<T, Args...>
void emplace_back(Args&&... args)
noexcept(std::is_nothrow_constructible<T, Args...>::value)
{
STL2_EXPECT(end_ < alloc_);
detail::construct(*end_, std::forward<Args>(args)...);
++end_;
}
void push_back(const T& t)
noexcept(std::is_nothrow_copy_constructible<T>::value)
requires copy_constructible<T>
{ emplace_back(t); }
void push_back(T&& t)
noexcept(std::is_nothrow_move_constructible<T>::value)
requires move_constructible<T>
{ emplace_back(std::move(t)); }
};
template<ext::destructible_object T>
temporary_vector<T> make_temporary_vector(temporary_buffer<T>& buf) {
return {buf};
}
}
} STL2_CLOSE_NAMESPACE
#endif