1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 #ifndef BASE_SCOPED_HANDLE_WIN_H_
8 #define BASE_SCOPED_HANDLE_WIN_H_
12 #include "base/basictypes.h"
13 #include "base/logging.h"
15 // Used so we always remember to close the handle.
16 // The class interface matches that of ScopedStdioHandle in addition to an
17 // IsValid() method since invalid handles on windows can be either NULL or
18 // INVALID_HANDLE_VALUE (-1).
21 // ScopedHandle hfile(CreateFile(...));
24 // ReadFile(hfile.Get(), ...);
26 // To sqirrel the handle away somewhere else:
27 // secret_handle_ = hfile.Take();
29 // To explicitly close the handle:
33 ScopedHandle() : handle_(NULL
) {}
35 explicit ScopedHandle(HANDLE h
) : handle_(NULL
) { Set(h
); }
37 ~ScopedHandle() { Close(); }
39 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
41 bool IsValid() const { return handle_
!= NULL
; }
43 void Set(HANDLE new_handle
) {
46 // Windows is inconsistent about invalid handles, so we always use NULL
47 if (new_handle
!= INVALID_HANDLE_VALUE
) handle_
= new_handle
;
50 HANDLE
Get() { return handle_
; }
52 operator HANDLE() { return handle_
; }
55 // transfers ownership away from this object
63 if (!::CloseHandle(handle_
)) {
72 DISALLOW_EVIL_CONSTRUCTORS(ScopedHandle
);
75 // Like ScopedHandle, but for HANDLEs returned from FindFile().
76 class ScopedFindFileHandle
{
78 explicit ScopedFindFileHandle(HANDLE handle
) : handle_(handle
) {
79 // Windows is inconsistent about invalid handles, so we always use NULL
80 if (handle_
== INVALID_HANDLE_VALUE
) handle_
= NULL
;
83 ~ScopedFindFileHandle() {
84 if (handle_
) FindClose(handle_
);
87 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
89 bool IsValid() const { return handle_
!= NULL
; }
91 operator HANDLE() { return handle_
; }
96 DISALLOW_EVIL_CONSTRUCTORS(ScopedFindFileHandle
);
99 // Like ScopedHandle but for HDC. Only use this on HDCs returned from
100 // CreateCompatibleDC. For an HDC returned by GetDC, use ReleaseDC instead.
103 ScopedHDC() : hdc_(NULL
) {}
104 explicit ScopedHDC(HDC h
) : hdc_(h
) {}
106 ~ScopedHDC() { Close(); }
108 HDC
Get() { return hdc_
; }
115 operator HDC() { return hdc_
; }
122 if (hdc_
) DeleteDC(hdc_
);
127 DISALLOW_EVIL_CONSTRUCTORS(ScopedHDC
);
130 // Like ScopedHandle but for GDI objects.
132 class ScopedGDIObject
{
134 ScopedGDIObject() : object_(NULL
) {}
135 explicit ScopedGDIObject(T object
) : object_(object
) {}
137 ~ScopedGDIObject() { Close(); }
139 T
Get() { return object_
; }
142 if (object_
&& object
!= object_
) Close();
146 ScopedGDIObject
& operator=(T object
) {
151 operator T() { return object_
; }
155 if (object_
) DeleteObject(object_
);
159 DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject
);
162 // Typedefs for some common use cases.
163 typedef ScopedGDIObject
<HBITMAP
> ScopedBitmap
;
164 typedef ScopedGDIObject
<HRGN
> ScopedHRGN
;
165 typedef ScopedGDIObject
<HFONT
> ScopedHFONT
;
167 // Like ScopedHandle except for HGLOBAL.
169 class ScopedHGlobal
{
171 explicit ScopedHGlobal(HGLOBAL glob
) : glob_(glob
) {
172 data_
= static_cast<T
*>(GlobalLock(glob_
));
174 ~ScopedHGlobal() { GlobalUnlock(glob_
); }
176 T
* get() { return data_
; }
178 size_t Size() const { return GlobalSize(glob_
); }
180 T
* operator->() const {
190 DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal
);
193 #endif // BASE_SCOPED_HANDLE_WIN_H_