[b1cd380c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 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_UTILITY
|
---|
| 30 | #define LIBCPP_UTILITY
|
---|
| 31 |
|
---|
[2084bfcd] | 32 | #include <type_traits>
|
---|
| 33 |
|
---|
[b1cd380c] | 34 | namespace std
|
---|
| 35 | {
|
---|
| 36 | /**
|
---|
| 37 | * 20.2.1, operators:
|
---|
| 38 | */
|
---|
[aab972f] | 39 |
|
---|
[f041811] | 40 | namespace rel_ops
|
---|
[b1cd380c] | 41 | {
|
---|
[f041811] | 42 | template<typename T>
|
---|
| 43 | bool operator!=(const T& lhs, const T& rhs)
|
---|
| 44 | {
|
---|
| 45 | return !(lhs == rhs);
|
---|
| 46 | }
|
---|
[b1cd380c] | 47 |
|
---|
[f041811] | 48 | template<typename T>
|
---|
| 49 | bool operator>(const T& lhs, const T& rhs)
|
---|
| 50 | {
|
---|
| 51 | return (rhs < lhs);
|
---|
| 52 | }
|
---|
[b1cd380c] | 53 |
|
---|
[f041811] | 54 | template<typename T>
|
---|
| 55 | bool operator<=(const T& lhs, const T& rhs)
|
---|
| 56 | {
|
---|
| 57 | return !(rhs < lhs);
|
---|
| 58 | }
|
---|
[b1cd380c] | 59 |
|
---|
[f041811] | 60 | template<typename T>
|
---|
| 61 | bool operator>=(const T& lhs, const T& rhs)
|
---|
| 62 | {
|
---|
| 63 | return !(lhs < rhs);
|
---|
| 64 | }
|
---|
[b1cd380c] | 65 | }
|
---|
| 66 |
|
---|
[add816c7] | 67 | /**
|
---|
| 68 | * 20.2.4, forward/move helpers:
|
---|
| 69 | */
|
---|
| 70 |
|
---|
| 71 | template<class T>
|
---|
| 72 | inline constexpr T&& forward(remove_reference_t<T>& t) noexcept
|
---|
| 73 | {
|
---|
| 74 | return static_cast<T&&>(t);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | template<class T>
|
---|
| 78 | inline constexpr T&& forward(remove_reference_t<T>&& t) noexcept
|
---|
| 79 | {
|
---|
| 80 | // TODO: check if t is lvalue reference, if it is, the program
|
---|
| 81 | // is ill-formed according to the standard
|
---|
| 82 | return static_cast<T&&>(t);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | template<class T>
|
---|
| 86 | inline constexpr remove_reference_t<T>&& move(T&& t) noexcept
|
---|
| 87 | {
|
---|
| 88 | return static_cast<remove_reference_t<T>&&>(t);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[5abc7fd] | 91 | /**
|
---|
| 92 | * 20.2.2, swap:
|
---|
| 93 | */
|
---|
| 94 |
|
---|
| 95 | template<class T>
|
---|
| 96 | void swap(T& x, T& y)
|
---|
[18944e0] | 97 | /* noexcept(is_nothrow_move_constructible<T>::value && */
|
---|
| 98 | /* is_nothrow_move_assignable<T>::value) */
|
---|
[5abc7fd] | 99 | {
|
---|
[add816c7] | 100 | T tmp{move(x)};
|
---|
| 101 | x = move(y);
|
---|
| 102 | y = move(tmp);
|
---|
[5abc7fd] | 103 | }
|
---|
| 104 |
|
---|
| 105 | template<class T, size_t N>
|
---|
| 106 | void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)))
|
---|
| 107 | {
|
---|
| 108 | // TODO: Use swap_ranges(a, a + N, b); when implemented.
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * 20.2.3, exchange:
|
---|
| 113 | */
|
---|
| 114 |
|
---|
| 115 | template<class T, class U = T>
|
---|
| 116 | T exchange(T& obj, U&& new_val)
|
---|
| 117 | {
|
---|
[add816c7] | 118 | T old_val = move(obj);
|
---|
| 119 | obj = forward<U>(new_val);
|
---|
[5abc7fd] | 120 |
|
---|
| 121 | return old_val;
|
---|
| 122 | }
|
---|
[b1cd380c] | 123 |
|
---|
| 124 | /**
|
---|
| 125 | * 20.2.5, function template declval:
|
---|
| 126 | * Note: This function only needs declaration, not
|
---|
| 127 | * implementation.
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | template<class T>
|
---|
| 131 | add_rvalue_reference_t<T> declval() noexcept;
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * 20.3, pairs:
|
---|
| 135 | */
|
---|
| 136 |
|
---|
| 137 | struct piecewise_construct_t
|
---|
| 138 | {
|
---|
| 139 | explicit piecewise_construct_t() = default;
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | template<typename T1, typename T2>
|
---|
| 143 | struct pair
|
---|
| 144 | {
|
---|
| 145 | using first_type = T1;
|
---|
| 146 | using second_type = T2;
|
---|
| 147 |
|
---|
| 148 | T1 first;
|
---|
| 149 | T2 second;
|
---|
| 150 |
|
---|
| 151 | pair(const pair&) = default;
|
---|
| 152 | pair(pair&&) = default;
|
---|
| 153 |
|
---|
| 154 | constexpr pair()
|
---|
| 155 | : first{}, second{}
|
---|
| 156 | { /* DUMMY BODY */ }
|
---|
| 157 |
|
---|
| 158 | constexpr pair(const T1& x, const T2& y)
|
---|
| 159 | : first{x}, second{y}
|
---|
| 160 | { /* DUMMY BODY */ }
|
---|
| 161 |
|
---|
| 162 | template<typename U, typename V>
|
---|
| 163 | constexpr pair(U&& x, V&& y)
|
---|
| 164 | : first(x), second(y)
|
---|
| 165 | { /* DUMMY BODY */ }
|
---|
| 166 |
|
---|
| 167 | template<typename U, typename V>
|
---|
| 168 | constexpr pair(const pair<U, V>& other)
|
---|
| 169 | : first(other.first), second(other.second)
|
---|
| 170 | { /* DUMMY BODY */ }
|
---|
| 171 |
|
---|
| 172 | template<typename U, typename V>
|
---|
| 173 | constexpr pair(pair<U, V>&& other)
|
---|
| 174 | : first(forward<first_type>(other.first)),
|
---|
| 175 | second(forward<second_type>(other.second))
|
---|
| 176 | { /* DUMMY BODY */ }
|
---|
| 177 |
|
---|
| 178 | /* TODO: need tuple, piecewise_construct_t
|
---|
| 179 | template<class... Args1, class... Args2>
|
---|
| 180 | pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args)
|
---|
| 181 | {
|
---|
| 182 | // TODO:
|
---|
| 183 | }
|
---|
| 184 | */
|
---|
| 185 |
|
---|
| 186 | pair& operator=(const pair& other)
|
---|
| 187 | {
|
---|
| 188 | first = other.first;
|
---|
| 189 | second = other.second;
|
---|
| 190 |
|
---|
| 191 | return *this;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | template<typename U, typename V>
|
---|
| 195 | pair& operator=(const pair<U, V>& other)
|
---|
| 196 | {
|
---|
| 197 | first = other.first;
|
---|
| 198 | second = other.second;
|
---|
| 199 |
|
---|
| 200 | return *this;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | pair& operator=(pair&& other) noexcept
|
---|
| 204 | {
|
---|
| 205 | first = forward<first_type>(other.first);
|
---|
| 206 | second = forward<second_type>(other.second);
|
---|
| 207 |
|
---|
| 208 | return *this;
|
---|
| 209 | }
|
---|
| 210 | };
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | #endif
|
---|