Alexandria 2.31.0
SDC-CH common library for the Euclid project
|
#include <FileManager.h>
Public Types | |
using | FileId = intptr_t |
Opaque FileId, its concrete type should only be assumed to be copyable and hashable. | |
Public Member Functions | |
FileManager () | |
Constructor. | |
virtual | ~FileManager () |
Destructor. | |
std::shared_ptr< FileHandler > | getFileHandler (const boost::filesystem::path &path) |
template<typename TFD > | |
std::pair< FileId, TFD > | open (const boost::filesystem::path &path, bool write, std::function< bool(FileId)> request_close) |
template<typename TFD > | |
void | close (FileId id, TFD &fd) |
void | closeAll () |
virtual void | notifyUsed (FileId id) |
bool | hasHandler (const boost::filesystem::path &path) const |
![]() | |
T | enable_shared_from_this (T... args) |
T | operator= (T... args) |
T | shared_from_this (T... args) |
T | ~enable_shared_from_this (T... args) |
Static Public Member Functions | |
static std::shared_ptr< FileManager > | getDefault () |
Protected Types | |
using | Clock = std::chrono::steady_clock |
using | Timestamp = Clock::time_point |
Protected Member Functions | |
virtual void | notifyIntentToOpen (bool write)=0 |
virtual void | notifyOpenedFile (FileId)=0 |
virtual void | notifyClosedFile (FileId)=0 |
Protected Attributes | |
std::mutex | m_mutex |
std::map< boost::filesystem::path, std::weak_ptr< FileHandler > > | m_handlers |
std::map< FileId, std::unique_ptr< FileMetadata > > | m_files |
Provide an open/close interface to FileHandler. Concrete policies must inherit this interface and implement the notify* methods.
Definition at line 59 of file FileManager.h.
|
protected |
Definition at line 159 of file FileManager.h.
using Euclid::FilePool::FileManager::FileId = intptr_t |
Opaque FileId, its concrete type should only be assumed to be copyable and hashable.
Definition at line 65 of file FileManager.h.
|
protected |
Definition at line 160 of file FileManager.h.
Euclid::FilePool::FileManager::FileManager | ( | ) |
Constructor.
Definition at line 64 of file FileManager.cpp.
|
virtual |
Destructor.
Definition at line 66 of file FileManager.cpp.
void Euclid::FilePool::FileManager::close | ( | FileId | id, |
TFD & | fd | ||
) |
Close a file
id | The id returned by open |
Referenced by Euclid::FilePool::FileHandler::TypedFdWrapper< TFD >::close().
void Euclid::FilePool::FileManager::closeAll | ( | ) |
Close all closeable file descriptors
Definition at line 75 of file FileManager.cpp.
References std::back_inserter(), std::map< K, T >::begin(), std::map< K, T >::end(), m_files, m_mutex, and std::transform().
Referenced by Euclid::FilePool::LRUFileManager::~LRUFileManager().
|
static |
Definition at line 59 of file FileManager.cpp.
std::shared_ptr< FileHandler > Euclid::FilePool::FileManager::getFileHandler | ( | const boost::filesystem::path & | path | ) |
Get a file handler
TFD | File descriptor type |
path | File path |
If there is already a FileHandler<TFD> for the given path, this will return the same shared pointer as already in use. The FileHandler is thread-safe, so this is OK. The path is normalized (no symlinks and no '.' or '..'), so this holds true even if the same file is specified in different manners.
Elements::Exception | If there is already a FileHandler with a different file descriptor type. |
Definition at line 93 of file FileManager.cpp.
References std::weak_ptr< T >::lock(), m_handlers, m_mutex, std::enable_shared_from_this< FileManager >::shared_from_this(), and weakly_canonical().
bool Euclid::FilePool::FileManager::hasHandler | ( | const boost::filesystem::path & | path | ) | const |
Definition at line 120 of file FileManager.cpp.
References m_handlers, m_mutex, and weakly_canonical().
|
protectedpure virtual |
Implemented in Euclid::FilePool::LRUFileManager.
|
protectedpure virtual |
Implemented in Euclid::FilePool::LRUFileManager.
|
protectedpure virtual |
Implemented in Euclid::FilePool::LRUFileManager.
|
virtual |
Notify that the given file has been/is going to be used. This will update the book-keeping data used to decide what to close when.
Reimplemented in Euclid::FilePool::LRUFileManager.
Definition at line 68 of file FileManager.cpp.
References std::chrono::steady_clock::now().
std::pair< FileId, TFD > Euclid::FilePool::FileManager::open | ( | const boost::filesystem::path & | path, |
bool | write, | ||
std::function< bool(FileId)> | request_close | ||
) |
Open a file
TFD | File descriptor type. |
path | File path |
write | True if the file is to be opened in write mode |
request_close | The manager will call this function when it needs to close the file descriptor, so whoever called open can put everything in order. The callback can return "false" if the given FileId can not be closed (i.e. it is still in use). The callback is responsible for calling close. |
Why the callback? The FilePool is designed to have single ownership of the file descriptors, so it can work with non-copyable file types. Files are either owned by the FileHandler (if not in use) or by the FileAccessor (if in use). Therefore, it can only let the owner know the file should be closed. Again, this caveat is unimportant if the access is done via FileHandlers.
|
protected |
Map a file id to its metadata
Definition at line 176 of file FileManager.h.
Referenced by closeAll(), and Euclid::FilePool::LRUFileManager::notifyIntentToOpen().
|
protected |
Map path / handler
The value is a std::weak_ptr because we are not really interested on keeping a handler alive if no one is using it. However, if someone has a handler pointing to a file alive, and someone else wants a handler to the same file, it should get the same handler.
Definition at line 171 of file FileManager.h.
Referenced by getFileHandler(), and hasHandler().
|
mutableprotected |
Definition at line 162 of file FileManager.h.
Referenced by closeAll(), getFileHandler(), hasHandler(), Euclid::FilePool::LRUFileManager::notifyClosedFile(), Euclid::FilePool::LRUFileManager::notifyIntentToOpen(), Euclid::FilePool::LRUFileManager::notifyOpenedFile(), and Euclid::FilePool::LRUFileManager::notifyUsed().