blob: 43ee9754cec5a462438c71b5fd5e1ce1ca0dc174 [file] [log] [blame]
[email protected]fc29bc702010-06-04 16:13:511// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]02b2eeb2008-11-18 19:08:185#ifndef BASE_SCOPED_HANDLE_H_
6#define BASE_SCOPED_HANDLE_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commitd7cae122008-07-26 21:49:388
[email protected]9f0ba44a2008-12-23 21:33:279#include <stdio.h>
10
initial.commitd7cae122008-07-26 21:49:3811#include "base/basictypes.h"
12
[email protected]717a63d02008-11-18 17:47:0813#if defined(OS_WIN)
14#include "base/scoped_handle_win.h"
15#endif
16
17class ScopedStdioHandle {
initial.commitd7cae122008-07-26 21:49:3818 public:
[email protected]717a63d02008-11-18 17:47:0819 ScopedStdioHandle()
20 : handle_(NULL) { }
initial.commitd7cae122008-07-26 21:49:3821
[email protected]717a63d02008-11-18 17:47:0822 explicit ScopedStdioHandle(FILE* handle)
23 : handle_(handle) { }
initial.commitd7cae122008-07-26 21:49:3824
[email protected]717a63d02008-11-18 17:47:0825 ~ScopedStdioHandle() {
initial.commitd7cae122008-07-26 21:49:3826 Close();
27 }
28
initial.commitd7cae122008-07-26 21:49:3829 void Close() {
30 if (handle_) {
[email protected]717a63d02008-11-18 17:47:0831 fclose(handle_);
initial.commitd7cae122008-07-26 21:49:3832 handle_ = NULL;
33 }
34 }
35
[email protected]717a63d02008-11-18 17:47:0836 FILE* get() const { return handle_; }
initial.commitd7cae122008-07-26 21:49:3837
[email protected]717a63d02008-11-18 17:47:0838 FILE* Take() {
39 FILE* temp = handle_;
40 handle_ = NULL;
41 return temp;
initial.commitd7cae122008-07-26 21:49:3842 }
43
[email protected]717a63d02008-11-18 17:47:0844 void Set(FILE* newhandle) {
[email protected]edf2446e72008-09-22 22:40:2145 Close();
[email protected]717a63d02008-11-18 17:47:0846 handle_ = newhandle;
initial.commitd7cae122008-07-26 21:49:3847 }
48
49 private:
[email protected]717a63d02008-11-18 17:47:0850 FILE* handle_;
initial.commitd7cae122008-07-26 21:49:3851
[email protected]fc29bc702010-06-04 16:13:5152 DISALLOW_COPY_AND_ASSIGN(ScopedStdioHandle);
initial.commitd7cae122008-07-26 21:49:3853};
54
[email protected]2fdc86a2010-01-26 23:08:0255#endif // BASE_SCOPED_HANDLE_H_