Alexandria  2.19
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) {
42  "Specialization of OpenCloseTrait or a constructible(path,bool) and movable");
43  return TFD(path, write);
44  }
45  static void close(TFD& /*fd*/) {
47  "Specialization of OpenCloseTrait or a constructible(path,bool) and movable");
48  // NOOP
49  }
50 };
51 
56 class FileManager : public std::enable_shared_from_this<FileManager> {
57 public:
59  struct FileMetadata;
60 
62  using FileId = FileMetadata*;
63 
65  FileManager();
66 
68  virtual ~FileManager();
69 
90  std::shared_ptr<FileHandler> getFileHandler(const boost::filesystem::path& path);
91 
118  template <typename TFD>
119  std::pair<FileId, TFD> open(const boost::filesystem::path& path, bool write, std::function<bool(FileId)> request_close);
120 
126  template <typename TFD>
127  void close(FileId id, TFD& fd);
128 
134  void closeAll();
135 
140  virtual void notifyUsed(FileId id);
141 
146  bool hasHandler(const boost::filesystem::path& path) const;
147 
153 
154 protected:
156  using Timestamp = Clock::time_point;
157 
159 
168 
173 
174  virtual void notifyIntentToOpen(bool write) = 0;
175  virtual void notifyOpenedFile(FileId) = 0;
176  virtual void notifyClosedFile(FileId) = 0;
177 };
178 
179 } // end of namespace FilePool
180 } // namespace Euclid
181 
182 #define FILEMANAGER_IMPL
184 #undef FILEMANAGER_IMPL
185 
186 #endif // POOLTESTS_FILEMANAGER_H
virtual void notifyUsed(FileId id)
Definition: FileManager.cpp:69
std::map< boost::filesystem::path, std::weak_ptr< FileHandler > > m_handlers
Definition: FileManager.h:167
std::shared_ptr< FileHandler > getFileHandler(const boost::filesystem::path &path)
Definition: FileManager.cpp:91
static TFD open(const boost::filesystem::path &path, bool write)
Definition: FileManager.h:40
static std::shared_ptr< FileManager > getDefault()
Definition: FileManager.cpp:59
virtual void notifyOpenedFile(FileId)=0
STL class.
virtual ~FileManager()
Destructor.
Definition: FileManager.cpp:66
FileMetadata * FileId
Opaque FileId, its concrete type should only be assumed to be copyable and hashable.
Definition: FileManager.h:62
Clock::time_point Timestamp
Definition: FileManager.h:156
virtual void notifyClosedFile(FileId)=0
std::pair< FileId, TFD > open(const boost::filesystem::path &path, bool write, std::function< bool(FileId)> request_close)
std::map< FileId, std::unique_ptr< FileMetadata > > m_files
Definition: FileManager.h:172
void close(FileId id, TFD &fd)
bool hasHandler(const boost::filesystem::path &path) const
Elements::Path::Item path
virtual void notifyIntentToOpen(bool write)=0