OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_ |
| 6 #define PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_ |
| 7 |
| 8 #include "ppapi/cpp/logging.h" |
| 9 |
| 10 namespace pp { |
| 11 |
| 12 enum NotOwned { |
| 13 NOT_OWNED |
| 14 }; |
| 15 |
| 16 namespace internal { |
| 17 |
| 18 template <class T> |
| 19 class MayOwnPtr { |
| 20 public: |
| 21 MayOwnPtr(T* value, NotOwned) : value_(value), owned_(false) { |
| 22 } |
| 23 |
| 24 MayOwnPtr() : value_(new T()), owned_(true) { |
| 25 } |
| 26 |
| 27 explicit MayOwnPtr(const T& other) : value_(new T(other)), |
| 28 owned_(true) { |
| 29 } |
| 30 |
| 31 MayOwnPtr(const MayOwnPtr& other) : value_(new T(*other.value_)), |
| 32 owned_(true) { |
| 33 } |
| 34 |
| 35 ~MayOwnPtr() { |
| 36 if (owned_) |
| 37 delete value_; |
| 38 } |
| 39 |
| 40 MayOwnPtr& operator=(const MayOwnPtr& other) { |
| 41 if (this == &other) |
| 42 return *this; |
| 43 |
| 44 value_ = new T(*other.value_); |
| 45 owned_ = true; |
| 46 |
| 47 return *this; |
| 48 } |
| 49 |
| 50 const T* get() const { |
| 51 return value_; |
| 52 } |
| 53 |
| 54 T* get() { |
| 55 return value_; |
| 56 } |
| 57 |
| 58 T& operator*() const { |
| 59 return *value_; |
| 60 } |
| 61 |
| 62 T* operator->() const { |
| 63 return value_; |
| 64 } |
| 65 |
| 66 bool owned() const { |
| 67 return owned_; |
| 68 } |
| 69 |
| 70 private: |
| 71 T* value_; |
| 72 bool owned_; |
| 73 }; |
| 74 |
| 75 } // namespace internal |
| 76 } // namespace pp |
| 77 |
| 78 #endif // PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_ |
OLD | NEW |