Home | History | Annotate | Download | only in memory
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_MEMORY_SCOPED_HANDLE_H_
      6 #define BASE_MEMORY_SCOPED_HANDLE_H_
      7 #pragma once
      8 
      9 #include <stdio.h>
     10 
     11 #include "base/basictypes.h"
     12 
     13 class ScopedStdioHandle {
     14  public:
     15   ScopedStdioHandle()
     16       : handle_(NULL) { }
     17 
     18   explicit ScopedStdioHandle(FILE* handle)
     19       : handle_(handle) { }
     20 
     21   ~ScopedStdioHandle() {
     22     Close();
     23   }
     24 
     25   void Close() {
     26     if (handle_) {
     27       fclose(handle_);
     28       handle_ = NULL;
     29     }
     30   }
     31 
     32   FILE* get() const { return handle_; }
     33 
     34   FILE* Take() {
     35     FILE* temp = handle_;
     36     handle_ = NULL;
     37     return temp;
     38   }
     39 
     40   void Set(FILE* newhandle) {
     41     Close();
     42     handle_ = newhandle;
     43   }
     44 
     45  private:
     46   FILE* handle_;
     47 
     48   DISALLOW_COPY_AND_ASSIGN(ScopedStdioHandle);
     49 };
     50 
     51 #endif  // BASE_MEMORY_SCOPED_HANDLE_H_
     52