Roll breakpad a513e85:7caf028 (svn 1384:1385)
[chromium-blink-merge.git] / base / mac / scoped_mach_port.h
blob36087c9bdecfdf5651f8a06a9d2bc08faec4d5f6
1 // Copyright (c) 2012 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.
5 #ifndef BASE_MAC_SCOPED_MACH_PORT_H_
6 #define BASE_MAC_SCOPED_MACH_PORT_H_
8 #include <mach/mach.h>
10 #include "base/base_export.h"
11 #include "base/scoped_generic.h"
13 namespace base {
14 namespace mac {
16 namespace internal {
18 struct BASE_EXPORT SendRightTraits {
19 static mach_port_t InvalidValue() {
20 return MACH_PORT_NULL;
23 static void Free(mach_port_t port);
26 struct BASE_EXPORT ReceiveRightTraits {
27 static mach_port_t InvalidValue() {
28 return MACH_PORT_NULL;
31 static void Free(mach_port_t port);
34 } // namespace internal
36 // A scoper for handling a Mach port that names a send right. Send rights are
37 // reference counted, and this takes ownership of the right on construction
38 // and then removes a reference to the right on destruction. If the reference
39 // is the last one on the right, the right is deallocated.
40 class BASE_EXPORT ScopedMachSendRight :
41 public base::ScopedGeneric<mach_port_t, internal::SendRightTraits> {
42 public:
43 explicit ScopedMachSendRight(mach_port_t port = traits_type::InvalidValue())
44 : ScopedGeneric(port) {}
46 operator mach_port_t() const { return get(); }
49 // A scoper for handling a Mach port's receive right. There is only one
50 // receive right per port. This takes ownership of the receive right on
51 // construction and then destroys the right on destruction, turning all
52 // outstanding send rights into dead names.
53 class BASE_EXPORT ScopedMachReceiveRight :
54 public base::ScopedGeneric<mach_port_t, internal::ReceiveRightTraits> {
55 public:
56 explicit ScopedMachReceiveRight(
57 mach_port_t port = traits_type::InvalidValue()) : ScopedGeneric(port) {}
59 operator mach_port_t() const { return get(); }
62 } // namespace mac
63 } // namespace base
65 #endif // BASE_MAC_SCOPED_MACH_PORT_H_