Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFile.hxx
Go to the documentation of this file.
1/// \file ROOT/RFile.hxx
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
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT7_RFile
17#define ROOT7_RFile
18
19#include "ROOT/RDirectory.hxx"
20#include "ROOT/RMakeUnique.hxx"
21#include "ROOT/RStringView.hxx"
22
23#include "TClass.h"
24#include <memory>
25
26namespace ROOT {
27namespace Experimental {
28
29class RFilePtr;
30
31namespace Internal {
32class RFileStorageInterface;
33class RFileSharedPtrCtor;
34} // namespace Internal
35
36/** \class ROOT::Experimental::RFile
37 A ROOT file.
38
39 A ROOT file is an object store: it can serialize any
40 object for which ROOT I/O is available (generally: an object which has a
41 dictionary), and it stores the object's data under a key name.
42
43 */
44class RFile: public RDirectory {
45private:
46 std::unique_ptr<Internal::RFileStorageInterface> fStorage; ///< Storage backend.
47
48 RFile(std::unique_ptr<Internal::RFileStorageInterface> &&storage);
49
50 /// Serialize the object at address, using the object's TClass.
51 // FIXME: what about `cl` "pointing" to a base class?
52 void WriteMemoryWithType(std::string_view name, const void *address, TClass *cl);
53
54 friend Internal::RFileSharedPtrCtor;
55
56public:
57 /// Options for RFile construction.
58 struct Options_t {
59 /// Default constructor needed for member inits.
61
62 /// Whether the file should be opened asynchronously, if available.
63 bool fAsynchronousOpen = false;
64
65 /// Timeout for asynchronous opening.
67
68 /// Whether the file should be cached before reading. Only available for
69 /// "remote" file protocols. If the download fails, the file will be opened
70 /// remotely.
71 bool fCachedRead = false;
72
73 /// Where to cache the file. If empty, defaults to RFilePtr::GetCacheDir().
74 std::string fCacheDir;
75 };
76
77 ///\name Generator functions
78 ///\{
79
80 /// Open a file with `name` for reading.
81 ///
82 /// \note: Synchronizes multi-threaded accesses through locks.
83 static RFilePtr Open(std::string_view name, const Options_t &opts = Options_t());
84
85 /// Open an existing file with `name` for reading and writing. If a file with
86 /// that name does not exist, an invalid RFilePtr will be returned.
87 ///
88 /// \note: Synchronizes multi-threaded accesses through locks.
89 static RFilePtr OpenForUpdate(std::string_view name, const Options_t &opts = Options_t());
90
91 /// Open a file with `name` for reading and writing. Fail (return an invalid
92 /// `RFilePtr`) if a file with this name already exists.
93 ///
94 /// \note: Synchronizes multi-threaded accesses through locks.
95 static RFilePtr Create(std::string_view name, const Options_t &opts = Options_t());
96
97 /// Open a file with `name` for reading and writing. If a file with this name
98 /// already exists, delete it and create a new one. Else simply create a new file.
99 ///
100 /// \note: Synchronizes multi-threaded accesses through locks.
101 static RFilePtr Recreate(std::string_view name, const Options_t &opts = Options_t());
102
103 ///\}
104
105 /// Set the new directory used for cached reads, returns the old directory.
106 ///
107 /// \note: Synchronizes multi-threaded accesses through locks.
108 static std::string SetCacheDir(std::string_view path);
109
110 /// Get the directory used for cached reads.
111 static std::string GetCacheDir();
112
113 /// Must not call Write() of all attached objects:
114 /// some might not be needed to be written or writing might be aborted due to
115 /// an exception; require explicit Write().
117
118 /// Save all objects associated with this directory (including file header) to
119 /// the storage medium.
120 void Flush();
121
122 /// Flush() and make the file non-writable: close it.
123 void Close();
124
125 /// Read the object for a key. `T` must be the object's type.
126 /// This will re-read the object for each call, returning a new copy; whether
127 /// the `RDirectory` is managing an object attached to this key or not.
128 /// \returns a `unique_ptr` to the object.
129 /// \throws RDirectoryUnknownKey if no object is stored under this name.
130 /// \throws RDirectoryTypeMismatch if the object stored under this name is of
131 /// a type different from `T`.
132 template <class T>
133 std::unique_ptr<T> Read(std::string_view name)
134 {
135 // FIXME: need separate collections for a RDirectory's key/value and registered objects. Here, we want to emit a
136 // read and must look through the key/values without attaching an object to the RDirectory.
137 // FIXME: do not register read object in RDirectory
138 // FIXME: implement actual read
139 // FIXME: for now, copy out of whatever the RDirectory manages.
140 return std::make_unique<T>(*Get<T>(name));
141 }
142
143 /// Write an object that is not lifetime managed by this RFileImplBase.
144 template <class T>
145 void Write(std::string_view name, const T &obj)
146 {
147 WriteMemoryWithType(name, &obj, TClass::GetClass<T>());
148 }
149
150 /// Write an object that is not lifetime managed by this RFileImplBase.
151 template <class T>
152 void Write(std::string_view name, const T *obj)
153 {
154 WriteMemoryWithType(name, obj, TClass::GetClass<T>());
155 }
156
157 /// Write an object that is already lifetime managed by this RFileImplBase.
158 void Write(std::string_view name)
159 {
160 auto dep = Find(name);
161 WriteMemoryWithType(name, dep.GetPointer().get(), dep.GetType());
162 }
163
164 /// Hand over lifetime management of an object to this RFileImplBase, and
165 /// write it.
166 template <class T>
167 void Write(std::string_view name, std::shared_ptr<T> &&obj)
168 {
169 Add(name, obj);
170 // FIXME: use an iterator from the insertion to write instead of a second name lookup.
171 Write(name);
172 }
173};
174
175/**
176 \class RFilePtr
177 \brief Points to an object that stores or reads objects in ROOT's binary
178 format.
179
180 FIXME: implement async open; likely using std::future, possibly removing the
181 Option_t element.
182
183 */
184
185class RFilePtr {
186private:
187 std::shared_ptr<RFile> fFile;
188
189 /// Constructed by Open etc.
190 RFilePtr(std::shared_ptr<RFile> &&);
191
192 friend class RFile;
193
194public:
195 /// Dereference the file pointer, giving access to the RFileImplBase object.
196 RFile *operator->() { return fFile.get(); }
197
198 /// Dereference the file pointer, giving access to the RFileImplBase object.
199 /// const overload.
200 const RFile *operator->() const { return fFile.get(); }
201
202 /// Check the validity of the file pointer.
203 operator bool() const { return fFile.get(); }
204};
205
206} // namespace Experimental
207} // namespace ROOT
208#endif
char name[80]
Definition TGX11.cxx:110
Key/value store of objects.
Internal::RDirectoryEntry Find(std::string_view name) const
Find the RDirectoryEntry associated to the name.
void Add(std::string_view name, const std::shared_ptr< T > &ptr)
Add an existing object (rather a shared_ptr to it) to the RDirectory.
Points to an object that stores or reads objects in ROOT's binary format.
Definition RFile.hxx:185
RFile * operator->()
Dereference the file pointer, giving access to the RFileImplBase object.
Definition RFile.hxx:196
const RFile * operator->() const
Dereference the file pointer, giving access to the RFileImplBase object.
Definition RFile.hxx:200
std::shared_ptr< RFile > fFile
Definition RFile.hxx:187
static RFilePtr Recreate(std::string_view name, const Options_t &opts=Options_t())
Open a file with name for reading and writing.
Definition RFile.cxx:171
void Flush()
Save all objects associated with this directory (including file header) to the storage medium.
Definition RFile.cxx:214
static std::string SetCacheDir(std::string_view path)
Set the new directory used for cached reads, returns the old directory.
Definition RFile.cxx:189
void Write(std::string_view name)
Write an object that is already lifetime managed by this RFileImplBase.
Definition RFile.hxx:158
void Write(std::string_view name, const T *obj)
Write an object that is not lifetime managed by this RFileImplBase.
Definition RFile.hxx:152
static std::string GetCacheDir()
Get the directory used for cached reads.
Definition RFile.cxx:198
std::unique_ptr< T > Read(std::string_view name)
Read the object for a key.
Definition RFile.hxx:133
~RFile()
Must not call Write() of all attached objects: some might not be needed to be written or writing migh...
std::unique_ptr< Internal::RFileStorageInterface > fStorage
Storage backend.
Definition RFile.hxx:46
static RFilePtr Open(std::string_view name, const Options_t &opts=Options_t())
Open a file with name for reading.
Definition RFile.cxx:153
void Write(std::string_view name, std::shared_ptr< T > &&obj)
Hand over lifetime management of an object to this RFileImplBase, and write it.
Definition RFile.hxx:167
static RFilePtr OpenForUpdate(std::string_view name, const Options_t &opts=Options_t())
Open an existing file with name for reading and writing.
Definition RFile.cxx:180
void WriteMemoryWithType(std::string_view name, const void *address, TClass *cl)
Serialize the object at address, using the object's TClass.
Definition RFile.cxx:222
static RFilePtr Create(std::string_view name, const Options_t &opts=Options_t())
Open a file with name for reading and writing.
Definition RFile.cxx:162
void Close()
Flush() and make the file non-writable: close it.
Definition RFile.cxx:218
void Write(std::string_view name, const T &obj)
Write an object that is not lifetime managed by this RFileImplBase.
Definition RFile.hxx:145
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Options for RFile construction.
Definition RFile.hxx:58
bool fCachedRead
Whether the file should be cached before reading.
Definition RFile.hxx:71
int fAsyncTimeout
Timeout for asynchronous opening.
Definition RFile.hxx:66
Options_t()
Default constructor needed for member inits.
Definition RFile.hxx:60
std::string fCacheDir
Where to cache the file. If empty, defaults to RFilePtr::GetCacheDir().
Definition RFile.hxx:74
bool fAsynchronousOpen
Whether the file should be opened asynchronously, if available.
Definition RFile.hxx:63