Alexandria  2.25.0
SDC-CH common library for the Euclid project
FileManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef POOLTESTS_FILEMANAGER_H
20 #define POOLTESTS_FILEMANAGER_H
21 
22 #include <boost/filesystem/path.hpp>
23 #include <list>
24 #include <map>
25 #include <memory>
26 #include <mutex>
27 
28 namespace Euclid {
29 namespace FilePool {
30 
31 // Forward declaration
32 class FileHandler;
33 
39 template <typename TFD>
41  static TFD open(const boost::filesystem::path& path, bool write) {
44  "Specialization of OpenCloseTrait or a constructible(path,bool) and movable");
45  return TFD(path, write);
46  }
47  static void close(TFD& /*fd*/) {
50  "Specialization of OpenCloseTrait or a constructible(path,bool) and movable");
51  // NOOP
52  }
53 };
54 
59 class FileManager : public std::enable_shared_from_this<FileManager> {
60 public:
62  struct FileMetadata;
63 
65  using FileId = intptr_t;
66 
68  FileManager();
69 
71  virtual ~FileManager();
72 
93  std::shared_ptr<FileHandler> getFileHandler(const boost::filesystem::path& path);
94 
121  template <typename TFD>
122  std::pair<FileId, TFD> open(const boost::filesystem::path& path, bool write,
123  std::function<bool(FileId)> request_close);
124 
130  template <typename TFD>
131  void close(FileId id, TFD& fd);
132 
138  void closeAll();
139 
144  virtual void notifyUsed(FileId id);
145 
150  bool hasHandler(const boost::filesystem::path& path) const;
151 
157 
158 protected:
160  using Timestamp = Clock::time_point;
161 
163 
172 
177 
178  virtual void notifyIntentToOpen(bool write) = 0;
179  virtual void notifyOpenedFile(FileId) = 0;
180  virtual void notifyClosedFile(FileId) = 0;
181 };
182 
183 } // end of namespace FilePool
184 } // namespace Euclid
185 
186 #define FILEMANAGER_IMPL
188 #undef FILEMANAGER_IMPL
189 
190 #endif // POOLTESTS_FILEMANAGER_H
virtual ~FileManager()
Destructor.
Definition: FileManager.cpp:66
intptr_t FileId
Opaque FileId, its concrete type should only be assumed to be copyable and hashable.
Definition: FileManager.h:65
std::map< boost::filesystem::path, std::weak_ptr< FileHandler > > m_handlers
Definition: FileManager.h:171
static std::shared_ptr< FileManager > getDefault()
Definition: FileManager.cpp:59
virtual void notifyOpenedFile(FileId)=0
virtual void notifyUsed(FileId id)
Definition: FileManager.cpp:68
Clock::time_point Timestamp
Definition: FileManager.h:160
std::map< FileId, std::unique_ptr< FileMetadata > > m_files
Definition: FileManager.h:176
void close(FileId id, TFD &fd)
std::shared_ptr< FileHandler > getFileHandler(const boost::filesystem::path &path)
Definition: FileManager.cpp:93
virtual void notifyIntentToOpen(bool write)=0
bool hasHandler(const boost::filesystem::path &path) const
virtual void notifyClosedFile(FileId)=0
std::pair< FileId, TFD > open(const boost::filesystem::path &path, bool write, std::function< bool(FileId)> request_close)
Path::Item path
static TFD open(const boost::filesystem::path &path, bool write)
Definition: FileManager.h:41