source: mainline/uspace/lib/cpp/include/utility@ bc7ec7c

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

cpp: partially implemented the utility header

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