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

Side by Side 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 David'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 unified diff | Download patch
OLDNEW
(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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698