Skip to content

Commit 99218bb

Browse files
committed
File now contains an IOChannel
Changing inheritance to containment avoids diamond inheritance with Storage. Removed some unused methods.
1 parent bf8f910 commit 99218bb

File tree

7 files changed

+39
-77
lines changed

7 files changed

+39
-77
lines changed

Utilities/StorageFactory/interface/File.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace edm::storage {
1111

1212
/** Basic file-related functions. Nicked from SEAL. */
13-
class File : public IOChannel, public Storage {
13+
class File : public Storage {
1414
public:
1515
File(void);
1616
File(IOFD fd, bool autoclose = true);
@@ -32,6 +32,8 @@ namespace edm::storage {
3232
using Storage::write;
3333
using Storage::writev;
3434

35+
IOFD fd() const { return m_channel.fd(); }
36+
3537
bool prefetch(const IOPosBuffer *what, IOSize n) override;
3638
IOSize read(void *into, IOSize n) override;
3739
IOSize read(void *into, IOSize n, IOOffset pos) override;
@@ -46,8 +48,8 @@ namespace edm::storage {
4648

4749
void resize(IOOffset size) override;
4850

49-
void flush(void) override;
50-
void close(void) override;
51+
void flush() override;
52+
void close() override;
5153
virtual void abort(void);
5254

5355
virtual void setAutoClose(bool closeit);
@@ -63,6 +65,7 @@ namespace edm::storage {
6365
static void sysopen(const char *name, int flags, int perms, IOFD &newfd, unsigned &newflags);
6466
static bool sysclose(IOFD fd, int *error = nullptr);
6567

68+
IOChannel m_channel;
6669
unsigned m_flags;
6770
};
6871
} // namespace edm::storage

Utilities/StorageFactory/interface/IOChannel.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace edm::storage {
88

99
/** Base class for stream-oriented I/O sources and targets, based
1010
on the operating system file descriptor. */
11-
class IOChannel : public virtual IOInput, public virtual IOOutput {
11+
class IOChannel : public IOInput, public IOOutput {
1212
public:
1313
IOChannel(IOFD fd = EDM_IOFD_INVALID);
14-
~IOChannel(void) override;
14+
~IOChannel() override;
1515
// implicit copy constructor
1616
// implicit assignment operator
1717

@@ -24,17 +24,11 @@ namespace edm::storage {
2424
IOSize write(const void *from, IOSize n) override;
2525
IOSize writev(const IOBuffer *from, IOSize buffers) override;
2626

27-
virtual IOFD fd(void) const;
28-
virtual void fd(IOFD value); // FIXME: dangerous?
27+
IOFD fd() const;
28+
void fd(IOFD value); // FIXME: dangerous?
2929

30-
virtual void close(void);
31-
32-
virtual void setBlocking(bool value);
33-
virtual bool isBlocking(void) const;
34-
35-
protected:
36-
// System implementation
37-
bool sysclose(IOFD fd, int *error = nullptr);
30+
void setBlocking(bool value);
31+
bool isBlocking() const;
3832

3933
private:
4034
IOFD m_fd; /*< System file descriptor. */

Utilities/StorageFactory/interface/Storage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace edm::storage {
1919
constexpr int PREFETCH_PROBE_LENGTH = 4096;
2020

21-
class Storage : public virtual IOInput, public virtual IOOutput {
21+
class Storage : public IOInput, public IOOutput {
2222
public:
2323
enum Relative { SET, CURRENT, END };
2424

Utilities/StorageFactory/src/File.cc

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,15 @@ using namespace edm::storage::IOFlags;
5555
//////////////////////////////////////////////////////////////////////
5656
//////////////////////////////////////////////////////////////////////
5757
/** Create a new file object without a file attached to it. */
58-
File::File(void) {
59-
fd(EDM_IOFD_INVALID);
60-
m_flags = 0;
61-
}
58+
File::File() : m_channel(EDM_IOFD_INVALID) { m_flags = 0; }
6259

6360
/** Create a new file object from a file descriptor. The descriptor
6461
will be closed automatically when the file object is destructed
6562
if @a autoclose is @c true (the default). */
66-
File::File(IOFD fd, bool autoclose /* = true */) {
67-
this->fd(fd);
68-
m_flags = autoclose ? InternalAutoClose : 0;
69-
}
63+
File::File(IOFD fd, bool autoclose /* = true */) : m_channel(fd) { m_flags = autoclose ? InternalAutoClose : 0; }
7064

7165
/** Internal function for copying file objects to retain the state flags. */
72-
File::File(IOFD fd, unsigned flags) {
73-
this->fd(fd);
74-
m_flags = flags;
75-
}
66+
File::File(IOFD fd, unsigned flags) : m_channel(fd) { m_flags = flags; }
7667

7768
/** Create a new file object by calling #open() with the given arguments. */
7869
File::File(const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/) { open(name, flags, perms); }
@@ -109,17 +100,17 @@ void File::setAutoClose(bool autoclose) {
109100
will not close its file descriptor on destruction, the original
110101
object (@c this) will. */
111102
File *File::duplicate(bool copy) const {
112-
File *dup = new File(fd(), copy ? m_flags : 0);
103+
File *dup = new File(m_channel.fd(), copy ? m_flags : 0);
113104
return copy ? this->duplicate(dup) : dup;
114105
}
115106

116107
/** Internal implementation of #duplicate() to actually duplicate the
117108
file handle into @a child. */
118109
File *File::duplicate(File *child) const {
119-
IOFD fd = this->fd();
110+
IOFD fd = m_channel.fd();
120111
assert(fd != EDM_IOFD_INVALID);
121112
assert(child);
122-
child->fd(sysduplicate(fd));
113+
child->m_channel.fd(sysduplicate(fd));
123114
child->m_flags = m_flags;
124115
return child;
125116
}
@@ -164,27 +155,27 @@ void File::open(const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/
164155
assert(flags & (OpenRead | OpenWrite));
165156

166157
// If I am already open, close the old file first.
167-
if (fd() != EDM_IOFD_INVALID && (m_flags & InternalAutoClose))
158+
if (m_channel.fd() != EDM_IOFD_INVALID && (m_flags & InternalAutoClose))
168159
close();
169160

170161
IOFD newfd = EDM_IOFD_INVALID;
171162
unsigned newflags = InternalAutoClose;
172163

173164
sysopen(name, flags, perms, newfd, newflags);
174165

175-
fd(newfd);
166+
m_channel.fd(newfd);
176167
m_flags = newflags;
177168
}
178169

179170
void File::attach(IOFD fd) {
180-
this->fd(fd);
171+
m_channel.fd(fd);
181172
m_flags = 0;
182173
}
183174

184175
//////////////////////////////////////////////////////////////////////
185176
/** Prefetch data for the file. */
186177
bool File::prefetch(const IOPosBuffer *what, IOSize n) {
187-
IOFD fd = this->fd();
178+
IOFD fd = m_channel.fd();
188179
for (IOSize i = 0; i < n; ++i) {
189180
#if F_RDADVISE
190181
radvisory info;
@@ -201,10 +192,10 @@ bool File::prefetch(const IOPosBuffer *what, IOSize n) {
201192
}
202193

203194
/** Read from the file. */
204-
IOSize File::read(void *into, IOSize n) { return IOChannel::read(into, n); }
195+
IOSize File::read(void *into, IOSize n) { return m_channel.read(into, n); }
205196

206197
/** Read from the file. */
207-
IOSize File::readv(IOBuffer *into, IOSize length) { return IOChannel::readv(into, length); }
198+
IOSize File::readv(IOBuffer *into, IOSize length) { return m_channel.readv(into, length); }
208199

209200
/** Write to the file. */
210201
IOSize File::write(const void *from, IOSize n) {
@@ -213,7 +204,7 @@ IOSize File::write(const void *from, IOSize n) {
213204
if (m_flags & OpenAppend)
214205
position(0, END);
215206

216-
IOSize s = IOChannel::write(from, n);
207+
IOSize s = m_channel.write(from, n);
217208

218209
if (m_flags & OpenUnbuffered)
219210
// FIXME: Exception handling?
@@ -229,7 +220,7 @@ IOSize File::writev(const IOBuffer *from, IOSize length) {
229220
if (m_flags & OpenAppend)
230221
position(0, END);
231222

232-
IOSize s = IOChannel::writev(from, length);
223+
IOSize s = m_channel.writev(from, length);
233224

234225
if (m_flags & OpenUnbuffered)
235226
// FIXME: Exception handling?
@@ -240,23 +231,23 @@ IOSize File::writev(const IOBuffer *from, IOSize length) {
240231

241232
/** Close the file. */
242233
void File::close() {
243-
IOFD fd = this->fd();
234+
IOFD fd = m_channel.fd();
244235
assert(fd != EDM_IOFD_INVALID);
245236

246237
int error;
247238
if (!sysclose(fd, &error))
248239
throwStorageError("FileCloseError", "Calling File::close()", "sysclose", error);
249240

250241
m_flags &= ~InternalAutoClose;
251-
this->fd(EDM_IOFD_INVALID);
242+
m_channel.fd(EDM_IOFD_INVALID);
252243
}
253244

254245
/** Close the file and ignore all errors. */
255246
void File::abort() {
256-
IOFD fd = this->fd();
247+
IOFD fd = m_channel.fd();
257248
if (fd != EDM_IOFD_INVALID) {
258249
sysclose(fd);
259250
m_flags &= ~InternalAutoClose;
260-
this->fd(EDM_IOFD_INVALID);
251+
m_channel.fd(EDM_IOFD_INVALID);
261252
}
262253
}

Utilities/StorageFactory/src/IOChannel.cc

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ using namespace edm::storage;
6161

6262
IOChannel::IOChannel(IOFD fd /* = EDM_IOFD_INVALID */) : m_fd(fd) {}
6363

64-
IOChannel::~IOChannel(void) {}
64+
IOChannel::~IOChannel() {}
6565

6666
//////////////////////////////////////////////////////////////////////
6767
//////////////////////////////////////////////////////////////////////
6868
//////////////////////////////////////////////////////////////////////
6969
/** Get the system file descriptor of the channel. */
70-
IOFD IOChannel::fd(void) const { return m_fd; }
70+
IOFD IOChannel::fd() const { return m_fd; }
7171

7272
/** Set the system file descriptor of the channel. (FIXME: This is
7373
dangerous. How to deal with WIN32 flags and state object?) */
@@ -76,19 +76,3 @@ void IOChannel::fd(IOFD value) {
7676
// FIXME: reset state?
7777
m_fd = value;
7878
}
79-
80-
//////////////////////////////////////////////////////////////////////
81-
//////////////////////////////////////////////////////////////////////
82-
//////////////////////////////////////////////////////////////////////
83-
/** Close the channel. By default closes the underlying operating
84-
system file descriptor. */
85-
void IOChannel::close(void) {
86-
if (fd() == EDM_IOFD_INVALID)
87-
return;
88-
89-
int error = 0;
90-
if (!sysclose(fd(), &error))
91-
throwStorageError("FileCloseError", "Calling IOChannel::close()", "sysclose()", error);
92-
93-
fd(EDM_IOFD_INVALID);
94-
}

Utilities/StorageFactory/src/UnixFile.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ IOSize File::read(void *into, IOSize n, IOOffset pos) {
6262

6363
ssize_t s;
6464
do
65-
s = ::pread(fd(), into, n, pos);
65+
s = ::pread(m_channel.fd(), into, n, pos);
6666
while (s == -1 && errno == EINTR);
6767

6868
if (s == -1)
@@ -76,7 +76,7 @@ IOSize File::write(const void *from, IOSize n, IOOffset pos) {
7676

7777
ssize_t s;
7878
do
79-
s = ::pwrite(fd(), from, n, pos);
79+
s = ::pwrite(m_channel.fd(), from, n, pos);
8080
while (s == -1 && errno == EINTR);
8181

8282
if (s == -1)
@@ -90,7 +90,7 @@ IOSize File::write(const void *from, IOSize n, IOOffset pos) {
9090
}
9191

9292
IOOffset File::size(void) const {
93-
IOFD fd = this->fd();
93+
IOFD fd = m_channel.fd();
9494
assert(fd != EDM_IOFD_INVALID);
9595

9696
struct stat info;
@@ -101,7 +101,7 @@ IOOffset File::size(void) const {
101101
}
102102

103103
IOOffset File::position(IOOffset offset, Relative whence /* = SET */) {
104-
IOFD fd = this->fd();
104+
IOFD fd = m_channel.fd();
105105
assert(fd != EDM_IOFD_INVALID);
106106
assert(whence == CURRENT || whence == SET || whence == END);
107107

@@ -114,15 +114,15 @@ IOOffset File::position(IOOffset offset, Relative whence /* = SET */) {
114114
}
115115

116116
void File::resize(IOOffset size) {
117-
IOFD fd = this->fd();
117+
IOFD fd = m_channel.fd();
118118
assert(fd != EDM_IOFD_INVALID);
119119

120120
if (ftruncate(fd, size) == -1)
121121
throwStorageError("FileResizeError", "Calling File::resize()", "ftruncate()", errno);
122122
}
123123

124124
void File::flush(void) {
125-
IOFD fd = this->fd();
125+
IOFD fd = m_channel.fd();
126126
assert(fd != EDM_IOFD_INVALID);
127127

128128
#if _POSIX_SYNCHRONIZED_IO > 0

Utilities/StorageFactory/src/UnixIOChannel.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,3 @@ bool IOChannel::isBlocking(void) const {
122122
return true;
123123
#endif // O_NONBLOCK
124124
}
125-
126-
//////////////////////////////////////////////////////////////////////
127-
//////////////////////////////////////////////////////////////////////
128-
//////////////////////////////////////////////////////////////////////
129-
bool IOChannel::sysclose(IOFD fd, int *error /* = 0 */) {
130-
int ret = ::close(fd);
131-
if (error)
132-
*error = errno;
133-
return ret != -1;
134-
}

0 commit comments

Comments
 (0)