michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_SCOPED_HANDLE_H_ michael@0: #define BASE_SCOPED_HANDLE_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #if defined(OS_WIN) michael@0: #include "base/scoped_handle_win.h" michael@0: #endif michael@0: michael@0: class ScopedStdioHandle { michael@0: public: michael@0: ScopedStdioHandle() michael@0: : handle_(NULL) { } michael@0: michael@0: explicit ScopedStdioHandle(FILE* handle) michael@0: : handle_(handle) { } michael@0: michael@0: ~ScopedStdioHandle() { michael@0: Close(); michael@0: } michael@0: michael@0: void Close() { michael@0: if (handle_) { michael@0: fclose(handle_); michael@0: handle_ = NULL; michael@0: } michael@0: } michael@0: michael@0: FILE* get() const { return handle_; } michael@0: michael@0: FILE* Take() { michael@0: FILE* temp = handle_; michael@0: handle_ = NULL; michael@0: return temp; michael@0: } michael@0: michael@0: void Set(FILE* newhandle) { michael@0: Close(); michael@0: handle_ = newhandle; michael@0: } michael@0: michael@0: private: michael@0: FILE* handle_; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(ScopedStdioHandle); michael@0: }; michael@0: michael@0: #endif // BASE_SCOPED_HANDLE_H_