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: mojo/public/cpp/system/handle.h

Issue 1932083002: Mojo: Use new message APIs to reduce copying (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 4 years, 7 months 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 | « mojo/public/cpp/system/BUILD.gn ('k') | mojo/public/cpp/system/message.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ 6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <limits> 9 #include <limits>
10 10
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 117
118 void reset(HandleType handle = HandleType()) { 118 void reset(HandleType handle = HandleType()) {
119 CloseIfNecessary(); 119 CloseIfNecessary();
120 handle_ = handle; 120 handle_ = handle;
121 } 121 }
122 122
123 bool is_valid() const { return handle_.is_valid(); } 123 bool is_valid() const { return handle_.is_valid(); }
124 124
125 private: 125 private:
126 void CloseIfNecessary() { 126 void CloseIfNecessary() {
127 if (!handle_.is_valid()) 127 if (handle_.is_valid())
128 return; 128 handle_.Close();
129 MojoResult result = MojoClose(handle_.value());
130 ALLOW_UNUSED_LOCAL(result);
131 DCHECK_EQ(MOJO_RESULT_OK, result);
132 } 129 }
133 130
134 HandleType handle_; 131 HandleType handle_;
135 }; 132 };
136 133
137 template <typename HandleType> 134 template <typename HandleType>
138 inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) { 135 inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) {
139 return ScopedHandleBase<HandleType>(handle); 136 return ScopedHandleBase<HandleType>(handle);
140 } 137 }
141 138
(...skipping 13 matching lines...) Expand all
155 value_ = other.value_; 152 value_ = other.value_;
156 other.value_ = temp; 153 other.value_ = temp;
157 } 154 }
158 155
159 bool is_valid() const { return value_ != kInvalidHandleValue; } 156 bool is_valid() const { return value_ != kInvalidHandleValue; }
160 157
161 const MojoHandle& value() const { return value_; } 158 const MojoHandle& value() const { return value_; }
162 MojoHandle* mutable_value() { return &value_; } 159 MojoHandle* mutable_value() { return &value_; }
163 void set_value(MojoHandle value) { value_ = value; } 160 void set_value(MojoHandle value) { value_ = value; }
164 161
162 void Close() {
163 DCHECK(is_valid());
164 MojoResult result = MojoClose(value_);
165 ALLOW_UNUSED_LOCAL(result);
166 DCHECK_EQ(MOJO_RESULT_OK, result);
167 }
168
165 private: 169 private:
166 MojoHandle value_; 170 MojoHandle value_;
167 171
168 // Copying and assignment allowed. 172 // Copying and assignment allowed.
169 }; 173 };
170 174
171 // Should have zero overhead. 175 // Should have zero overhead.
172 static_assert(sizeof(Handle) == sizeof(MojoHandle), "Bad size for C++ Handle"); 176 static_assert(sizeof(Handle) == sizeof(MojoHandle), "Bad size for C++ Handle");
173 177
174 // The scoper should also impose no more overhead. 178 // The scoper should also impose no more overhead.
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 } 295 }
292 296
293 // Comparison, so that |Handle|s can be used as keys in hash maps. 297 // Comparison, so that |Handle|s can be used as keys in hash maps.
294 inline bool operator==(const Handle a, const Handle b) { 298 inline bool operator==(const Handle a, const Handle b) {
295 return a.value() == b.value(); 299 return a.value() == b.value();
296 } 300 }
297 301
298 } // namespace mojo 302 } // namespace mojo
299 303
300 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ 304 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/system/BUILD.gn ('k') | mojo/public/cpp/system/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698