summaryrefslogtreecommitdiffstats
path: root/base/win/scoped_handle.h
diff options
context:
space:
mode:
Diffstat (limited to 'base/win/scoped_handle.h')
-rw-r--r--base/win/scoped_handle.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/base/win/scoped_handle.h b/base/win/scoped_handle.h
index 0d038e0..d3f5c22 100644
--- a/base/win/scoped_handle.h
+++ b/base/win/scoped_handle.h
@@ -39,6 +39,22 @@ class GenericScopedHandle {
public:
typedef typename Traits::Handle Handle;
+ // Helper object to contain the effect of Receive() to the function that needs
+ // a pointer, and allow proper tracking of the handle.
+ class Receiver {
+ public:
+ explicit Receiver(GenericScopedHandle* owner)
+ : handle_(Traits::NullHandle()),
+ owner_(owner) {}
+ ~Receiver() { owner_->Set(handle_); }
+
+ operator Handle*() { return &handle_; }
+
+ private:
+ Handle handle_;
+ GenericScopedHandle* owner_;
+ };
+
GenericScopedHandle() : handle_(Traits::NullHandle()) {}
explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) {
@@ -86,6 +102,16 @@ class GenericScopedHandle {
return handle_;
}
+ // This method is intended to be used with functions that require a pointer to
+ // a destination handle, like so:
+ // void CreateRequiredHandle(Handle* out_handle);
+ // ScopedHandle a;
+ // CreateRequiredHandle(a.Receive());
+ Receiver Receive() {
+ DCHECK(!Traits::IsHandleValid(handle_)) << "Handle must be NULL";
+ return Receiver(this);
+ }
+
// Transfers ownership away from this object.
Handle Take() {
Handle temp = handle_;