OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 SERVICES_MEDIA_FRAMEWORK_PTR_H_ | |
6 #define SERVICES_MEDIA_FRAMEWORK_PTR_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 namespace mojo { | |
11 namespace media { | |
12 | |
13 // unique_ptr with Clone. | |
14 template<class T, class Deleter = std::default_delete<T>> | |
15 class UniquePtr : public std::unique_ptr<T, Deleter> { | |
16 public: | |
17 UniquePtr() : std::unique_ptr<T, Deleter>() {} | |
18 | |
19 UniquePtr(std::nullptr_t) : std::unique_ptr<T, Deleter>() {} | |
20 | |
21 explicit UniquePtr(T* ptr) : std::unique_ptr<T, Deleter>(ptr) {} | |
22 | |
23 UniquePtr(UniquePtr&& other) : | |
24 std::unique_ptr<T, Deleter>(std::move(other)) {} | |
25 | |
26 UniquePtr& operator=(std::nullptr_t) { | |
27 this->reset(); | |
28 return *this; | |
29 } | |
30 | |
31 UniquePtr& operator=(UniquePtr&& other) { | |
32 *static_cast<std::unique_ptr<T, Deleter>*>(this) = std::move(other); | |
33 return *this; | |
34 } | |
35 | |
36 UniquePtr Clone() const { return *this ? this->get()->Clone() : UniquePtr(); } | |
johngro
2016/01/26 23:47:29
Do we really want to sub-class std::unique_ptr to
dalesat
2016/01/28 18:49:16
Yes, this is just to get rid of the null check. I'
| |
37 }; | |
38 | |
39 // shared_ptr with upcast to TBase. | |
johngro
2016/01/26 23:47:29
Why do this? std::shared_ptr already has 3 static
dalesat
2016/01/28 18:49:16
I wasn't aware of static_pointer_cast. This class
johngro
2016/02/01 22:38:16
Acknowledged.
| |
40 template<class T, typename TBase> | |
41 class SharedPtr : public std::shared_ptr<T> { | |
42 public: | |
43 SharedPtr() : std::shared_ptr<T>() {} | |
44 | |
45 SharedPtr(std::nullptr_t) : std::shared_ptr<T>() {} | |
46 | |
47 explicit SharedPtr(T* ptr) : std::shared_ptr<T>(ptr) {} | |
48 | |
49 SharedPtr& operator=(std::nullptr_t) { | |
50 this->reset(); | |
51 return *this; | |
52 } | |
53 | |
54 operator std::shared_ptr<TBase>() const { | |
55 return std::shared_ptr<TBase>(*this, this->get()); | |
56 } | |
57 }; | |
58 | |
59 } // namespace media | |
60 } // namespace mojo | |
61 | |
62 #endif // SERVICES_MEDIA_FRAMEWORK_PTR_H_ | |
OLD | NEW |