ipc/chromium/src/base/scoped_handle_win.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/scoped_handle_win.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,217 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef BASE_SCOPED_HANDLE_WIN_H_
     1.9 +#define BASE_SCOPED_HANDLE_WIN_H_
    1.10 +
    1.11 +#include <windows.h>
    1.12 +
    1.13 +#include "base/basictypes.h"
    1.14 +#include "base/logging.h"
    1.15 +
    1.16 +// Used so we always remember to close the handle.
    1.17 +// The class interface matches that of ScopedStdioHandle in  addition to an
    1.18 +// IsValid() method since invalid handles on windows can be either NULL or
    1.19 +// INVALID_HANDLE_VALUE (-1).
    1.20 +//
    1.21 +// Example:
    1.22 +//   ScopedHandle hfile(CreateFile(...));
    1.23 +//   if (!hfile.Get())
    1.24 +//     ...process error
    1.25 +//   ReadFile(hfile.Get(), ...);
    1.26 +//
    1.27 +// To sqirrel the handle away somewhere else:
    1.28 +//   secret_handle_ = hfile.Take();
    1.29 +//
    1.30 +// To explicitly close the handle:
    1.31 +//   hfile.Close();
    1.32 +class ScopedHandle {
    1.33 + public:
    1.34 +  ScopedHandle() : handle_(NULL) {
    1.35 +  }
    1.36 +
    1.37 +  explicit ScopedHandle(HANDLE h) : handle_(NULL) {
    1.38 +    Set(h);
    1.39 +  }
    1.40 +
    1.41 +  ~ScopedHandle() {
    1.42 +    Close();
    1.43 +  }
    1.44 +
    1.45 +  // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
    1.46 +  // usage for errors.
    1.47 +  bool IsValid() const {
    1.48 +    return handle_ != NULL;
    1.49 +  }
    1.50 +
    1.51 +  void Set(HANDLE new_handle) {
    1.52 +    Close();
    1.53 +
    1.54 +    // Windows is inconsistent about invalid handles, so we always use NULL
    1.55 +    if (new_handle != INVALID_HANDLE_VALUE)
    1.56 +      handle_ = new_handle;
    1.57 +  }
    1.58 +
    1.59 +  HANDLE Get() {
    1.60 +    return handle_;
    1.61 +  }
    1.62 +
    1.63 +  operator HANDLE() { return handle_; }
    1.64 +
    1.65 +  HANDLE Take() {
    1.66 +    // transfers ownership away from this object
    1.67 +    HANDLE h = handle_;
    1.68 +    handle_ = NULL;
    1.69 +    return h;
    1.70 +  }
    1.71 +
    1.72 +  void Close() {
    1.73 +    if (handle_) {
    1.74 +      if (!::CloseHandle(handle_)) {
    1.75 +        NOTREACHED();
    1.76 +      }
    1.77 +      handle_ = NULL;
    1.78 +    }
    1.79 +  }
    1.80 +
    1.81 + private:
    1.82 +  HANDLE handle_;
    1.83 +  DISALLOW_EVIL_CONSTRUCTORS(ScopedHandle);
    1.84 +};
    1.85 +
    1.86 +// Like ScopedHandle, but for HANDLEs returned from FindFile().
    1.87 +class ScopedFindFileHandle {
    1.88 + public:
    1.89 +  explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) {
    1.90 +    // Windows is inconsistent about invalid handles, so we always use NULL
    1.91 +    if (handle_ == INVALID_HANDLE_VALUE)
    1.92 +      handle_ = NULL;
    1.93 +  }
    1.94 +
    1.95 +  ~ScopedFindFileHandle() {
    1.96 +    if (handle_)
    1.97 +      FindClose(handle_);
    1.98 +  }
    1.99 +
   1.100 +  // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
   1.101 +  // usage for errors.
   1.102 +  bool IsValid() const { return handle_ != NULL; }
   1.103 +
   1.104 +  operator HANDLE() { return handle_; }
   1.105 +
   1.106 + private:
   1.107 +  HANDLE handle_;
   1.108 +
   1.109 +  DISALLOW_EVIL_CONSTRUCTORS(ScopedFindFileHandle);
   1.110 +};
   1.111 +
   1.112 +// Like ScopedHandle but for HDC.  Only use this on HDCs returned from
   1.113 +// CreateCompatibleDC.  For an HDC returned by GetDC, use ReleaseDC instead.
   1.114 +class ScopedHDC {
   1.115 + public:
   1.116 +  ScopedHDC() : hdc_(NULL) { }
   1.117 +  explicit ScopedHDC(HDC h) : hdc_(h) { }
   1.118 +
   1.119 +  ~ScopedHDC() {
   1.120 +    Close();
   1.121 +  }
   1.122 +
   1.123 +  HDC Get() {
   1.124 +    return hdc_;
   1.125 +  }
   1.126 +
   1.127 +  void Set(HDC h) {
   1.128 +    Close();
   1.129 +    hdc_ = h;
   1.130 +  }
   1.131 +
   1.132 +  operator HDC() { return hdc_; }
   1.133 +
   1.134 + private:
   1.135 +  void Close() {
   1.136 +#ifdef NOGDI
   1.137 +    assert(false);
   1.138 +#else
   1.139 +    if (hdc_)
   1.140 +      DeleteDC(hdc_);
   1.141 +#endif  // NOGDI
   1.142 +  }
   1.143 +
   1.144 +  HDC hdc_;
   1.145 +  DISALLOW_EVIL_CONSTRUCTORS(ScopedHDC);
   1.146 +};
   1.147 +
   1.148 +// Like ScopedHandle but for GDI objects.
   1.149 +template<class T>
   1.150 +class ScopedGDIObject {
   1.151 + public:
   1.152 +  ScopedGDIObject() : object_(NULL) {}
   1.153 +  explicit ScopedGDIObject(T object) : object_(object) {}
   1.154 +
   1.155 +  ~ScopedGDIObject() {
   1.156 +    Close();
   1.157 +  }
   1.158 +
   1.159 +  T Get() {
   1.160 +    return object_;
   1.161 +  }
   1.162 +
   1.163 +  void Set(T object) {
   1.164 +    if (object_ && object != object_)
   1.165 +      Close();
   1.166 +    object_ = object;
   1.167 +  }
   1.168 +
   1.169 +  ScopedGDIObject& operator=(T object) {
   1.170 +    Set(object);
   1.171 +    return *this;
   1.172 +  }
   1.173 +
   1.174 +  operator T() { return object_; }
   1.175 +
   1.176 + private:
   1.177 +  void Close() {
   1.178 +    if (object_)
   1.179 +      DeleteObject(object_);
   1.180 +  }
   1.181 +
   1.182 +  T object_;
   1.183 +  DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
   1.184 +};
   1.185 +
   1.186 +// Typedefs for some common use cases.
   1.187 +typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
   1.188 +typedef ScopedGDIObject<HRGN> ScopedHRGN;
   1.189 +typedef ScopedGDIObject<HFONT> ScopedHFONT;
   1.190 +
   1.191 +
   1.192 +// Like ScopedHandle except for HGLOBAL.
   1.193 +template<class T>
   1.194 +class ScopedHGlobal {
   1.195 + public:
   1.196 +  explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) {
   1.197 +    data_ = static_cast<T*>(GlobalLock(glob_));
   1.198 +  }
   1.199 +  ~ScopedHGlobal() {
   1.200 +    GlobalUnlock(glob_);
   1.201 +  }
   1.202 +
   1.203 +  T* get() { return data_; }
   1.204 +
   1.205 +  size_t Size() const { return GlobalSize(glob_); }
   1.206 +
   1.207 +  T* operator->() const  {
   1.208 +    assert(data_ != 0);
   1.209 +    return data_;
   1.210 +  }
   1.211 +
   1.212 + private:
   1.213 +  HGLOBAL glob_;
   1.214 +
   1.215 +  T* data_;
   1.216 +
   1.217 +  DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal);
   1.218 +};
   1.219 +
   1.220 +#endif // BASE_SCOPED_HANDLE_WIN_H_

mercurial