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

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

cpp: moved the implementation of the system headers to .hpp files in the impl directory, this way we get to use C++ aware tools such as syntax higlighting and error checking during development

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