Alexandria  2.19
Please provide a description of the project.
FileHandler.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_FILEHANDLER_H
20 #define POOLTESTS_FILEHANDLER_H
21 
22 #include "FileAccessor.h"
23 #include "FileManager.h"
24 #include <boost/filesystem/path.hpp>
25 #include <list>
26 
27 namespace Euclid {
28 namespace FilePool {
29 
39 class FileHandler {
40 public:
42  enum Mode { kRead = 0, kWrite = 1, kTry = 2, kTryRead = kTry, kTryWrite = kTry | kWrite };
43 
45  virtual ~FileHandler();
46 
56  template <typename TFD>
58 
60  bool isReadOnly() const;
61 
62 private:
63  friend class FileManager;
64 
68 
69  struct FdWrapper {
70  virtual ~FdWrapper() = default;
71 
72  virtual void close() = 0;
73  };
74 
75  template <typename TFD>
76  struct TypedFdWrapper : public FdWrapper {
78  TFD m_fd;
80 
82  : m_id(id), m_fd(std::move(fd)), m_file_manager(manager) {}
83 
84  void close() final {
86  }
87  };
88 
90  boost::filesystem::path m_path;
95 
105  FileHandler(const boost::filesystem::path& path, std::weak_ptr<FileManager> file_manager);
106 
114  bool close(FileManager::FileId id);
115 
116  template <typename TFD>
118 
119  template <typename TFD>
121 };
122 
123 } // namespace FilePool
124 } // namespace Euclid
125 
126 #define FILEHANDLER_IMPL
128 #undef FILEHANDLER_IMPL
129 
130 #endif // POOLTESTS_FILEHANDLER_H
Euclid::FilePool::FileManager::FileId
FileMetadata * FileId
Opaque FileId, its concrete type should only be assumed to be copyable and hashable.
Definition: FileManager.h:62
Euclid::FilePool::FileAccessorBase::UniqueLock
boost::unique_lock< SharedMutex > UniqueLock
Definition: FileAccessor.h:34
Euclid::FilePool::FileHandler::~FileHandler
virtual ~FileHandler()
Destructor.
Definition: FileHandler.cpp:27
Euclid::FilePool::FileHandler
Definition: FileHandler.h:39
FileHandler.icpp
Euclid::FilePool::FileAccessorBase::SharedMutex
boost::shared_mutex SharedMutex
Definition: FileAccessor.h:32
Euclid::FilePool::FileHandler::m_file_manager
std::weak_ptr< FileManager > m_file_manager
Definition: FileHandler.h:91
Euclid::FilePool::FileHandler::close
bool close(FileManager::FileId id)
Definition: FileHandler.cpp:37
Euclid::FilePool::FileHandler::UniqueLock
typename FileAccessorBase::UniqueLock UniqueLock
Definition: FileHandler.h:67
Euclid::FilePool::FileHandler::SharedMutex
typename FileAccessorBase::SharedMutex SharedMutex
Definition: FileHandler.h:65
Euclid::FilePool::FileHandler::FdWrapper
Definition: FileHandler.h:69
Euclid::FilePool::FileHandler::kTry
@ kTry
Definition: FileHandler.h:42
Euclid::FilePool::FileHandler::FdWrapper::close
virtual void close()=0
Euclid::FilePool::FileHandler::kTryWrite
@ kTryWrite
Definition: FileHandler.h:42
Euclid::FilePool::FileHandler::m_available_fd
std::map< FileManager::FileId, std::unique_ptr< FdWrapper > > m_available_fd
Definition: FileHandler.h:93
Euclid::FilePool::FileHandler::Mode
Mode
Open modes.
Definition: FileHandler.h:42
Euclid::FilePool::FileHandler::m_path
boost::filesystem::path m_path
Definition: FileHandler.h:90
Euclid::FilePool::FileHandler::TypedFdWrapper::m_fd
TFD m_fd
Definition: FileHandler.h:78
FileAccessor.h
Euclid::FilePool::FileHandler::SharedLock
typename FileAccessorBase::SharedLock SharedLock
Definition: FileHandler.h:66
Euclid::FilePool::FileHandler::m_handler_mutex
std::mutex m_handler_mutex
Definition: FileHandler.h:89
Euclid::FilePool::FileHandler::TypedFdWrapper::m_file_manager
FileManager * m_file_manager
Definition: FileHandler.h:79
Euclid::FilePool::FileHandler::kTryRead
@ kTryRead
Definition: FileHandler.h:42
Euclid::FilePool::FileManager
Definition: FileManager.h:56
Euclid::FilePool::FileHandler::FileHandler
FileHandler(const boost::filesystem::path &path, std::weak_ptr< FileManager > file_manager)
Definition: FileHandler.cpp:24
std::map
STL class.
std::weak_ptr
STL class.
Euclid::FilePool::FileHandler::TypedFdWrapper::m_id
FileManager::FileId m_id
Definition: FileHandler.h:77
Euclid::FilePool::FileAccessorBase::SharedLock
boost::shared_lock< SharedMutex > SharedLock
Definition: FileAccessor.h:33
Euclid::FilePool::FileHandler::FdWrapper::~FdWrapper
virtual ~FdWrapper()=default
Euclid::FilePool::FileHandler::getWriteAccessor
std::unique_ptr< FileAccessor< TFD > > getWriteAccessor(bool try_lock)
Euclid::FilePool::FileManager::close
void close(FileId id, TFD &fd)
Euclid::FilePool::FileHandler::kRead
@ kRead
Definition: FileHandler.h:42
std
STL namespace.
Euclid::FilePool::FileHandler::TypedFdWrapper::TypedFdWrapper
TypedFdWrapper(FileManager::FileId id, TFD &&fd, FileManager *manager)
Definition: FileHandler.h:81
Euclid::FilePool::FileHandler::m_is_readonly
bool m_is_readonly
Definition: FileHandler.h:94
Euclid::FilePool::FileHandler::m_file_mutex
SharedMutex m_file_mutex
Definition: FileHandler.h:92
Euclid::FilePool::FileHandler::TypedFdWrapper
Definition: FileHandler.h:76
std::mutex
STL class.
path
Elements::Path::Item path
FileManager.h
std::unique_ptr
STL class.
Euclid
Definition: InstOrRefHolder.h:29
Euclid::FilePool::FileHandler::getAccessor
std::unique_ptr< FileAccessor< TFD > > getAccessor(Mode mode=kRead)
Euclid::FilePool::FileHandler::isReadOnly
bool isReadOnly() const
Definition: FileHandler.cpp:33
Euclid::FilePool::FileHandler::TypedFdWrapper::close
void close() final
Definition: FileHandler.h:84
Euclid::FilePool::FileHandler::getReadAccessor
std::unique_ptr< FileAccessor< TFD > > getReadAccessor(bool try_lock)
Euclid::FilePool::FileHandler::kWrite
@ kWrite
Definition: FileHandler.h:42