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
3// found in the LICENSE file.
4
5#ifndef MOJO_EDK_EMBEDDER_SCOPED_PLATFORM_HANDLE_H_
6#define MOJO_EDK_EMBEDDER_SCOPED_PLATFORM_HANDLE_H_
7
8#include "base/compiler_specific.h"
9#include "base/macros.h"
10#include "mojo/edk/embedder/platform_handle.h"
11#include "mojo/edk/system/system_impl_export.h"
12#include "mojo/public/c/system/macros.h"
13
14namespace mojo {
15namespace edk {
16
17class MOJO_SYSTEM_IMPL_EXPORT ScopedPlatformHandle {
18 public:
19  ScopedPlatformHandle() {}
20  explicit ScopedPlatformHandle(PlatformHandle handle) : handle_(handle) {}
21  ~ScopedPlatformHandle() { handle_.CloseIfNecessary(); }
22
23  // Move-only constructor and operator=.
24  ScopedPlatformHandle(ScopedPlatformHandle&& other)
25      : handle_(other.release()) {}
26
27  ScopedPlatformHandle& operator=(ScopedPlatformHandle&& other) {
28    if (this != &other)
29      handle_ = other.release();
30    return *this;
31  }
32
33  const PlatformHandle& get() const { return handle_; }
34
35  void swap(ScopedPlatformHandle& other) {
36    PlatformHandle temp = handle_;
37    handle_ = other.handle_;
38    other.handle_ = temp;
39  }
40
41  PlatformHandle release() WARN_UNUSED_RESULT {
42    PlatformHandle rv = handle_;
43    handle_ = PlatformHandle();
44    return rv;
45  }
46
47  void reset(PlatformHandle handle = PlatformHandle()) {
48    handle_.CloseIfNecessary();
49    handle_ = handle;
50  }
51
52  bool is_valid() const { return handle_.is_valid(); }
53
54 private:
55  PlatformHandle handle_;
56
57  DISALLOW_COPY_AND_ASSIGN(ScopedPlatformHandle);
58};
59
60}  // namespace edk
61}  // namespace mojo
62
63#endif  // MOJO_EDK_EMBEDDER_SCOPED_PLATFORM_HANDLE_H_
64