source: mainline/uspace/lib/cpp/include/__bits/memory/misc.hpp@ 8fd0675f

Last change on this file since 8fd0675f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef LIBCPP_BITS_MEMORY_MISC
8#define LIBCPP_BITS_MEMORY_MISC
9
10#include <cstdlib>
11#include <iterator>
12#include <new>
13#include <utility>
14
15namespace std
16{
17 /**
18 * 20.7.10, raw storage iterator:
19 */
20
21 template<class OutputIterator, class T>
22 class raw_storage_iterator: public iterator<output_iterator_tag, void, void, void, void>
23 {
24 public:
25 explicit raw_storage_iterator(OutputIterator it)
26 : it_{it}
27 { /* DUMMY BODY */ }
28
29 raw_storage_iterator& operator*()
30 {
31 return *this;
32 }
33
34 raw_storage_iterator& operator=(const T& element)
35 {
36 new(it_) T{element};
37
38 return *this;
39 }
40
41 raw_storage_iterator& operator++()
42 {
43 ++it_;
44
45 return *this;
46 }
47
48 raw_storage_iterator operator++(int)
49 {
50 return raw_storage_iterator{it_++};
51 }
52
53 private:
54 OutputIterator it_;
55 };
56
57 /**
58 * 20.7.11, temporary buffers:
59 */
60
61 template<class T>
62 pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept
63 {
64 T* res{};
65
66 while (n > 0)
67 {
68 res = (T*)malloc(n * sizeof(T));
69
70 if (res)
71 return make_pair(res, n);
72
73 --n;
74 }
75
76 return make_pair(nullptr, ptrdiff_t{});
77 }
78
79 template<class T>
80 void return_temporary_buffer(T* ptr)
81 {
82 free(ptr);
83 }
84
85 /**
86 * 20.7.12, specialized algorithms:
87 */
88
89 template<class Iterator>
90 struct iterator_traits;
91
92 template<class InputIterator, class ForwardIterator>
93 ForwardIterator unitialized_copy(
94 InputIterator first, InputIterator last,
95 ForwardIterator result
96 )
97 {
98 for (; first != last; ++first, ++result)
99 ::new (static_cast<void*>(&*result)) typename iterator_traits<ForwardIterator>::value_type(*first);
100
101 return result;
102 }
103
104 template<class InputIterator, class Size, class ForwardIterator>
105 ForwardIterator unitialized_copy_n(
106 InputIterator first, Size n, ForwardIterator result
107 )
108 {
109 for (; n > 0; ++first, --n, ++result)
110 ::new (static_cast<void*>(&*result)) typename iterator_traits<ForwardIterator>::value_type(*first);
111
112 return result;
113 }
114
115 template<class ForwardIterator, class T>
116 void unitialized_fill(
117 ForwardIterator first, ForwardIterator last, const T& x
118 )
119 {
120 for (; first != last; ++first)
121 ::new (static_cast<void*>(&*first)) typename iterator_traits<ForwardIterator>::value_type(x);
122 }
123
124 template<class ForwardIterator, class Size, class T>
125 ForwardIterator unitialized_fill_n(
126 ForwardIterator first, Size n, const T& x
127 )
128 {
129 for (; n > 0; ++first, --n)
130 ::new (static_cast<void*>(&*first)) typename iterator_traits<ForwardIterator>::value_type(x);
131
132 return first;
133 }
134}
135
136#endif
Note: See TracBrowser for help on using the repository browser.