ROOT  6.06/09
Reference Guide
TFile.h
Go to the documentation of this file.
1 /// \file TFile.h
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 #ifndef ROOT7_TFile
16 #define ROOT7_TFile
17 
18 #include "ROOT/TDirectory.h"
19 
20 #include <memory>
21 #include <experimental/string_view>
22 
23 namespace ROOT {
24 
25 namespace Internal {
26 /** \class TFileImplBase
27  Base class for storage-specific ROOT file implementations.
28 
29  A class deriving from `TFileImplBase` is an object store: it can serialize any
30  object for which ROOT I/O is available (generally: an object which has a
31  dictionary), and it stores the object's data under a key name.
32 
33  A `TFileImplBase` stores whatever was added to it as a `TDirectory`, when the
34  `TFileImplBase` object is destructed. It can store non-lifetime managed objects
35  by passing them to `Save()`.
36 
37  */
38 class TFileImplBase: public TDirectory {
39 public:
40  ~TFileImplBase() = default;
41 
42  /// Save all objects associated with this directory to the storage medium.
43  virtual void Flush() = 0;
44 
45  /// Flush() and make the file non-writable: close it.
46  virtual void Close() = 0;
47 
48  template <class T>
49  void Write(const std::string& /*name*/, const T& /*ptr*/) {}
50 
51 };
52 }
53 
54 
55 /**
56  \class TFilePtr
57  \brief Points to an object that stores or reads objects in ROOT's binary
58  format.
59 
60  */
61 
62 class TFilePtr {
64 
65 public:
66  ///\name Generator functions
67  ///\{
68 
69  /// Open a file with `name` for reading.
70  static TFilePtr OpenForRead(std::string_view name);
71 
72  /// Open an existing file with `name` for reading and writing. If a file with
73  /// that name does not exist, an invalid TFilePtr will be returned.
74  static TFilePtr OpenForUpdate(std::string_view name);
75 
76  /// Open a file with `name` for reading and writing. Fail (return an invalid
77  /// `TFilePtr`) if a file with this name already exists.
78  static TFilePtr Create(std::string_view name);
79 
80  /// Open a file with `name` for reading and writing. If a file with this name
81  /// already exists, delete it and create a new one. Else simply create a new file.
82  static TFilePtr Recreate(std::string_view name);
83 
84  ///\}
85 
86  /// Dereference the file pointer, giving access to the TFileImplBase object.
87  Internal::TFileImplBase* operator ->() { return fImpl.Get(); }
88 
89  /// Dereference the file pointer, giving access to the TFileImplBase object.
90  /// const overload.
91  const Internal::TFileImplBase* operator ->() const { return fImpl.Get(); }
92 
93  /// Check the validity of the file pointer.
94  operator bool() const { return fImpl; }
95 
96 private:
97  /// Constructed by
98  TFilePtr(TCoopPtr<Internal::TFileImplBase>);
99 };
100 }
101 #endif
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
double T(double x)
Definition: ChebyshevPol.h:34
TCoopPtr< Internal::TFileImplBase > fImpl
Definition: TFile.h:63
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
void Write(const std::string &, const T &)
Definition: TFile.h:49
virtual void Close()=0
Flush() and make the file non-writable: close it.
static TFilePtr OpenForUpdate(std::string_view name)
Open an existing file with name for reading and writing.
Definition: TFile.cxx:87
virtual void Flush()=0
Save all objects associated with this directory to the storage medium.
#define name(a, b)
Definition: linkTestLib0.cpp:5
Internal::TFileImplBase * operator->()
Dereference the file pointer, giving access to the TFileImplBase object.
Definition: TFile.h:87
static TFilePtr Recreate(std::string_view name)
Open a file with name for reading and writing.
Definition: TFile.cxx:83
Pointer_t Get() const
Get the raw pointer.
Definition: TCoopPtr.h:90
Several pointers point to the same object, any of them can delete the object, setting all of them to ...
Definition: TCoopPtr.h:46