source: mainline/uspace/lib/cpp/include/impl/utility.hpp@ 5fb070d

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

cpp: moved utility to impl/utility.hpp

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
32#include <type_traits>
33
34namespace std
35{
36 /**
37 * 20.2.1, operators:
38 */
39 template<typename T>
40 bool operator!=(const T& lhs, const T& rhs)
41 {
42 return !(lhs == rhs);
43 }
44
45 template<typename T>
46 bool operator>(const T& lhs, const T& rhs)
47 {
48 return (rhs < lhs);
49 }
50
51 template<typename T>
52 bool operator<=(const T& lhs, const T& rhs)
53 {
54 return !(rhs < lhs);
55 }
56
57 template<typename T>
58 bool operator>=(const T& lhs, const T& rhs)
59 {
60 return !(lhs < rhs);
61 }
62
63 // TODO: swap
64 // TODO: exchange
65
66 /**
67 * 20.2.4, forward/move helpers:
68 */
69
70 template<class T>
71 inline constexpr T&& forward(remove_reference_t<T>& t) noexcept
72 {
73 return static_cast<T&&>(t);
74 }
75
76 template<class T>
77 inline constexpr T&& forward(remove_reference_t<T>&& t) noexcept
78 {
79 // TODO: check if t is lvalue reference, if it is, the program
80 // is ill-formed according to the standard
81 return static_cast<T&&>(t);
82 }
83
84 template<class T>
85 inline constexpr remove_reference_t<T>&& move(T&& t) noexcept
86 {
87 return static_cast<remove_reference_t<T>&&>(t);
88 }
89
90 /**
91 * 20.2.5, function template declval:
92 * Note: This function only needs declaration, not
93 * implementation.
94 */
95
96 template<class T>
97 add_rvalue_reference_t<T> declval() noexcept;
98
99 /**
100 * 20.3, pairs:
101 */
102
103
104 struct piecewise_construct_t
105 {
106 explicit piecewise_construct_t() = default;
107 };
108
109 template<typename T1, typename T2>
110 struct pair
111 {
112 using first_type = T1;
113 using second_type = T2;
114
115 T1 first;
116 T2 second;
117
118 pair(const pair&) = default;
119 pair(pair&&) = default;
120
121 constexpr pair()
122 : first{}, second{}
123 { /* DUMMY BODY */ }
124
125 constexpr pair(const T1& x, const T2& y)
126 : first{x}, second{y}
127 { /* DUMMY BODY */ }
128
129 template<typename U, typename V>
130 constexpr pair(U&& x, V&& y)
131 : first(x), second(y)
132 { /* DUMMY BODY */ }
133
134 template<typename U, typename V>
135 constexpr pair(const pair<U, V>& other)
136 : first(other.first), second(other.second)
137 { /* DUMMY BODY */ }
138
139 template<typename U, typename V>
140 constexpr pair(pair<U, V>&& other)
141 : first(forward<first_type>(other.first)),
142 second(forward<second_type>(other.second))
143 { /* DUMMY BODY */ }
144
145 /* TODO: need tuple, piecewise_construct_t
146 template<class... Args1, class... Args2>
147 pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args)
148 {
149 // TODO:
150 }
151 */
152
153 pair& operator=(const pair& other)
154 {
155 first = other.first;
156 second = other.second;
157
158 return *this;
159 }
160
161 template<typename U, typename V>
162 pair& operator=(const pair<U, V>& other)
163 {
164 first = other.first;
165 second = other.second;
166
167 return *this;
168 }
169
170 pair& operator=(pair&& other) noexcept
171 {
172 first = forward<first_type>(other.first);
173 second = forward<second_type>(other.second);
174
175 return *this;
176 }
177 };
178}
179
180#endif
Note: See TracBrowser for help on using the repository browser.