Alexandria  2.22.0
Please provide a description of the 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 <mutex>
26 
27 namespace Euclid {
28 namespace FilePool {
29 
30 // Forward declaration
31 class FileHandler;
32 
38 template <typename TFD>
40  static TFD open(const boost::filesystem::path& path, bool write) {
43  "Specialization of OpenCloseTrait or a constructible(path,bool) and movable");
44  return TFD(path, write);
45  }
46  static void close(TFD& /*fd*/) {
49  "Specialization of OpenCloseTrait or a constructible(path,bool) and movable");
50  // NOOP
51  }
52 };
53 
58 class FileManager : public std::enable_shared_from_this<FileManager> {
59 public:
61  struct FileMetadata;
62 
64  using FileId = intptr_t;
65 
67  FileManager();
68 
70  virtual ~FileManager();
71 
92  std::shared_ptr<FileHandler> getFileHandler(const boost::filesystem::path& path);
93 
120  template <typename TFD>
121  std::pair<FileId, TFD> open(const boost::filesystem::path& path, bool write,
122  std::function<bool(FileId)> request_close);
123 
129  template <typename TFD>
130  void close(FileId id, TFD& fd);
131 
137  void closeAll();
138 
143  virtual void notifyUsed(FileId id);
144 
149  bool hasHandler(const boost::filesystem::path& path) const;
150 
156 
157 protected:
159  using Timestamp = Clock::time_point;
160 
162 
171 
176 
177  virtual void notifyIntentToOpen(bool write) = 0;
178  virtual void notifyOpenedFile(FileId) = 0;
179  virtual void notifyClosedFile(FileId) = 0;
180 };
181 
182 } // end of namespace FilePool
183 } // namespace Euclid
184 
185 #define FILEMANAGER_IMPL
187 #undef FILEMANAGER_IMPL
188 
189 #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:64
std::map< boost::filesystem::path, std::weak_ptr< FileHandler > > m_handlers
Definition: FileManager.h:170
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:159
std::map< FileId, std::unique_ptr< FileMetadata > > m_files
Definition: FileManager.h:175
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)
Elements::Path::Item path
static TFD open(const boost::filesystem::path &path, bool write)
Definition: FileManager.h:40