Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Unified Diff: ppapi/cpp/dev/may_own_ptr_dev.h

Issue 116963003: App APIs in Pepper: C++ APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: changes according to Sam's suggestions. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ppapi/cpp/dev/array_dev.h ('k') | ppapi/cpp/dev/optional_dev.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..59a0873a89784327b37782054aa643a1729ba088
--- /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:
+ // Creates and owns a T instance.
+ // NOTE: "()" after T is important to do zero-initialization for POD types.
+ MayOwnPtr() : value_(new T()), owned_(true) {}
+
+ // 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_; }
+
+ 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_
« no previous file with comments | « ppapi/cpp/dev/array_dev.h ('k') | ppapi/cpp/dev/optional_dev.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698