mirror of https://github.com/ARMmbed/mbed-os.git
Add C++17 uninitialized storage ops
parent
f27c7ecb64
commit
85cd3cd9cc
|
@ -23,12 +23,19 @@
|
|||
* - For ARM C 5, C++11/14 features:
|
||||
* - std::align
|
||||
* - std::addressof
|
||||
* - std::uninitialized_copy_n
|
||||
* - std::unique_ptr, std::make_unique, std::default_delete
|
||||
* - For all toolchains, C++17 backports:
|
||||
* - mstd::uninitialized_default_construct, mstd::uninitialized_value_construct
|
||||
* - mstd::uninitialized_move, mstd::uninitialized_move_n
|
||||
* - mstd::destroy_at, mstd::destroy, mstd::destroy_n
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <mstd_type_traits>
|
||||
#include <mstd_utility> // std::pair
|
||||
#include <mstd_iterator> // std::iterator_traits
|
||||
|
||||
#ifdef __CC_ARM
|
||||
|
||||
|
@ -60,6 +67,19 @@ T *addressof(T &arg) noexcept
|
|||
return reinterpret_cast<T *>(const_cast<char *>(&reinterpret_cast<const volatile char &>(arg)));
|
||||
}
|
||||
|
||||
// [uninitialized.copy] - ARMCC has pre-C++11 uninitialized_copy
|
||||
template <class InputIterator, class Size, class ForwardIterator>
|
||||
ForwardIterator uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result) {
|
||||
for ( ; n > 0; ++result, (void) ++first, --n) {
|
||||
::new (static_cast<void*>(addressof(*result)))
|
||||
typename std::iterator_traits<ForwardIterator>::value_type(*first);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// [uninitialized.fill] - ARMCC has pre-C++11 uninitialized_fill and uninitialized_fill_n
|
||||
|
||||
// [unique.ptr]
|
||||
namespace impl
|
||||
{
|
||||
|
@ -496,12 +516,97 @@ bool operator>=(nullptr_t, const unique_ptr<T, D> &x) noexcept
|
|||
#endif // __CC_ARM
|
||||
|
||||
namespace mstd {
|
||||
using std::align;
|
||||
using std::allocator;
|
||||
using std::addressof;
|
||||
|
||||
// [uninitialized.construct.default] (C++17)
|
||||
template <class ForwardIterator, class Size>
|
||||
void uninitialized_default_construct(ForwardIterator first, ForwardIterator last) {
|
||||
for (; first != last; ++first) {
|
||||
::new (static_cast<void*>(addressof(*first)))
|
||||
typename std::iterator_traits<ForwardIterator>::value_type;
|
||||
}
|
||||
}
|
||||
|
||||
template <class ForwardIterator, class Size>
|
||||
ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n) {
|
||||
for (; n; ++first, --n) {
|
||||
::new (static_cast<void*>(addressof(*first)))
|
||||
typename std::iterator_traits<ForwardIterator>::value_type;
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
// [uninitialized.construct.value] (C++17)
|
||||
template <class ForwardIterator, class Size>
|
||||
void uninitialized_value_construct(ForwardIterator first, ForwardIterator last) {
|
||||
for (; first != last; ++first) {
|
||||
::new (static_cast<void*>(addressof(*first)))
|
||||
typename std::iterator_traits<ForwardIterator>::value_type();
|
||||
}
|
||||
}
|
||||
|
||||
template <class ForwardIterator, class Size>
|
||||
ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n) {
|
||||
for (; n; ++first, --n) {
|
||||
::new (static_cast<void*>(addressof(*first)))
|
||||
typename std::iterator_traits<ForwardIterator>::value_type();
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
// [uninitialized.move] (C++17)
|
||||
template <class InputIterator, class ForwardIterator>
|
||||
ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result) {
|
||||
for (; first != last; ++result, (void) ++first) {
|
||||
::new (static_cast<void*>(addressof(*result)))
|
||||
typename std::iterator_traits<ForwardIterator>::value_type(move(*first));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class InputIterator, class Size, class ForwardIterator>
|
||||
std::pair<InputIterator, ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result) {
|
||||
for ( ; n > 0; ++result, (void) ++first, --n) {
|
||||
::new (static_cast<void*>(addressof(*result)))
|
||||
typename std::iterator_traits<ForwardIterator>::value_type(std::move(*first));
|
||||
}
|
||||
|
||||
return { first, result };
|
||||
}
|
||||
|
||||
using std::uninitialized_copy;
|
||||
using std::uninitialized_copy_n;
|
||||
using std::uninitialized_fill;
|
||||
using std::uninitialized_fill_n;
|
||||
using std::addressof;
|
||||
using std::align;
|
||||
|
||||
// [specialized.destroy] (C++17)
|
||||
template <class T>
|
||||
void destroy_at(T *location)
|
||||
{
|
||||
location->~T();
|
||||
}
|
||||
|
||||
template <class ForwardIterator>
|
||||
void destroy(ForwardIterator first, ForwardIterator last)
|
||||
{
|
||||
for (; first != last; ++first) {
|
||||
destroy_at(addressof(*first));
|
||||
}
|
||||
}
|
||||
|
||||
template <class ForwardIterator, class Size>
|
||||
ForwardIterator destroy_n(ForwardIterator first, Size n)
|
||||
{
|
||||
for (; n > 0; (void)++first, --n) {
|
||||
destroy_at(addressof(*first));
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
using std::default_delete;
|
||||
using std::unique_ptr;
|
||||
|
|
Loading…
Reference in New Issue