Skip to content

Commit 4d47a47

Browse files
committed
Simplified the move semantics by avoiding virtual functions
1 parent fc55153 commit 4d47a47

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

source/matplot/util/popen.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ int common_pipe::close(int *exit_code)
169169
{
170170
if (!opened())
171171
return 0;
172-
fclose(file_);
172+
::fclose(file_);
173173
file_ = nullptr;
174-
while (waitpid(pid, exit_code, 0) == -1) {
174+
while (::waitpid(pid, exit_code, 0) == -1) {
175175
if (errno != EINTR) {
176176
if (exit_code != nullptr)
177177
*exit_code = -1;
@@ -190,6 +190,10 @@ inline int common_pipe::error(int code, const std::string& what) const
190190
return code;
191191
}
192192

193+
ipipe::ipipe() = default; // starting with C++20 could be in-class
194+
195+
opipe::opipe() = default; // starting with C++20 could be in-class
196+
193197
int opipe::write(std::string_view data)
194198
{
195199
constexpr auto CSIZE = sizeof(std::string_view::value_type);

source/matplot/util/popen.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
/// State of a child process pipe
1717
class proc_pipe
1818
{
19-
public:
20-
proc_pipe() = default;
21-
2219
protected:
20+
proc_pipe() = default;
2321
HANDLE hProcess = 0; ///< WIN32 process handle
2422
HANDLE hThread = 0; ///< WIN32 thread handle
2523
FILE *file_ = nullptr; ///< C file handle for I/O (not both)
@@ -32,10 +30,8 @@ class proc_pipe
3230
/// State of a child process pipe
3331
class proc_pipe
3432
{
35-
public:
36-
proc_pipe() = default;
37-
3833
protected:
34+
proc_pipe() = default;
3935
pid_t pid = 0; ///< POSIX process identifier
4036
FILE *file_ = nullptr; ///< C file handle for input or output (not both)
4137
};
@@ -48,18 +44,22 @@ class proc_pipe
4844
class common_pipe : public proc_pipe
4945
{
5046
public:
51-
common_pipe() : proc_pipe{} {}
52-
virtual ~common_pipe() noexcept {
53-
if (opened())
54-
close();
55-
}
5647
FILE *file() const { return file_; }
5748
int close(int *exit_code = nullptr);
5849
bool opened() const { return file_ != nullptr; }
5950
bool exceptions() const { return exceptions_; }
6051
void exceptions(bool exc) { exceptions_ = exc; }
6152

6253
protected:
54+
common_pipe() = default;
55+
common_pipe(const common_pipe&) = delete;
56+
common_pipe& operator=(const common_pipe&) = delete;
57+
common_pipe(common_pipe&&) noexcept = default;
58+
common_pipe& operator=(common_pipe&&) noexcept = default;
59+
~common_pipe() noexcept {
60+
if (opened())
61+
close();
62+
}
6363
bool exceptions_ = false;
6464
int error(int err, const std::string& what) const;
6565
int open(const std::string &command, char mode);
@@ -69,7 +69,7 @@ class common_pipe : public proc_pipe
6969
class ipipe : public common_pipe
7070
{
7171
public:
72-
ipipe() : common_pipe{} {}
72+
ipipe();
7373
int open(const std::string &cmd) { return common_pipe::open(cmd, 'r'); }
7474
int read(std::string &data);
7575
};
@@ -78,7 +78,7 @@ class ipipe : public common_pipe
7878
class opipe : public common_pipe
7979
{
8080
public:
81-
opipe() : common_pipe{} {}
81+
opipe();
8282
int open(const std::string &cmd) { return common_pipe::open(cmd, 'w'); }
8383
int write(std::string_view data);
8484
int flush(std::string_view data = {});

0 commit comments

Comments
 (0)