blob: 232d83e097de2efbc4f1bb1aa430f1682252c8cd [file] [log] [blame]
[email protected]b90d7e802011-01-09 16:32:201// Copyright (c) 2011 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]3b63f8f42011-03-28 01:54:155#ifndef BASE_MEMORY_SCOPED_HANDLE_H_
6#define BASE_MEMORY_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:0813class ScopedStdioHandle {
initial.commitd7cae122008-07-26 21:49:3814 public:
[email protected]717a63d02008-11-18 17:47:0815 ScopedStdioHandle()
16 : handle_(NULL) { }
initial.commitd7cae122008-07-26 21:49:3817
[email protected]717a63d02008-11-18 17:47:0818 explicit ScopedStdioHandle(FILE* handle)
19 : handle_(handle) { }
initial.commitd7cae122008-07-26 21:49:3820
[email protected]717a63d02008-11-18 17:47:0821 ~ScopedStdioHandle() {
initial.commitd7cae122008-07-26 21:49:3822 Close();
23 }
24
initial.commitd7cae122008-07-26 21:49:3825 void Close() {
26 if (handle_) {
[email protected]717a63d02008-11-18 17:47:0827 fclose(handle_);
initial.commitd7cae122008-07-26 21:49:3828 handle_ = NULL;
29 }
30 }
31
[email protected]717a63d02008-11-18 17:47:0832 FILE* get() const { return handle_; }
initial.commitd7cae122008-07-26 21:49:3833
[email protected]717a63d02008-11-18 17:47:0834 FILE* Take() {
35 FILE* temp = handle_;
36 handle_ = NULL;
37 return temp;
initial.commitd7cae122008-07-26 21:49:3838 }
39
[email protected]717a63d02008-11-18 17:47:0840 void Set(FILE* newhandle) {
[email protected]edf2446e72008-09-22 22:40:2141 Close();
[email protected]717a63d02008-11-18 17:47:0842 handle_ = newhandle;
initial.commitd7cae122008-07-26 21:49:3843 }
44
45 private:
[email protected]717a63d02008-11-18 17:47:0846 FILE* handle_;
initial.commitd7cae122008-07-26 21:49:3847
[email protected]fc29bc702010-06-04 16:13:5148 DISALLOW_COPY_AND_ASSIGN(ScopedStdioHandle);
initial.commitd7cae122008-07-26 21:49:3849};
50
[email protected]3b63f8f42011-03-28 01:54:1551#endif // BASE_MEMORY_SCOPED_HANDLE_H_