| 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 // An annotation used along with a pointer parameter of a function to indicate |
| 13 // that the function doesn't take ownership of the pointer. |
| 14 enum NotOwned { |
| 15 NOT_OWNED |
| 16 }; |
| 17 |
| 18 namespace internal { |
| 19 |
| 20 // MayOwnPtr keeps track of whether it has ownership of the pointer it holds, |
| 21 // and deletes the pointer on destruction if it does. |
| 22 template <typename T> |
| 23 class MayOwnPtr { |
| 24 public: |
| 25 // Creates and owns a T instance. |
| 26 // NOTE: "()" after T is important to do zero-initialization for POD types. |
| 27 MayOwnPtr() : value_(new T()), owned_(true) {} |
| 28 |
| 29 // It doesn't take ownership of |value|, therefore |value| must live longer |
| 30 // than this object. |
| 31 MayOwnPtr(T* value, NotOwned) : value_(value), owned_(false) { |
| 32 PP_DCHECK(value); |
| 33 } |
| 34 |
| 35 ~MayOwnPtr() { |
| 36 if (owned_) |
| 37 delete value_; |
| 38 } |
| 39 |
| 40 const T* get() const { return value_; } |
| 41 T* get() { return value_; } |
| 42 |
| 43 const T& operator*() const { return *value_; } |
| 44 T& operator*() { return *value_; } |
| 45 |
| 46 const T* operator->() const { return value_; } |
| 47 T* operator->() { return value_; } |
| 48 |
| 49 private: |
| 50 // Disallow copying and assignment. |
| 51 MayOwnPtr(const MayOwnPtr<T>&); |
| 52 MayOwnPtr<T>& operator=(const MayOwnPtr<T>&); |
| 53 |
| 54 T* value_; |
| 55 bool owned_; |
| 56 }; |
| 57 |
| 58 } // namespace internal |
| 59 } // namespace pp |
| 60 |
| 61 #endif // PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_ |
| OLD | NEW |