File: scoped_ipcz_handle.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (79 lines) | stat: -rw-r--r-- 2,456 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_CORE_SCOPED_IPCZ_HANDLE_H_
#define MOJO_CORE_SCOPED_IPCZ_HANDLE_H_

#include <utility>

#include "base/check.h"
#include "base/memory/raw_ref.h"
#include "mojo/core/system_impl_export.h"
#include "third_party/ipcz/include/ipcz/ipcz.h"

namespace mojo::core {

// ScopedIpczHandle implements unique ownership of an IpczHandle. This object
// closes the underlying handle value on destruction if it hasn't been closed
// yet.
class MOJO_SYSTEM_IMPL_EXPORT ScopedIpczHandle {
 public:
  // Receiver is an adapter which can be used to pass a ScopedIpczHandle as an
  // IpczHandle*, for interfacing with C APIs that return handles through
  // output parameters. For some function like:
  //
  //     Foo(IpczHandle* out_handle)
  //
  // We can receive Foo's output into a ScopedIpczHandles with something like:
  //
  //     ScopedIpczHandle foo;
  //     Foo(ScopedIpczHandle::Receiver(foo))
  //
  class Receiver {
   public:
    explicit Receiver(ScopedIpczHandle& target) : target_(target) {}

    ~Receiver() {
      if (received_handle_ != IPCZ_INVALID_HANDLE) {
        (*target_) = ScopedIpczHandle(received_handle_);
      }
    }

    operator IpczHandle*() { return &received_handle_; }

   private:
    const raw_ref<ScopedIpczHandle> target_;
    IpczHandle received_handle_ = IPCZ_INVALID_HANDLE;
  };

  ScopedIpczHandle();
  explicit ScopedIpczHandle(IpczHandle handle);
  ScopedIpczHandle(ScopedIpczHandle&&);
  ScopedIpczHandle(const ScopedIpczHandle&) = delete;
  ScopedIpczHandle& operator=(ScopedIpczHandle&&);
  ScopedIpczHandle& operator=(const ScopedIpczHandle&) = delete;
  ~ScopedIpczHandle();

  bool is_valid() const { return handle_ != IPCZ_INVALID_HANDLE; }
  const IpczHandle& get() const { return handle_; }

  // Resets this object to an invalid handle, closing the previously held handle
  // if it was valid.
  void reset();

  // Releases ownership of the underlying IpczHandle and returns its value.
  [[nodiscard]] IpczHandle release() {
    return std::exchange(handle_, IPCZ_INVALID_HANDLE);
  }

 private:
  IpczHandle handle_ = IPCZ_INVALID_HANDLE;
};

static_assert(sizeof(IpczHandle) == sizeof(ScopedIpczHandle),
              "ScopedIpczHandle must be the same size as IpczHandle.");

}  // namespace mojo::core

#endif  // MOJO_CORE_SCOPED_IPCZ_HANDLE_H_