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

Side by Side Diff: net/quic/platform/api/quic_reference_counted.h

Issue 2589983002: Create a QUIC wrapper around scoped_refptr. (Closed)
Patch Set: rm = nullptr Created 4 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
« no previous file with comments | « net/quic/core/quic_stream_test.cc ('k') | net/quic/platform/api/quic_reference_counted_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 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 NET_QUIC_PLATFORM_API_QUIC_REFERENCE_COUNTED_H_
6 #define NET_QUIC_PLATFORM_API_QUIC_REFERENCE_COUNTED_H_
7
8 #include "net/quic/platform/impl/quic_reference_counted_impl.h"
9
10 namespace net {
11
12 // Base class for explicitly reference-counted objects in QUIC.
13 class QUIC_EXPORT_PRIVATE QuicReferenceCounted
14 : public QuicReferenceCountedImpl {
15 public:
16 QuicReferenceCounted() {}
17
18 protected:
19 ~QuicReferenceCounted() override {}
20 };
21
22 // A class representing a reference counted pointer in QUIC.
23 //
24 // Construct or initialize QuicReferenceCountedPointer from raw pointer. Here
25 // raw pointer MUST be a newly created object. Reference count of a newly
26 // created object is undefined, but that will be 1 after being added to
27 // QuicReferenceCountedPointer.
28 // QuicReferenceCountedPointer is used as a local variable.
29 // QuicReferenceCountedPointer<T> r_ptr(new T());
30 // or, equivalently:
31 // QuicReferenceCountedPointer<T> r_ptr;
32 // T* p = new T();
33 // r_ptr = T;
34 //
35 // QuicReferenceCountedPointer is used as a member variable:
36 // MyClass::MyClass() : r_ptr(new T()) {}
37 //
38 // This is WRONG, since *p is not guaranteed to be newly created:
39 // MyClass::MyClass(T* p) : r_ptr(p) {}
40 //
41 // Given an existing QuicReferenceCountedPointer, create a duplicate that has
42 // its own reference on the object:
43 // QuicReferenceCountedPointer<T> r_ptr_b(r_ptr_a);
44 // or, equivalently:
45 // QuicReferenceCountedPointer<T> r_ptr_b = r_ptr_a;
46 //
47 // Given an existing QuicReferenceCountedPointer, create a
48 // QuicReferenceCountedPointer that adopts the reference:
49 // QuicReferenceCountedPointer<T> r_ptr_b(std::move(r_ptr_a));
50 // or, equivalently:
51 // QuicReferenceCountedPointer<T> r_ptr_b = std::move(r_ptr_a);
52
53 template <class T>
54 class QuicReferenceCountedPointer {
55 public:
56 QuicReferenceCountedPointer() = default;
57
58 // Constructor from raw pointer |p|. This guarantees the reference count of *p
59 // is 1. This should only be used when a new object is created, calling this
60 // on an already existent object is undefined behavior.
61 explicit QuicReferenceCountedPointer(T* p) : impl_(p) {}
62
63 // Allows implicit conversion from nullptr.
64 QuicReferenceCountedPointer(std::nullptr_t) : impl_(nullptr) {} // NOLINT
65
66 // Copy and copy conversion constructors. It does not take the reference away
67 // from |other| and they each end up with their own reference.
68 template <typename U>
69 QuicReferenceCountedPointer( // NOLINT
70 const QuicReferenceCountedPointer<U>& other)
71 : impl_(other.impl()) {}
72 QuicReferenceCountedPointer(const QuicReferenceCountedPointer& other)
73 : impl_(other.impl()) {}
74
75 // Move constructors. After move, It adopts the reference from |other|.
76 template <typename U>
77 QuicReferenceCountedPointer(QuicReferenceCountedPointer<U>&& other) // NOLINT
78 : impl_(std::move(other.impl())) {}
79 QuicReferenceCountedPointer(QuicReferenceCountedPointer&& other)
80 : impl_(std::move(other.impl())) {}
81
82 ~QuicReferenceCountedPointer() = default;
83
84 // Copy assignments.
85 QuicReferenceCountedPointer& operator=(
86 const QuicReferenceCountedPointer& other) {
87 impl_ = other.impl();
88 return *this;
89 }
90 template <typename U>
91 QuicReferenceCountedPointer<T>& operator=(
92 const QuicReferenceCountedPointer<U>& other) {
93 impl_ = other.impl();
94 return *this;
95 }
96
97 // Move assignments.
98 QuicReferenceCountedPointer& operator=(QuicReferenceCountedPointer&& other) {
99 impl_ = std::move(other.impl());
100 return *this;
101 }
102 template <typename U>
103 QuicReferenceCountedPointer<T>& operator=(
104 QuicReferenceCountedPointer<U>&& other) {
105 impl_ = std::move(other.impl());
106 return *this;
107 }
108
109 // Accessors for the referenced object.
110 // operator* and operator-> will assert() if there is no current object.
111 T& operator*() const { return *impl_; }
112 T* operator->() const { return impl_.get(); }
113
114 explicit operator bool() const { return static_cast<bool>(impl_); }
115
116 // Assignment operator on raw pointer. Drops a reference to current pointee,
117 // if any and replaces it with |p|. This garantee the reference count of *p is
118 // 1. This should only be used when a new object is created, calling this
119 // on a already existent object is undefined behavior.
120 QuicReferenceCountedPointer<T>& operator=(T* p) {
121 impl_ = p;
122 return *this;
123 }
124
125 // Returns the raw pointer with no change in reference.
126 T* get() const { return impl_.get(); }
127
128 QuicReferenceCountedPointerImpl<T>& impl() { return impl_; }
129 const QuicReferenceCountedPointerImpl<T>& impl() const { return impl_; }
130
131 // Comparisons against same type.
132 friend bool operator==(const QuicReferenceCountedPointer& a,
133 const QuicReferenceCountedPointer& b) {
134 return a.get() == b.get();
135 }
136 friend bool operator!=(const QuicReferenceCountedPointer& a,
137 const QuicReferenceCountedPointer& b) {
138 return a.get() != b.get();
139 }
140
141 // Comparisons against nullptr.
142 friend bool operator==(const QuicReferenceCountedPointer& a, std::nullptr_t) {
143 return a.get() == nullptr;
144 }
145 friend bool operator==(std::nullptr_t, const QuicReferenceCountedPointer& b) {
146 return nullptr == b.get();
147 }
148 friend bool operator!=(const QuicReferenceCountedPointer& a, std::nullptr_t) {
149 return a.get() != nullptr;
150 }
151 friend bool operator!=(std::nullptr_t, const QuicReferenceCountedPointer& b) {
152 return nullptr != b.get();
153 }
154
155 private:
156 QuicReferenceCountedPointerImpl<T> impl_;
157 };
158
159 } // namespace net
160
161 #endif // NET_QUIC_PLATFORM_API_QUIC_REFERENCE_COUNTED_H_
OLDNEW
« no previous file with comments | « net/quic/core/quic_stream_test.cc ('k') | net/quic/platform/api/quic_reference_counted_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698