Alexandria  2.27.0
SDC-CH common library for the Euclid project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
boost::unique_lock< SharedMutex > UniqueLock
Definition: FileAccessor.h:34
std::map< FileManager::FileId, std::unique_ptr< FdWrapper > > m_available_fd
Definition: FileHandler.h:93
boost::filesystem::path m_path
Definition: FileHandler.h:90
std::unique_ptr< FileAccessor< TFD > > getWriteAccessor(bool try_lock)
typename FileAccessorBase::SharedLock SharedLock
Definition: FileHandler.h:66
FileHandler(const boost::filesystem::path &path, std::weak_ptr< FileManager > file_manager)
Definition: FileHandler.cpp:24
STL class.
typename FileAccessorBase::UniqueLock UniqueLock
Definition: FileHandler.h:67
std::unique_ptr< FileAccessor< TFD > > getReadAccessor(bool try_lock)
intptr_t FileId
Opaque FileId, its concrete type should only be assumed to be copyable and hashable.
Definition: FileManager.h:65
bool close(FileManager::FileId id)
Definition: FileHandler.cpp:37
TypedFdWrapper(FileManager::FileId id, TFD &&fd, FileManager *manager)
Definition: FileHandler.h:81
typename FileAccessorBase::SharedMutex SharedMutex
Definition: FileHandler.h:65
std::unique_ptr< FileAccessor< TFD > > getAccessor(Mode mode=kRead)
void close(FileId id, TFD &fd)
std::weak_ptr< FileManager > m_file_manager
Definition: FileHandler.h:91
STL class.
STL class.
boost::shared_lock< SharedMutex > SharedLock
Definition: FileAccessor.h:33
boost::shared_mutex SharedMutex
Definition: FileAccessor.h:32
virtual ~FileHandler()
Destructor.
Definition: FileHandler.cpp:27
Path::Item path