#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 {
class MOJO_SYSTEM_IMPL_EXPORT ScopedIpczHandle {
public:
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_; }
void reset();
[[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.");
}
#endif