diff options
author | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-18 17:47:08 +0000 |
---|---|---|
committer | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-18 17:47:08 +0000 |
commit | 717a63d05d675240b77b0cc6eb5acbdf1840e112 (patch) | |
tree | f1985e55623f1c88172f06d39cabd3f657410700 /base/scoped_handle.h | |
parent | 3a9eb83a621e991d593b9d02d377db75a0272f29 (diff) | |
download | chromium_src-717a63d05d675240b77b0cc6eb5acbdf1840e112.zip chromium_src-717a63d05d675240b77b0cc6eb5acbdf1840e112.tar.gz chromium_src-717a63d05d675240b77b0cc6eb5acbdf1840e112.tar.bz2 |
Add scoped stdio handles.
Since file_util uses stdio handles, we'll need this. Specifically, _ph
has a review out at the moment which requires them.
Review URL: http://codereview.chromium.org/11422
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5612 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/scoped_handle.h')
-rw-r--r-- | base/scoped_handle.h | 203 |
1 files changed, 23 insertions, 180 deletions
diff --git a/base/scoped_handle.h b/base/scoped_handle.h index 8ed568d..616a37d 100644 --- a/base/scoped_handle.h +++ b/base/scoped_handle.h @@ -2,208 +2,51 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef BASE_SCOPED_HANDLE_H__ -#define BASE_SCOPED_HANDLE_H__ - -#include <windows.h> +#ifndef BASE_SCOPED_HANDLE_FIXME_H__ +#define BASE_SCOPED_HANDLE_FIXME_H__ #include "base/basictypes.h" -#include "base/logging.h" -// Used so we always remember to close the handle. 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: -// CloseHandle(hfile.Take()); -class ScopedHandle { - public: - ScopedHandle() : handle_(NULL) { - } - - explicit ScopedHandle(HANDLE h) : handle_(NULL) { - Set(h); - } +#if defined(OS_WIN) +#include "base/scoped_handle_win.h" +#endif - ~ScopedHandle() { - Close(); - } +class ScopedStdioHandle { + public: + ScopedStdioHandle() + : handle_(NULL) { } - // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL - // usage for errors. - bool IsValid() const { - return handle_ != NULL; - } + explicit ScopedStdioHandle(FILE* handle) + : handle_(handle) { } - void Set(HANDLE new_handle) { + ~ScopedStdioHandle() { 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; - } - - private: void Close() { if (handle_) { - if (!::CloseHandle(handle_)) { - NOTREACHED(); - } + fclose(handle_); handle_ = NULL; } } - HANDLE handle_; - DISALLOW_EVIL_CONSTRUCTORS(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_EVIL_CONSTRUCTORS(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_; - } + FILE* get() const { return handle_; } - void Set(HDC h) { - Close(); - hdc_ = h; - } - - operator HDC() { return hdc_; } - - private: - void Close() { - if (hdc_) - DeleteDC(hdc_); + FILE* Take() { + FILE* temp = handle_; + handle_ = NULL; + return temp; } - HDC hdc_; - DISALLOW_EVIL_CONSTRUCTORS(ScopedHDC); -}; - -// Like ScopedHandle but for GDI objects. -template<class T> -class ScopedGDIObject { - public: - ScopedGDIObject() : object_(NULL) {} - explicit ScopedGDIObject(T object) : object_(object) {} - - ~ScopedGDIObject() { + void Set(FILE* newhandle) { Close(); - } - - T Get() { - return object_; - } - - void Set(T object) { - if (object_ && object != object_) - Close(); - object_ = object; - } - - ScopedGDIObject& operator=(T object) { - Set(object); - return *this; - } - - operator T() { return object_; } - - private: - void Close() { - if (object_) - DeleteObject(object_); - } - - T object_; - DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject); -}; - -// Typedefs for some common use cases. -typedef ScopedGDIObject<HBITMAP> ScopedBitmap; -typedef ScopedGDIObject<HRGN> ScopedHRGN; -typedef ScopedGDIObject<HFONT> ScopedHFONT; - - -// 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_; + handle_ = newhandle; } private: - HGLOBAL glob_; + FILE* handle_; - T* data_; - - DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal); + DISALLOW_EVIL_CONSTRUCTORS(ScopedStdioHandle); }; -#endif // BASE_SCOPED_HANDLE_H__ - +#endif // BASE_SCOPED_HANDLE_FIXME_H__ |