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
FileAccessor.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_FILEACCESSOR_H
20 #define POOLTESTS_FILEACCESSOR_H
21 
22 #include <boost/thread/shared_mutex.hpp>
23 
24 namespace Euclid {
25 namespace FilePool {
26 
31 public:
32  using SharedMutex = boost::shared_mutex;
33  using SharedLock = boost::shared_lock<SharedMutex>;
34  using UniqueLock = boost::unique_lock<SharedMutex>;
35  using UpgradeLock = boost::upgrade_lock<SharedMutex>;
36  using UpgradeToUniqueLock = boost::upgrade_to_unique_lock<SharedMutex>;
37 
38  virtual ~FileAccessorBase() = default;
39 
41  virtual bool isReadOnly() const = 0;
42 };
43 
53 template <typename TFD>
55 public:
57 
59  TFD m_fd;
60 
62  virtual ~FileAccessor() = default;
63 
64 protected:
65  FileAccessor(TFD&& fd, ReleaseDescriptorCallback release_callback);
66 
68 };
69 
80 template <typename TFD>
81 class FileReadAccessor : public FileAccessor<TFD> {
82 public:
85  using SharedLock = typename Base_::SharedLock;
86 
96  FileReadAccessor(TFD&& fd, ReleaseDescriptorCallback release_callback, SharedLock lock);
97 
99  FileReadAccessor(const FileReadAccessor&) = delete;
100 
102  FileReadAccessor(FileReadAccessor&&) = default;
103 
105  virtual ~FileReadAccessor();
106 
108  bool isReadOnly() const final;
109 
110 private:
112 };
113 
120 template <typename TFD>
121 class FileWriteAccessor : public FileAccessor<TFD> {
122 public:
125  using SharedLock = typename Base_::SharedLock;
126  using UniqueLock = typename Base_::UniqueLock;
127 
137  FileWriteAccessor(TFD&& fd, ReleaseDescriptorCallback release_callback, UniqueLock lock);
138 
140  virtual ~FileWriteAccessor();
141 
143  bool isReadOnly() const final;
144 
145 private:
146  UniqueLock m_unique_lock;
147 };
148 
149 } // namespace FilePool
150 } // namespace Euclid
151 
152 #define FILEACCESSOR_IMPL
154 #undef FILEACCESSOR_IMPL
155 
156 #endif // POOLTESTS_FILEACCESSOR_H
boost::unique_lock< SharedMutex > UniqueLock
Definition: FileAccessor.h:34
boost::upgrade_lock< SharedMutex > UpgradeLock
Definition: FileAccessor.h:35
FileReadAccessor(TFD &&fd, ReleaseDescriptorCallback release_callback, SharedLock lock)
ReleaseDescriptorCallback m_release_callback
Definition: FileAccessor.h:67
std::function< void(TFD &&)> ReleaseDescriptorCallback
Definition: FileAccessor.h:56
TFD m_fd
The wrapped file descriptor.
Definition: FileAccessor.h:59
virtual ~FileAccessor()=default
Destructor.
FileAccessor(TFD &&fd, ReleaseDescriptorCallback release_callback)
boost::shared_lock< SharedMutex > SharedLock
Definition: FileAccessor.h:33
virtual ~FileReadAccessor()
Destructor.
virtual bool isReadOnly() const =0
boost::shared_mutex SharedMutex
Definition: FileAccessor.h:32
boost::upgrade_to_unique_lock< SharedMutex > UpgradeToUniqueLock
Definition: FileAccessor.h:36