Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4139)

Unified Diff: base/win/scoped_gdi_object.h

Issue 3781009: Move the windows-specific scoped_* stuff from base to base/win and in the bas... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/win/scoped_gdi_object.h
===================================================================
--- base/win/scoped_gdi_object.h (revision 62694)
+++ base/win/scoped_gdi_object.h (working copy)
@@ -2,8 +2,8 @@
// 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_
+#ifndef BASE_WIN_SCOPED_GDI_OBJECT_H_
+#define BASE_WIN_SCOPED_GDI_OBJECT_H_
#pragma once
#include <windows.h>
@@ -11,138 +11,9 @@
#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) {
- }
+namespace base {
+namespace win {
- 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 {
@@ -201,38 +72,7 @@
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_);
- }
+} // namespace win
+} // namespace base
- 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_
+#endif // BASE_WIN_SCOPED_GDI_OBJECT_H_

Powered by Google App Engine
This is Rietveld 408576698