Alexandria  2.19
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) {
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
Euclid::FilePool::FileManager::FileManager
FileManager()
Constructor.
Definition: FileManager.cpp:64
Euclid::FilePool::FileManager::FileId
FileMetadata * FileId
Opaque FileId, its concrete type should only be assumed to be copyable and hashable.
Definition: FileManager.h:62
std::chrono::steady_clock
Euclid::FilePool::FileManager::open
std::pair< FileId, TFD > open(const boost::filesystem::path &path, bool write, std::function< bool(FileId)> request_close)
std::shared_ptr
STL class.
std::pair
Euclid::FilePool::FileManager::notifyOpenedFile
virtual void notifyOpenedFile(FileId)=0
Euclid::FilePool::FileManager::~FileManager
virtual ~FileManager()
Destructor.
Definition: FileManager.cpp:66
Euclid::FilePool::FileManager::hasHandler
bool hasHandler(const boost::filesystem::path &path) const
Definition: FileManager.cpp:119
Euclid::FilePool::FileManager::Timestamp
Clock::time_point Timestamp
Definition: FileManager.h:156
std::function
Euclid::FilePool::OpenCloseTrait::close
static void close(TFD &)
Definition: FileManager.h:45
std::is_move_constructible
Euclid::FilePool::OpenCloseTrait
Definition: FileManager.h:39
Euclid::FilePool::OpenCloseTrait::open
static TFD open(const boost::filesystem::path &path, bool write)
Definition: FileManager.h:40
std::enable_shared_from_this
Euclid::FilePool::FileManager
Definition: FileManager.h:56
Euclid::FilePool::FileManager::closeAll
void closeAll()
Definition: FileManager.cpp:75
FileManager.icpp
std::map
STL class.
Euclid::FilePool::FileManager::m_mutex
std::mutex m_mutex
Definition: FileManager.h:158
Euclid::FilePool::FileManager::getFileHandler
std::shared_ptr< FileHandler > getFileHandler(const boost::filesystem::path &path)
Definition: FileManager.cpp:91
Euclid::FilePool::FileManager::close
void close(FileId id, TFD &fd)
Euclid::FilePool::FileManager::notifyIntentToOpen
virtual void notifyIntentToOpen(bool write)=0
std::is_constructible
std::mutex
STL class.
Euclid::FilePool::FileManager::getDefault
static std::shared_ptr< FileManager > getDefault()
Definition: FileManager.cpp:59
Euclid::FilePool::FileManager::notifyClosedFile
virtual void notifyClosedFile(FileId)=0
path
Elements::Path::Item path
Euclid::FilePool::FileManager::m_files
std::map< FileId, std::unique_ptr< FileMetadata > > m_files
Definition: FileManager.h:172
Euclid::FilePool::FileManager::m_handlers
std::map< boost::filesystem::path, std::weak_ptr< FileHandler > > m_handlers
Definition: FileManager.h:167
Euclid
Definition: InstOrRefHolder.h:29
Euclid::FilePool::FileManager::notifyUsed
virtual void notifyUsed(FileId id)
Definition: FileManager.cpp:69