OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_PTR_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_PTR_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "mojo/public/cpp/bindings/lib/shared_data.h" | |
10 | |
11 namespace mojo { | |
12 namespace internal { | |
13 | |
14 // Used to manage a heap-allocated instance of P that can be shared via | |
15 // reference counting. When the last reference is dropped, the instance is | |
16 // deleted. | |
17 template <typename P> | |
18 class SharedPtr { | |
19 public: | |
20 SharedPtr() {} | |
21 | |
22 explicit SharedPtr(P* ptr) { impl_.mutable_value()->ptr = ptr; } | |
23 | |
24 // Default copy-constructor and assignment operator are OK. | |
25 | |
26 P* get() { return impl_.value().ptr; } | |
27 const P* get() const { return impl_.value().ptr; } | |
28 | |
29 void reset() { impl_.reset(); } | |
30 | |
31 P* operator->() { return get(); } | |
32 const P* operator->() const { return get(); } | |
33 | |
34 private: | |
35 class Impl { | |
36 public: | |
37 ~Impl() { | |
38 if (ptr) | |
39 delete ptr; | |
40 } | |
41 | |
42 Impl() : ptr(nullptr) {} | |
43 | |
44 Impl(P* ptr) : ptr(ptr) {} | |
45 | |
46 P* ptr; | |
47 | |
48 private: | |
49 DISALLOW_COPY_AND_ASSIGN(Impl); | |
50 }; | |
51 | |
52 SharedData<Impl> impl_; | |
53 }; | |
54 | |
55 } // namespace mojo | |
56 } // namespace internal | |
57 | |
58 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_PTR_H_ | |
OLD | NEW |