Chromium Code Reviews| 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 MayOwnPtr() : value_(new T()), owned_(true) {} | |
|
dmichael (off chromium)
2013/12/20 18:38:00
(requires that T have a default constructor... pro
yzshen1
2013/12/20 19:50:43
Right, all things that we will used with MayOwnPtr
| |
| 26 | |
| 27 // It doesn't take ownership of |value|, therefore |value| must live longer | |
| 28 // than this object. | |
| 29 MayOwnPtr(T* value, NotOwned) : value_(value), owned_(false) { | |
| 30 PP_DCHECK(value); | |
| 31 } | |
| 32 | |
| 33 ~MayOwnPtr() { | |
| 34 if (owned_) | |
| 35 delete value_; | |
| 36 } | |
| 37 | |
| 38 const T* get() const { return value_; } | |
| 39 T* get() { return value_; } | |
| 40 | |
| 41 const T& operator*() const { return *value_; } | |
| 42 T& operator*() { return *value_; } | |
| 43 | |
| 44 const T* operator->() const { return value_; } | |
| 45 T* operator->() { return value_; } | |
| 46 | |
| 47 bool owned() const { return owned_; } | |
| 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 |