scoped_handle.h (1057B)
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. 6 7 #ifndef BASE_SCOPED_HANDLE_H_ 8 #define BASE_SCOPED_HANDLE_H_ 9 10 #include <stdio.h> 11 12 #include "base/basictypes.h" 13 14 #if defined(XP_WIN) 15 # include "base/scoped_handle_win.h" 16 #endif 17 18 class ScopedStdioHandle { 19 public: 20 ScopedStdioHandle() : handle_(NULL) {} 21 22 explicit ScopedStdioHandle(FILE* handle) : handle_(handle) {} 23 24 ~ScopedStdioHandle() { Close(); } 25 26 void Close() { 27 if (handle_) { 28 fclose(handle_); 29 handle_ = NULL; 30 } 31 } 32 33 FILE* get() const { return handle_; } 34 35 FILE* Take() { 36 FILE* temp = handle_; 37 handle_ = NULL; 38 return temp; 39 } 40 41 void Set(FILE* newhandle) { 42 Close(); 43 handle_ = newhandle; 44 } 45 46 private: 47 FILE* handle_; 48 49 DISALLOW_EVIL_CONSTRUCTORS(ScopedStdioHandle); 50 }; 51 52 #endif // BASE_SCOPED_HANDLE_H_