• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef NASTY_CONTAINERS_H
11 #define NASTY_CONTAINERS_H
12 
13 #include <cassert>
14 #include <vector>
15 #include <list>
16 
17 #include "test_macros.h"
18 
19 template <class T>
20 class nasty_vector
21 {
22 public:
23     typedef typename std::vector<T>                           nested_container;
24     typedef typename nested_container::value_type             value_type;
25     typedef typename nested_container::reference              reference;
26     typedef typename nested_container::const_reference        const_reference;
27     typedef typename nested_container::iterator               iterator;
28     typedef typename nested_container::const_iterator         const_iterator;
29 
30     typedef typename nested_container::size_type              size_type;
31     typedef typename nested_container::difference_type        difference_type;
32     typedef typename nested_container::pointer                pointer;
33     typedef typename nested_container::const_pointer          const_pointer;
34 
35     typedef typename nested_container::reverse_iterator       reverse_iterator;
36     typedef typename nested_container::const_reverse_iterator const_reverse_iterator;
37 
nasty_vector()38     nasty_vector() : v_() {}
nasty_vector(size_type n)39     explicit nasty_vector(size_type n) : v_(n) {}
nasty_vector(size_type n,const value_type & value)40     nasty_vector(size_type n, const value_type& value) : v_(n, value) {}
nasty_vector(InputIterator first,InputIterator last)41     template <class InputIterator> nasty_vector(InputIterator first, InputIterator last) : v_(first, last) {}
42 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
nasty_vector(std::initializer_list<value_type> il)43     nasty_vector(std::initializer_list<value_type> il) : v_(il) {}
44 #endif
~nasty_vector()45     ~nasty_vector() {}
46 
47     template <class InputIterator>
assign(InputIterator first,InputIterator last)48         void assign(InputIterator first, InputIterator last) { v_.assign(first, last); }
assign(size_type n,const value_type & u)49     void assign(size_type n, const value_type& u) { v_.assign(n, u); }
50 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
assign(std::initializer_list<value_type> il)51     void assign(std::initializer_list<value_type> il)  { v_.assign(il); }
52 #endif
53 
begin()54     iterator               begin() TEST_NOEXCEPT         { return v_.begin(); }
begin() const55     const_iterator         begin()   const TEST_NOEXCEPT { return v_.begin(); }
end()56     iterator               end() TEST_NOEXCEPT           { return v_.end(); }
end() const57     const_iterator         end()     const TEST_NOEXCEPT { return v_.end(); }
58 
rbegin()59     reverse_iterator       rbegin() TEST_NOEXCEPT        { return v_.rbegin(); }
rbegin() const60     const_reverse_iterator rbegin()  const TEST_NOEXCEPT { return v_.rbegin(); }
rend()61     reverse_iterator       rend() TEST_NOEXCEPT          { return v_.rend(); }
rend() const62     const_reverse_iterator rend()    const TEST_NOEXCEPT { return v_.rend(); }
63 
cbegin() const64     const_iterator         cbegin()  const TEST_NOEXCEPT { return v_.cbegin(); }
cend() const65     const_iterator         cend()    const TEST_NOEXCEPT { return v_.cend(); }
crbegin() const66     const_reverse_iterator crbegin() const TEST_NOEXCEPT { return v_.crbegin(); }
crend() const67     const_reverse_iterator crend()   const TEST_NOEXCEPT { return v_.crend(); }
68 
size() const69     size_type size() const TEST_NOEXCEPT      { return v_.size(); }
max_size() const70     size_type max_size() const TEST_NOEXCEPT  { return v_.max_size(); }
capacity() const71     size_type capacity() const TEST_NOEXCEPT  { return v_.capacity(); }
empty() const72     bool empty() const TEST_NOEXCEPT          { return v_.empty(); }
reserve(size_type n)73     void reserve(size_type n)             { v_.reserve(n); };
shrink_to_fit()74     void shrink_to_fit() TEST_NOEXCEPT        { v_.shrink_to_fit(); }
75 
operator [](size_type n)76     reference       operator[](size_type n)       { return v_[n]; }
operator [](size_type n) const77     const_reference operator[](size_type n) const { return v_[n]; }
at(size_type n)78     reference       at(size_type n)               { return v_.at(n); }
at(size_type n) const79     const_reference at(size_type n) const         { return v_.at(n); }
80 
front()81     reference       front()       { return v_.front(); }
front() const82     const_reference front() const { return v_.front(); }
back()83     reference       back()        { return v_.back(); }
back() const84     const_reference back() const  { return v_.back(); }
85 
data()86     value_type*       data() TEST_NOEXCEPT       { return v_.data(); }
data() const87     const value_type* data() const TEST_NOEXCEPT { return v_.data(); }
88 
push_back(const value_type & x)89     void push_back(const value_type& x)     { v_.push_back(x); }
90 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
push_back(value_type && x)91     void push_back(value_type&& x)          { v_.push_back(std::forward<value_type&&>(x)); }
92 #ifndef _LIBCPP_HAS_NO_VARIADICS
93     template <class... Args>
emplace_back(Args &&...args)94         void emplace_back(Args&&... args)   { v_.emplace_back(std::forward<Args>(args)...); }
95 #endif
96 #endif
pop_back()97     void pop_back()                         { v_.pop_back(); }
98 
99 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
100 #ifndef _LIBCPP_HAS_NO_VARIADICS
emplace(const_iterator pos,Args &&...args)101     template <class... Args> iterator emplace(const_iterator pos, Args&&... args)
102     { return v_.emplace(pos, std::forward<Args>(args)...); }
103 #endif
104 #endif
105 
insert(const_iterator pos,const value_type & x)106     iterator insert(const_iterator pos, const value_type& x) { return v_.insert(pos, x); }
107 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
insert(const_iterator pos,value_type && x)108     iterator insert(const_iterator pos, value_type&& x)      { return v_.insert(pos, std::forward<value_type>(x)); }
109 #endif
insert(const_iterator pos,size_type n,const value_type & x)110     iterator insert(const_iterator pos, size_type n, const value_type& x) { return v_.insert(pos, n, x); }
111     template <class InputIterator>
insert(const_iterator pos,InputIterator first,InputIterator last)112         iterator insert(const_iterator pos, InputIterator first, InputIterator last)
113     { return v_.insert(pos, first, last); }
114 
115 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
insert(const_iterator pos,std::initializer_list<value_type> il)116     iterator insert(const_iterator pos, std::initializer_list<value_type> il) { return v_.insert(pos, il); }
117 #endif
118 
erase(const_iterator pos)119     iterator erase(const_iterator pos)                        { return v_.erase(pos); }
erase(const_iterator first,const_iterator last)120     iterator erase(const_iterator first, const_iterator last) { return v_.erase(first, last); }
121 
clear()122     void clear() TEST_NOEXCEPT { v_.clear(); }
123 
resize(size_type sz)124     void resize(size_type sz)                      { v_.resize(sz); }
resize(size_type sz,const value_type & c)125     void resize(size_type sz, const value_type& c) { v_.resize(sz, c); }
126 
swap(nasty_vector & nv)127     void swap(nasty_vector &nv)
128 #if TEST_STD_VER > 14
129     noexcept(std::is_nothrow_swappable<nested_container>::value)
130 #elif defined(_LIBCPP_VERSION)
131     TEST_NOEXCEPT_COND(std::__is_nothrow_swappable<nested_container>::value)
132 #endif
133     { v_.swap(nv.v_); }
134 
operator &()135     nasty_vector *operator &()             { assert(false); return nullptr; }  // nasty
operator &() const136     const nasty_vector *operator &() const { assert(false); return nullptr; }  // nasty
137 
138     nested_container v_;
139 };
140 
141 template <class T>
operator ==(const nasty_vector<T> & x,const nasty_vector<T> & y)142 bool operator==(const nasty_vector<T>& x, const nasty_vector<T>& y) { return x.v_ == y.v_; }
143 
144 template <class T>
145 class nasty_list
146 {
147 public:
148 
149     typedef typename std::list<T>                             nested_container;
150     typedef typename nested_container::value_type             value_type;
151     typedef typename nested_container::reference              reference;
152     typedef typename nested_container::const_reference        const_reference;
153     typedef typename nested_container::iterator               iterator;
154     typedef typename nested_container::const_iterator         const_iterator;
155 
156     typedef typename nested_container::size_type              size_type;
157     typedef typename nested_container::difference_type        difference_type;
158     typedef typename nested_container::pointer                pointer;
159     typedef typename nested_container::const_pointer          const_pointer;
160 
161     typedef typename nested_container::reverse_iterator       reverse_iterator;
162     typedef typename nested_container::const_reverse_iterator const_reverse_iterator;
163 
nasty_list()164     nasty_list() : l_() {}
nasty_list(size_type n)165     explicit nasty_list(size_type n)  : l_(n) {}
nasty_list(size_type n,const value_type & value)166     nasty_list(size_type n, const value_type& value)  : l_(n,value) {}
167     template <class Iter>
nasty_list(Iter first,Iter last)168         nasty_list(Iter first, Iter last)  : l_(first, last) {}
169 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
nasty_list(std::initializer_list<value_type> il)170     nasty_list(std::initializer_list<value_type> il) : l_(il) {}
171 #endif
172 
~nasty_list()173     ~nasty_list() {}
174 
175 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
operator =(std::initializer_list<value_type> il)176     nasty_list& operator=(std::initializer_list<value_type> il) { l_ = il; return *this; }
177 #endif
178     template <class Iter>
assign(Iter first,Iter last)179         void assign(Iter first, Iter last) { l_.assign(first, last); }
assign(size_type n,const value_type & t)180     void assign(size_type n, const value_type& t) { l_.assign(n, t); }
181 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
assign(std::initializer_list<value_type> il)182     void assign(std::initializer_list<value_type> il) { l_.assign(il); }
183 #endif
184 
185 
begin()186     iterator               begin() TEST_NOEXCEPT         { return l_.begin(); }
begin() const187     const_iterator         begin()   const TEST_NOEXCEPT { return l_.begin(); }
end()188     iterator               end() TEST_NOEXCEPT           { return l_.end(); }
end() const189     const_iterator         end()     const TEST_NOEXCEPT { return l_.end(); }
190 
rbegin()191     reverse_iterator       rbegin() TEST_NOEXCEPT        { return l_.rbegin(); }
rbegin() const192     const_reverse_iterator rbegin()  const TEST_NOEXCEPT { return l_.rbegin(); }
rend()193     reverse_iterator       rend() TEST_NOEXCEPT          { return l_.rend(); }
rend() const194     const_reverse_iterator rend()    const TEST_NOEXCEPT { return l_.rend(); }
195 
cbegin() const196     const_iterator         cbegin()  const TEST_NOEXCEPT { return l_.cbegin(); }
cend() const197     const_iterator         cend()    const TEST_NOEXCEPT { return l_.cend(); }
crbegin() const198     const_reverse_iterator crbegin() const TEST_NOEXCEPT { return l_.crbegin(); }
crend() const199     const_reverse_iterator crend()   const TEST_NOEXCEPT { return l_.crend(); }
200 
front()201     reference       front()       { return l_.front(); }
front() const202     const_reference front() const { return l_.front(); }
back()203     reference       back()        { return l_.back(); }
back() const204     const_reference back() const  { return l_.back(); }
205 
size() const206     size_type size() const TEST_NOEXCEPT      { return l_.size(); }
max_size() const207     size_type max_size() const TEST_NOEXCEPT  { return l_.max_size(); }
empty() const208     bool empty() const TEST_NOEXCEPT          { return l_.empty(); }
209 
push_front(const value_type & x)210     void push_front(const value_type& x)    { l_.push_front(x); }
push_back(const value_type & x)211     void push_back(const value_type& x)     { l_.push_back(x); }
212 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
push_back(value_type && x)213     void push_back(value_type&& x)          { l_.push_back(std::forward<value_type&&>(x)); }
push_front(value_type && x)214     void push_front(value_type&& x)         { l_.push_back(std::forward<value_type&&>(x)); }
215 #ifndef _LIBCPP_HAS_NO_VARIADICS
216     template <class... Args>
emplace_back(Args &&...args)217         void emplace_back(Args&&... args)   { l_.emplace_back(std::forward<Args>(args)...); }
218     template <class... Args>
emplace_front(Args &&...args)219         void emplace_front(Args&&... args)  { l_.emplace_front(std::forward<Args>(args)...); }
220 #endif
221 #endif
pop_front()222     void pop_front()                        { l_.pop_front(); }
pop_back()223     void pop_back()                         { l_.pop_back(); }
224 
225 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
226 #ifndef _LIBCPP_HAS_NO_VARIADICS
emplace(const_iterator pos,Args &&...args)227     template <class... Args> iterator emplace(const_iterator pos, Args&&... args)
228     { return l_.emplace(pos, std::forward<Args>(args)...); }
229 #endif
230 #endif
231 
insert(const_iterator pos,const value_type & x)232     iterator insert(const_iterator pos, const value_type& x) { return l_.insert(pos, x); }
233 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
insert(const_iterator pos,value_type && x)234     iterator insert(const_iterator pos, value_type&& x)      { return l_.insert(pos, std::forward<value_type>(x)); }
235 #endif
insert(const_iterator pos,size_type n,const value_type & x)236     iterator insert(const_iterator pos, size_type n, const value_type& x) { return l_.insert(pos, n, x); }
237     template <class InputIterator>
insert(const_iterator pos,InputIterator first,InputIterator last)238         iterator insert(const_iterator pos, InputIterator first, InputIterator last)
239     { return l_.insert(pos, first, last); }
240 
241 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
insert(const_iterator pos,std::initializer_list<value_type> il)242     iterator insert(const_iterator pos, std::initializer_list<value_type> il) { return l_.insert(pos, il); }
243 #endif
244 
erase(const_iterator pos)245     iterator erase(const_iterator pos)                      { return l_.erase(pos); }
erase(const_iterator pos,const_iterator last)246     iterator erase(const_iterator pos, const_iterator last) { return l_.erase(pos, last); }
247 
resize(size_type)248     void resize(size_type)                      { l_.resize(); }
resize(size_type,const value_type & c)249     void resize(size_type, const value_type& c) { l_.resize(c); }
250 
swap(nasty_list & nl)251     void swap(nasty_list &nl)
252 #if TEST_STD_VER > 14
253     noexcept(std::is_nothrow_swappable<nested_container>::value)
254 #elif defined(_LIBCPP_VERSION)
255     TEST_NOEXCEPT_COND(std::__is_nothrow_swappable<nested_container>::value)
256 #endif
257     { l_.swap(nl.l_); }
258 
clear()259     void clear() TEST_NOEXCEPT { l_.clear(); }
260 
261 //     void splice(const_iterator position, list& x);
262 //     void splice(const_iterator position, list&& x);
263 //     void splice(const_iterator position, list& x, const_iterator i);
264 //     void splice(const_iterator position, list&& x, const_iterator i);
265 //     void splice(const_iterator position, list& x, const_iterator first,
266 //                                                   const_iterator last);
267 //     void splice(const_iterator position, list&& x, const_iterator first,
268 //                                                   const_iterator last);
269 //
270 //     void remove(const value_type& value);
271 //     template <class Pred> void remove_if(Pred pred);
272 //     void unique();
273 //     template <class BinaryPredicate>
274 //         void unique(BinaryPredicate binary_pred);
275 //     void merge(list& x);
276 //     void merge(list&& x);
277 //     template <class Compare>
278 //         void merge(list& x, Compare comp);
279 //     template <class Compare>
280 //         void merge(list&& x, Compare comp);
281 //     void sort();
282 //     template <class Compare>
283 //         void sort(Compare comp);
284 //     void reverse() noexcept;
285 
operator &()286     nasty_list *operator &()             { assert(false); return nullptr; }  // nasty
operator &() const287     const nasty_list *operator &() const { assert(false); return nullptr; }  // nasty
288 
289     nested_container l_;
290 };
291 
292 template <class T>
operator ==(const nasty_list<T> & x,const nasty_list<T> & y)293 bool operator==(const nasty_list<T>& x, const nasty_list<T>& y) { return x.l_ == y.l_; }
294 
295 // Not really a mutex, but can play one in tests
296 class nasty_mutex
297 {
298 public:
nasty_mutex()299      nasty_mutex() TEST_NOEXCEPT {}
~nasty_mutex()300      ~nasty_mutex() {}
301 
operator &()302 	nasty_mutex *operator& ()   { assert(false); return nullptr; }
303 	template <typename T>
operator ,(const T &)304 	void operator, (const T &) { assert(false); }
305 
306 private:
nasty_mutex(const nasty_mutex &)307     nasty_mutex(const nasty_mutex&)            { assert(false); }
operator =(const nasty_mutex &)308     nasty_mutex& operator=(const nasty_mutex&) { assert(false); return *this; }
309 
310 public:
lock()311     void lock()               {}
try_lock()312     bool try_lock() TEST_NOEXCEPT { return true; }
unlock()313     void unlock() TEST_NOEXCEPT   {}
314 
315     // Shared ownership
lock_shared()316     void lock_shared()     {}
try_lock_shared()317     bool try_lock_shared() { return true; }
unlock_shared()318     void unlock_shared()   {}
319 };
320 
321 #endif
322