Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Redistribution and use in source and binary forms, with or without | 3 // Redistribution and use in source and binary forms, with or without |
| 4 // modification, are permitted provided that the following conditions are | 4 // modification, are permitted provided that the following conditions are |
| 5 // met: | 5 // met: |
| 6 // | 6 // |
| 7 // * Redistributions of source code must retain the above copyright | 7 // * Redistributions of source code must retain the above copyright |
| 8 // notice, this list of conditions and the following disclaimer. | 8 // notice, this list of conditions and the following disclaimer. |
| 9 // * Redistributions in binary form must reproduce the above | 9 // * Redistributions in binary form must reproduce the above |
| 10 // copyright notice, this list of conditions and the following disclaimer | 10 // copyright notice, this list of conditions and the following disclaimer |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 // this->reset(this->get()) works. | 169 // this->reset(this->get()) works. |
| 170 void reset(C* p = NULL) { | 170 void reset(C* p = NULL) { |
| 171 if (p != ptr_) { | 171 if (p != ptr_) { |
| 172 enum { type_must_be_complete = sizeof(C) }; | 172 enum { type_must_be_complete = sizeof(C) }; |
| 173 C* old_ptr = ptr_; | 173 C* old_ptr = ptr_; |
| 174 ptr_ = p; | 174 ptr_ = p; |
| 175 delete old_ptr; | 175 delete old_ptr; |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 // Move assignment. | |
| 180 unique_ptr<C>& operator=(unique_ptr<C>&& that) { | |
| 181 if (that.ptr_ != ptr_) { | |
|
Jeffrey Yasskin
2015/09/22 20:12:55
Implement this as "reset(that.release());"
Tom Sepez
2015/09/22 22:05:13
Done. clever.
| |
| 182 enum { type_must_be_complete = sizeof(C) }; | |
| 183 C* old_ptr = ptr_; | |
| 184 ptr_ = that.ptr_; | |
| 185 that.ptr_ = nullptr; | |
| 186 delete old_ptr; | |
| 187 } | |
| 188 return *this; | |
| 189 } | |
| 190 | |
| 179 private: | 191 private: |
| 180 // Forbid comparison of unique_ptr types. If C2 != C, it totally doesn't | 192 // Forbid comparison of unique_ptr types. If C2 != C, it totally doesn't |
| 181 // make sense, and if C2 == C, it still doesn't make sense because you should | 193 // make sense, and if C2 == C, it still doesn't make sense because you should |
| 182 // never have the same object owned by two different unique_ptrs. | 194 // never have the same object owned by two different unique_ptrs. |
| 183 template <class C2> bool operator==(unique_ptr<C2> const& p2) const; | 195 template <class C2> bool operator==(unique_ptr<C2> const& p2) const; |
| 184 template <class C2> bool operator!=(unique_ptr<C2> const& p2) const; | 196 template <class C2> bool operator!=(unique_ptr<C2> const& p2) const; |
| 185 | 197 |
| 186 // Disallow evil constructors. It doesn't make sense to make a copy of | 198 // Disallow evil constructors. It doesn't make sense to make a copy of |
| 187 // something that's allegedly unique. | 199 // something that's allegedly unique. |
| 188 unique_ptr(const unique_ptr&) = delete; | 200 unique_ptr(const unique_ptr&) = delete; |
| 189 void operator=(const unique_ptr&) = delete; | 201 void operator=(const unique_ptr&) = delete; |
| 190 }; | 202 }; |
| 191 | 203 |
| 192 // Specialization for arrays using delete[]. | 204 // Specialization for arrays using delete[]. |
| 193 template <class C> | 205 template <class C> |
| 194 class unique_ptr<C[]> : public unique_ptr_base<C> { | 206 class unique_ptr<C[]> : public unique_ptr_base<C> { |
|
Jeffrey Yasskin
2015/09/22 20:12:55
Probably keep unique_ptr<C[]> consistent with uniq
Tom Sepez
2015/09/22 22:05:13
Done.
| |
| 195 public: | 207 public: |
| 196 using unique_ptr_base<C>::ptr_; | 208 using unique_ptr_base<C>::ptr_; |
| 197 | 209 |
| 198 // Constructor. Defaults to initializing with NULL. There is no way | 210 // Constructor. Defaults to initializing with NULL. There is no way |
| 199 // to create an uninitialized unique_ptr. The input parameter must be | 211 // to create an uninitialized unique_ptr. The input parameter must be |
| 200 // allocated with new[] (not new - see above). | 212 // allocated with new[] (not new - see above). |
| 201 explicit unique_ptr(C* p = NULL) : unique_ptr_base<C>(p) { } | 213 explicit unique_ptr(C* p = NULL) : unique_ptr_base<C>(p) { } |
| 202 | 214 |
| 203 // Move constructor. | 215 // Move constructor. |
| 204 unique_ptr(unique_ptr<C>&& that) : unique_ptr_base<C>(nonstd::move(that)) {} | 216 unique_ptr(unique_ptr<C>&& that) : unique_ptr_base<C>(nonstd::move(that)) {} |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 } | 262 } |
| 251 | 263 |
| 252 template <class C> | 264 template <class C> |
| 253 bool operator!=(C* p1, const unique_ptr<C>& p2) { | 265 bool operator!=(C* p1, const unique_ptr<C>& p2) { |
| 254 return p1 != p2.get(); | 266 return p1 != p2.get(); |
| 255 } | 267 } |
| 256 | 268 |
| 257 } // namespace nonstd | 269 } // namespace nonstd |
| 258 | 270 |
| 259 #endif // NONSTD_UNIQUE_PTR_H_ | 271 #endif // NONSTD_UNIQUE_PTR_H_ |
| OLD | NEW |