source: mainline/uspace/lib/cpp/include/__bits/memory/misc.hpp@ 57264ac3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 57264ac3 was 7bbf91e, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: changed internal to bits to avoid include space pollusion, fixed old std::hel:: bugs in files that weren't touched since

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_BITS_MEMORY_MISC
30#define LIBCPP_BITS_MEMORY_MISC
31
32#include <cstdlib>
33#include <iterator>
34#include <new>
35#include <utility>
36
37namespace std
38{
39 /**
40 * 20.7.10, raw storage iterator:
41 */
42
43 template<class OutputIterator, class T>
44 class raw_storage_iterator: public iterator<output_iterator_tag, void, void, void, void>
45 {
46 public:
47 explicit raw_storage_iterator(OutputIterator it)
48 : it_{it}
49 { /* DUMMY BODY */ }
50
51 raw_storage_iterator& operator*()
52 {
53 return *this;
54 }
55
56 raw_storage_iterator& operator=(const T& element)
57 {
58 new(it_) T{element};
59
60 return *this;
61 }
62
63 raw_storage_iterator& operator++()
64 {
65 ++it_;
66
67 return *this;
68 }
69
70 raw_storage_iterator operator++(int)
71 {
72 return raw_storage_iterator{it_++};
73 }
74
75 private:
76 OutputIterator it_;
77 };
78
79 /**
80 * 20.7.11, temporary buffers:
81 */
82
83 template<class T>
84 pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept
85 {
86 T* res{};
87
88 while (n > 0)
89 {
90 res = (T*)malloc(n * sizeof(T));
91
92 if (res)
93 return make_pair(res, n);
94
95 --n;
96 }
97
98 return make_pair(nullptr, ptrdiff_t{});
99 }
100
101 template<class T>
102 void return_temporary_buffer(T* ptr)
103 {
104 free(ptr);
105 }
106
107 /**
108 * 20.7.12, specialized algorithms:
109 */
110
111 template<class Iterator>
112 struct iterator_traits;
113
114 template<class InputIterator, class ForwardIterator>
115 ForwardIterator unitialized_copy(
116 InputIterator first, InputIterator last,
117 ForwardIterator result
118 )
119 {
120 for (; first != last; ++first, ++result)
121 ::new (static_cast<void*>(&*result)) typename iterator_traits<ForwardIterator>::value_type(*first);
122
123 return result;
124 }
125
126 template<class InputIterator, class Size, class ForwardIterator>
127 ForwardIterator unitialized_copy_n(
128 InputIterator first, Size n, ForwardIterator result
129 )
130 {
131 for (; n > 0; ++first, --n, ++result)
132 ::new (static_cast<void*>(&*result)) typename iterator_traits<ForwardIterator>::value_type(*first);
133
134 return result;
135 }
136
137 template<class ForwardIterator, class T>
138 void unitialized_fill(
139 ForwardIterator first, ForwardIterator last, const T& x
140 )
141 {
142 for (; first != last; ++first)
143 ::new (static_cast<void*>(&*first)) typename iterator_traits<ForwardIterator>::value_type(x);
144 }
145
146 template<class ForwardIterator, class Size, class T>
147 ForwardIterator unitialized_fill_n(
148 ForwardIterator first, Size n, const T& x
149 )
150 {
151 for (; n > 0; ++first, --n)
152 ::new (static_cast<void*>(&*first)) typename iterator_traits<ForwardIterator>::value_type(x);
153
154 return first;
155 }
156}
157
158#endif
Note: See TracBrowser for help on using the repository browser.