Chromium Code Reviews| Index: ppapi/cpp/dev/may_own_ptr_dev.h |
| diff --git a/ppapi/cpp/dev/may_own_ptr_dev.h b/ppapi/cpp/dev/may_own_ptr_dev.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..71561d5b616fb755f2ef25df1ef5f3f362693517 |
| --- /dev/null |
| +++ b/ppapi/cpp/dev/may_own_ptr_dev.h |
| @@ -0,0 +1,61 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_ |
| +#define PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_ |
| + |
| +#include "ppapi/cpp/logging.h" |
| + |
| +namespace pp { |
| + |
| +// An annotation used along with a pointer parameter of a function to indicate |
| +// that the function doesn't take ownership of the pointer. |
| +enum NotOwned { |
| + NOT_OWNED |
| +}; |
| + |
| +namespace internal { |
| + |
| +// MayOwnPtr keeps track of whether it has ownership of the pointer it holds, |
| +// and deletes the pointer on destruction if it does. |
| +template <typename T> |
| +class MayOwnPtr { |
| + public: |
| + 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
|
| + |
| + // It doesn't take ownership of |value|, therefore |value| must live longer |
| + // than this object. |
| + MayOwnPtr(T* value, NotOwned) : value_(value), owned_(false) { |
| + PP_DCHECK(value); |
| + } |
| + |
| + ~MayOwnPtr() { |
| + if (owned_) |
| + delete value_; |
| + } |
| + |
| + const T* get() const { return value_; } |
| + T* get() { return value_; } |
| + |
| + const T& operator*() const { return *value_; } |
| + T& operator*() { return *value_; } |
| + |
| + const T* operator->() const { return value_; } |
| + T* operator->() { return value_; } |
| + |
| + bool owned() const { return owned_; } |
| + |
| + private: |
| + // Disallow copying and assignment. |
| + MayOwnPtr(const MayOwnPtr<T>&); |
| + MayOwnPtr<T>& operator=(const MayOwnPtr<T>&); |
| + |
| + T* value_; |
| + bool owned_; |
| +}; |
| + |
| +} // namespace internal |
| +} // namespace pp |
| + |
| +#endif // PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_ |