ROOT  6.06/09
Reference Guide
TFile.cxx
Go to the documentation of this file.
1 /// \file TFile.cxx
2 /// \ingroup Base ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-07-31
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
6 
7 /*************************************************************************
8  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
9  * All rights reserved. *
10  * *
11  * For the licensing terms see $ROOTSYS/LICENSE. *
12  * For the list of contributors see $ROOTSYS/README/CREDITS. *
13  *************************************************************************/
14 
15 #include "ROOT/TFile.h"
16 #include "TFile.h"
17 
18 #include <mutex>
19 
21  static TDirectory heapDir;
22  return heapDir;
23 }
24 
25 namespace {
26 /// We cannot afford users not closing their files. Yes, we return a unique_ptr -
27 /// but that might be stored in an object that itself leaks. That would leave
28 /// the TFile unclosed and data corrupted / not written. Instead, keep a
29 /// collection of all opened writable TFiles and close them at destruction time,
30 /// explicitly.
31 static void AddFilesToClose(ROOT::TCoopPtr<ROOT::Internal::TFileImplBase> pFile) {
32  struct CloseFiles_t {
33  std::vector<ROOT::TCoopPtr<ROOT::Internal::TFileImplBase>> fFiles;
34  std::mutex fMutex;
35  ~CloseFiles_t() {
36  for (auto& pFile: fFiles)
37  if (pFile)
38  pFile->Flush(); // or Close()? but what if there's still a Write()?
39  }
40  };
41  static CloseFiles_t closer;
42 
43  std::lock_guard<std::mutex> lock(closer.fMutex);
44  closer.fFiles.emplace_back(pFile);
45 }
46 
47 /** \class TFSFile
48  TFileImplBase for a file-system (POSIX) style TFile.
49  */
50 class TFileSystemFile: public ROOT::Internal::TFileImplBase {
51  ::TFile* fOldFile;
52 
53 public:
54  TFileSystemFile(const std::string& name, const char* mode):
55  fOldFile(::TFile::Open(name.c_str(), mode)) {
56  }
57 
58  void Flush() final { fOldFile->Flush(); }
59 
60  void Close() final { fOldFile->Close(); }
61 
62  ~TFileSystemFile() {
63  delete fOldFile;
64  }
65 };
66 }
67 
69 fImpl(impl)
70 {
71  AddFilesToClose(impl);
72 }
73 
74 
76  // will become delegation to TFileSystemFile, TWebFile etc.
77  return TFilePtr(MakeCoop<TFileSystemFile>(name.to_string(), "READ"));
78 }
80  // will become delegation to TFileSystemFile, TWebFile etc.
81  return TFilePtr(MakeCoop<TFileSystemFile>(name.to_string(), "CREATE"));
82 }
84  // will become delegation to TFileSystemFile, TWebFile etc.
85  return TFilePtr(MakeCoop<TFileSystemFile>(name.to_string(), "RECREATE"));
86 }
88  // will become delegation to TFileSystemFile, TWebFile etc.
89  return TFilePtr(MakeCoop<TFileSystemFile>(name.to_string(), "UPDATE"));
90 }
virtual void Flush()
Synchronize a file's in-memory and on-disk states.
Definition: TFile.cxx:1080
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
TFilePtr(TCoopPtr< Internal::TFileImplBase >)
Constructed by.
Definition: TFile.cxx:68
Base class for storage-specific ROOT file implementations.
Definition: TFile.h:38
static TFilePtr Create(std::string_view name)
Open a file with name for reading and writing.
Definition: TFile.cxx:79
Key/value store of objects.
Definition: TDirectory.h:42
static TFilePtr OpenForRead(std::string_view name)
Open a file with name for reading.
Definition: TFile.cxx:75
Points to an object that stores or reads objects in ROOT's binary format.
Definition: TFile.h:62
static TDirectory & Heap()
Dedicated, process-wide TDirectory.
Definition: TFile.cxx:20
static TFilePtr OpenForUpdate(std::string_view name)
Open an existing file with name for reading and writing.
Definition: TFile.cxx:87
#define name(a, b)
Definition: linkTestLib0.cpp:5
static TFilePtr Recreate(std::string_view name)
Open a file with name for reading and writing.
Definition: TFile.cxx:83
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898
Several pointers point to the same object, any of them can delete the object, setting all of them to ...
Definition: TCoopPtr.h:46