libstdc++
|
00001 // <experimental/memory> -*- C++ -*- 00002 00003 // Copyright (C) 2015-2016 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file experimental/memory 00026 * This is a TS C++ Library header. 00027 */ 00028 00029 // 00030 // N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2 00031 // 00032 00033 #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY 00034 #define _GLIBCXX_EXPERIMENTAL_MEMORY 1 00035 00036 #pragma GCC system_header 00037 00038 #if __cplusplus <= 201103L 00039 # include <bits/c++14_warning.h> 00040 #else 00041 00042 #include <memory> 00043 #include <type_traits> 00044 #include <utility> 00045 #include <functional> 00046 #include <experimental/bits/shared_ptr.h> 00047 00048 namespace std _GLIBCXX_VISIBILITY(default) 00049 { 00050 namespace experimental 00051 { 00052 inline namespace fundamentals_v2 00053 { 00054 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00055 00056 #define __cpp_lib_experimental_observer_ptr 201411 00057 00058 template <typename _Tp> 00059 class observer_ptr 00060 { 00061 public: 00062 // publish our template parameter and variations thereof 00063 using element_type = _Tp; 00064 using __pointer = add_pointer_t<_Tp>; // exposition-only 00065 using __reference = add_lvalue_reference_t<_Tp>; // exposition-only 00066 00067 // 3.2.2, observer_ptr constructors 00068 // default c’tor 00069 constexpr observer_ptr() noexcept 00070 : __t() 00071 { } 00072 00073 // pointer-accepting c’tors 00074 constexpr observer_ptr(nullptr_t) noexcept 00075 : __t() 00076 { } 00077 00078 constexpr explicit observer_ptr(__pointer __p) noexcept 00079 : __t(__p) 00080 { } 00081 00082 // copying c’tors (in addition to compiler-generated copy c’tor) 00083 template <typename _Up, 00084 typename = typename enable_if< 00085 is_convertible<typename add_pointer<_Up>::type, __pointer 00086 >::value 00087 >::type> 00088 constexpr observer_ptr(observer_ptr<_Up> __p) noexcept 00089 : __t(__p.get()) 00090 { 00091 } 00092 00093 // 3.2.3, observer_ptr observers 00094 constexpr __pointer 00095 get() const noexcept 00096 { 00097 return __t; 00098 } 00099 00100 constexpr __reference 00101 operator*() const 00102 { 00103 return *get(); 00104 } 00105 00106 constexpr __pointer 00107 operator->() const noexcept 00108 { 00109 return get(); 00110 } 00111 00112 constexpr explicit operator bool() const noexcept 00113 { 00114 return get() != nullptr; 00115 } 00116 00117 // 3.2.4, observer_ptr conversions 00118 constexpr explicit operator __pointer() const noexcept 00119 { 00120 return get(); 00121 } 00122 00123 // 3.2.5, observer_ptr modifiers 00124 constexpr __pointer 00125 release() noexcept 00126 { 00127 __pointer __tmp = get(); 00128 reset(); 00129 return __tmp; 00130 } 00131 00132 constexpr void 00133 reset(__pointer __p = nullptr) noexcept 00134 { 00135 __t = __p; 00136 } 00137 00138 constexpr void 00139 swap(observer_ptr& __p) noexcept 00140 { 00141 std::swap(__t, __p.__t); 00142 } 00143 00144 private: 00145 __pointer __t; 00146 }; // observer_ptr<> 00147 00148 template<typename _Tp> 00149 void 00150 swap(observer_ptr<_Tp>& __p1, observer_ptr<_Tp>& __p2) noexcept 00151 { 00152 __p1.swap(__p2); 00153 } 00154 00155 template<typename _Tp> 00156 observer_ptr<_Tp> 00157 make_observer(_Tp* __p) noexcept 00158 { 00159 return observer_ptr<_Tp>(__p); 00160 } 00161 00162 template<typename _Tp, typename _Up> 00163 bool 00164 operator==(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2) 00165 { 00166 return __p1.get() == __p2.get(); 00167 } 00168 00169 template<typename _Tp, typename _Up> 00170 bool 00171 operator!=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2) 00172 { 00173 return !(__p1 == __p2); 00174 } 00175 00176 template<typename _Tp> 00177 bool 00178 operator==(observer_ptr<_Tp> __p, nullptr_t) noexcept 00179 { 00180 return !__p; 00181 } 00182 00183 template<typename _Tp> 00184 bool 00185 operator==(nullptr_t, observer_ptr<_Tp> __p) noexcept 00186 { 00187 return !__p; 00188 } 00189 00190 template<typename _Tp> 00191 bool 00192 operator!=(observer_ptr<_Tp> __p, nullptr_t) noexcept 00193 { 00194 return bool(__p); 00195 } 00196 00197 template<typename _Tp> 00198 bool 00199 operator!=(nullptr_t, observer_ptr<_Tp> __p) noexcept 00200 { 00201 return bool(__p); 00202 } 00203 00204 template<typename _Tp, typename _Up> 00205 bool 00206 operator<(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2) 00207 { 00208 return std::less<typename common_type<typename add_pointer<_Tp>::type, 00209 typename add_pointer<_Up>::type 00210 >::type 00211 >{}(__p1.get(), __p2.get()); 00212 } 00213 00214 template<typename _Tp, typename _Up> 00215 bool 00216 operator>(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2) 00217 { 00218 return __p2 < __p1; 00219 } 00220 00221 template<typename _Tp, typename _Up> 00222 bool 00223 operator<=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2) 00224 { 00225 return !(__p2 < __p1); 00226 } 00227 00228 template<typename _Tp, typename _Up> 00229 bool 00230 operator>=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2) 00231 { 00232 return !(__p1 < __p2); 00233 } 00234 00235 _GLIBCXX_END_NAMESPACE_VERSION 00236 } // namespace fundamentals_v2 00237 } // namespace experimental 00238 00239 template <typename _Tp> 00240 struct hash<experimental::observer_ptr<_Tp>> 00241 { 00242 using result_type = size_t; 00243 using argument_type = experimental::observer_ptr<_Tp>; 00244 00245 size_t 00246 operator()(const experimental::observer_ptr<_Tp>& __t) const 00247 noexcept(noexcept(hash<typename add_pointer<_Tp>::type> {}(__t.get()))) 00248 { 00249 return hash<typename add_pointer<_Tp>::type> {}(__t.get()); 00250 } 00251 }; 00252 00253 } // namespace std 00254 00255 #endif // __cplusplus <= 201103L 00256 00257 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY