15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#ifndef MOJO_EMBEDDER_SCOPED_PLATFORM_HANDLE_H_
623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#define MOJO_EMBEDDER_SCOPED_PLATFORM_HANDLE_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/compiler_specific.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/move.h"
1023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "mojo/embedder/platform_handle.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "mojo/system/system_impl_export.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace mojo {
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace embedder {
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MOJO_SYSTEM_IMPL_EXPORT ScopedPlatformHandle {
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MOVE_ONLY_TYPE_FOR_CPP_03(ScopedPlatformHandle, RValue)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ScopedPlatformHandle() {}
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  explicit ScopedPlatformHandle(PlatformHandle handle) : handle_(handle) {}
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~ScopedPlatformHandle() { handle_.CloseIfNecessary(); }
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Move-only constructor and operator=.
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ScopedPlatformHandle(RValue other) : handle_(other.object->release()) {}
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ScopedPlatformHandle& operator=(RValue other) {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    handle_ = other.object->release();
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return *this;
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const PlatformHandle& get() const { return handle_; }
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void swap(ScopedPlatformHandle& other) {
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PlatformHandle temp = handle_;
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    handle_ = other.handle_;
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    other.handle_ = temp;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PlatformHandle release() WARN_UNUSED_RESULT {
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PlatformHandle rv = handle_;
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    handle_ = PlatformHandle();
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return rv;
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void reset(PlatformHandle handle = PlatformHandle()) {
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    handle_.CloseIfNecessary();
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    handle_ = handle;
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  bool is_valid() const { return handle_.is_valid(); }
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PlatformHandle handle_;
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace embedder
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace mojo
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#endif  // MOJO_EMBEDDER_SCOPED_PLATFORM_HANDLE_H_
60