source: mainline/uspace/lib/cpp/include/__bits/adt/stack.hpp@ c735afb

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

cpp: refactored the library layout, everything from the impl directory was moved to the bits directory for the sake of consistency, updated copyright notices and include guards

  • Property mode set to 100644
File size: 5.7 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_ADT_STACK
30#define LIBCPP_BITS_ADT_STACK
31
32#include <deque>
33#include <initializer_list>
34#include <utility>
35
36namespace std
37{
38 /**
39 * 23.5.6.2, stack:
40 */
41
42 template<class T, class Container = deque<T>>
43 class stack
44 {
45 public:
46 using container_type = Container;
47 using value_type = typename container_type::value_type;
48 using reference = typename container_type::reference;
49 using const_reference = typename container_type::const_reference;
50 using size_type = typename container_type::size_type;
51
52 explicit stack(container_type& cont)
53 : c{cont}
54 { /* DUMMY BODY */ }
55
56 explicit stack(container_type&& cont = container_type{})
57 : c{move(cont)}
58 { /* DUMMY BODY */ }
59
60 /**
61 * TODO: The allocator constructor should use enable_if
62 * as a last parameter that checks if uses_allocator
63 * from <memory> holds.
64 */
65 template<class Alloc>
66 explicit stack(Alloc& alloc)
67 : c{alloc}
68 { /* DUMMY BODY */ }
69
70 template<class Alloc>
71 stack(const container_type& cont, const Alloc& alloc)
72 : c{cont, alloc}
73 { /* DUMMY BODY */ }
74
75 template<class Alloc>
76 stack(container_type&& cont, const Alloc& alloc)
77 : c{move(cont), alloc}
78 { /* DUMMY BODY */ }
79
80 template<class Alloc>
81 stack(const stack& other, const Alloc& alloc)
82 : c{other.c, alloc}
83 { /* DUMMY BODY */ }
84
85 template<class Alloc>
86 stack(stack&& other, const Alloc& alloc)
87 : c{move(other.c), alloc}
88 { /* DUMMY BODY */ }
89
90 bool empty()
91 {
92 return c.empty();
93 }
94
95 size_type size()
96 {
97 return c.size();
98 }
99
100 reference top()
101 {
102 return c.back();
103 }
104
105 const_reference top() const
106 {
107 return c.back();
108 }
109
110 void push(const value_type& val)
111 {
112 c.push_back(val);
113 }
114
115 void push(value_type&& val)
116 {
117 c.push_back(move(val));
118 }
119
120 template<class... Args>
121 void emplace(Args&&... args)
122 {
123 c.emplace_back(forward<Args>(args)...);
124 }
125
126 void pop()
127 {
128 c.pop_back();
129 }
130
131 void swap(stack& other) // We cannot use c in the noexcept :/
132 noexcept(noexcept(declval<container_type>().swap(declval<container_type&>())))
133 {
134 std::swap(c, other.c);
135 }
136
137 protected:
138 container_type c;
139 };
140
141 /**
142 * 23.6.5.5, stack operators:
143 */
144
145 template<class T, class Container>
146 bool operator==(const stack<T, Container>& lhs,
147 const stack<T, Container>& rhs)
148 {
149 return lhs.c == rhs.c;
150 }
151
152 template<class T, class Container>
153 bool operator!=(const stack<T, Container>& lhs,
154 const stack<T, Container>& rhs)
155 {
156 return lhs.c != rhs.c;
157 }
158
159 template<class T, class Container>
160 bool operator<(const stack<T, Container>& lhs,
161 const stack<T, Container>& rhs)
162 {
163 return lhs.c < rhs.c;
164 }
165
166 template<class T, class Container>
167 bool operator<=(const stack<T, Container>& lhs,
168 const stack<T, Container>& rhs)
169 {
170 return lhs.c <= rhs.c;
171 }
172
173 template<class T, class Container>
174 bool operator>(const stack<T, Container>& lhs,
175 const stack<T, Container>& rhs)
176 {
177 return lhs.c > rhs.c;
178 }
179
180 template<class T, class Container>
181 bool operator>=(const stack<T, Container>& lhs,
182 const stack<T, Container>& rhs)
183 {
184 return lhs.c >= rhs.c;
185 }
186
187 /**
188 * 23.6.5.6, stack specialized algorithms:
189 */
190
191 template<class T, class Container>
192 void swap(stack<T, Container>& lhs, stack<T, Container>& rhs)
193 noexcept(noexcept(lhs.swap(rhs)))
194 {
195 lhs.swap(rhs);
196 }
197}
198
199#endif
Note: See TracBrowser for help on using the repository browser.