summaryrefslogtreecommitdiffstats
path: root/base/scoped_handle_win.h
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-16 03:46:05 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-16 03:46:05 +0000
commitce0e7246e92f2da8e6b865dd51ae626f9867ca4f (patch)
tree8181f26fbe349c35ff37a9ae837603c3f11be7f8 /base/scoped_handle_win.h
parent21e74460f5b7cfe6f622557b20d40eecf3b8e98b (diff)
downloadchromium_src-ce0e7246e92f2da8e6b865dd51ae626f9867ca4f.zip
chromium_src-ce0e7246e92f2da8e6b865dd51ae626f9867ca4f.tar.gz
chromium_src-ce0e7246e92f2da8e6b865dd51ae626f9867ca4f.tar.bz2
Move the windows-specific scoped_* stuff from base to base/win and in the base::win namespace.
This keeps old headers that forward to the new versions and have using declarations that allow the existing code to compile. I fixed all the callers in base to use the new ones, and also the other files I happened to touch. This splits out the stuff from scoped_handle into a few separate files. I just deleted ScopedFindFile since it was only used in one place and it wasn't even really helping there. I removed StackBstr which was a #define and used the "regular" ScopedBstr in the 7 places that used it. This is an optimization to avoid an extra allocation, but none of the callers are remotely performance critical. TEST=it compiles BUG=none Review URL: http://codereview.chromium.org/3781009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62843 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/scoped_handle_win.h')
-rw-r--r--base/scoped_handle_win.h250
1 files changed, 16 insertions, 234 deletions
diff --git a/base/scoped_handle_win.h b/base/scoped_handle_win.h
index 4011a18..a98114b 100644
--- a/base/scoped_handle_win.h
+++ b/base/scoped_handle_win.h
@@ -2,237 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef BASE_SCOPED_HANDLE_WIN_H_
-#define BASE_SCOPED_HANDLE_WIN_H_
-#pragma once
-
-#include <windows.h>
-
-#include "base/basictypes.h"
-#include "base/logging.h"
-
-// Used so we always remember to close the handle.
-// The class interface matches that of ScopedStdioHandle in addition to an
-// IsValid() method since invalid handles on windows can be either NULL or
-// INVALID_HANDLE_VALUE (-1).
-//
-// Example:
-// ScopedHandle hfile(CreateFile(...));
-// if (!hfile.Get())
-// ...process error
-// ReadFile(hfile.Get(), ...);
-//
-// To sqirrel the handle away somewhere else:
-// secret_handle_ = hfile.Take();
-//
-// To explicitly close the handle:
-// hfile.Close();
-class ScopedHandle {
- public:
- ScopedHandle() : handle_(NULL) {
- }
-
- explicit ScopedHandle(HANDLE h) : handle_(NULL) {
- Set(h);
- }
-
- ~ScopedHandle() {
- Close();
- }
-
- // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
- // usage for errors.
- bool IsValid() const {
- return handle_ != NULL;
- }
-
- void Set(HANDLE new_handle) {
- Close();
-
- // Windows is inconsistent about invalid handles, so we always use NULL
- if (new_handle != INVALID_HANDLE_VALUE)
- handle_ = new_handle;
- }
-
- HANDLE Get() {
- return handle_;
- }
-
- operator HANDLE() { return handle_; }
-
- HANDLE Take() {
- // transfers ownership away from this object
- HANDLE h = handle_;
- handle_ = NULL;
- return h;
- }
-
- void Close() {
- if (handle_) {
- if (!::CloseHandle(handle_)) {
- NOTREACHED();
- }
- handle_ = NULL;
- }
- }
-
- private:
- HANDLE handle_;
- DISALLOW_COPY_AND_ASSIGN(ScopedHandle);
-};
-
-// Like ScopedHandle, but for HANDLEs returned from FindFile().
-class ScopedFindFileHandle {
- public:
- explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) {
- // Windows is inconsistent about invalid handles, so we always use NULL
- if (handle_ == INVALID_HANDLE_VALUE)
- handle_ = NULL;
- }
-
- ~ScopedFindFileHandle() {
- if (handle_)
- FindClose(handle_);
- }
-
- // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
- // usage for errors.
- bool IsValid() const { return handle_ != NULL; }
-
- operator HANDLE() { return handle_; }
-
- private:
- HANDLE handle_;
-
- DISALLOW_COPY_AND_ASSIGN(ScopedFindFileHandle);
-};
-
-// Like ScopedHandle but for HDC. Only use this on HDCs returned from
-// CreateCompatibleDC. For an HDC returned by GetDC, use ReleaseDC instead.
-class ScopedHDC {
- public:
- ScopedHDC() : hdc_(NULL) { }
- explicit ScopedHDC(HDC h) : hdc_(h) { }
-
- ~ScopedHDC() {
- Close();
- }
-
- HDC Get() {
- return hdc_;
- }
-
- void Set(HDC h) {
- Close();
- hdc_ = h;
- }
-
- operator HDC() { return hdc_; }
-
- private:
- void Close() {
-#ifdef NOGDI
- assert(false);
-#else
- if (hdc_)
- DeleteDC(hdc_);
-#endif // NOGDI
- }
-
- HDC hdc_;
- DISALLOW_COPY_AND_ASSIGN(ScopedHDC);
-};
-
-// Like ScopedHandle but for GDI objects.
-template<class T>
-class ScopedGDIObject {
- public:
- ScopedGDIObject() : object_(NULL) {}
- explicit ScopedGDIObject(T object) : object_(object) {}
-
- ~ScopedGDIObject() {
- Close();
- }
-
- T Get() {
- return object_;
- }
-
- void Set(T object) {
- if (object_ && object != object_)
- Close();
- object_ = object;
- }
-
- ScopedGDIObject& operator=(T object) {
- Set(object);
- return *this;
- }
-
- T release() {
- T object = object_;
- object_ = NULL;
- return object;
- }
-
- operator T() { return object_; }
-
- private:
- void Close() {
- if (object_)
- DeleteObject(object_);
- }
-
- T object_;
- DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
-};
-
-// An explicit specialization for HICON because we have to call DestroyIcon()
-// instead of DeleteObject() for HICON.
-template<>
-void ScopedGDIObject<HICON>::Close() {
- if (object_)
- DestroyIcon(object_);
-}
-
-// Typedefs for some common use cases.
-typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
-typedef ScopedGDIObject<HRGN> ScopedRegion;
-typedef ScopedGDIObject<HFONT> ScopedHFONT;
-typedef ScopedGDIObject<HICON> ScopedHICON;
-
-// Like ScopedHandle except for HGLOBAL.
-template<class T>
-class ScopedHGlobal {
- public:
- explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) {
- data_ = static_cast<T*>(GlobalLock(glob_));
- }
- ~ScopedHGlobal() {
- GlobalUnlock(glob_);
- }
-
- T* get() { return data_; }
-
- size_t Size() const { return GlobalSize(glob_); }
-
- T* operator->() const {
- assert(data_ != 0);
- return data_;
- }
-
- T* release() {
- T* data = data_;
- data_ = NULL;
- return data;
- }
-
- private:
- HGLOBAL glob_;
-
- T* data_;
-
- DISALLOW_COPY_AND_ASSIGN(ScopedHGlobal);
-};
-
-#endif // BASE_SCOPED_HANDLE_WIN_H_
+// TODO(brettw) remove this file when all callers are converted to using the
+// new location/namespace
+#include "base/win/scoped_handle.h"
+#include "base/win/scoped_gdi_object.h"
+#include "base/win/scoped_handle.h"
+#include "base/win/scoped_hdc.h"
+#include "base/win/scoped_hglobal.h"
+
+using base::win::ScopedBitmap;
+using base::win::ScopedGDIObject;
+using base::win::ScopedHandle;
+using base::win::ScopedHDC;
+using base::win::ScopedHFONT;
+using base::win::ScopedHGlobal;
+using base::win::ScopedHICON;
+using base::win::ScopedRegion;