internal/fd: Make it possible to not own the original fd
diff --git a/src/gpioplus/event.cpp b/src/gpioplus/event.cpp
index 3fc4062..d7e3716 100644
--- a/src/gpioplus/event.cpp
+++ b/src/gpioplus/event.cpp
@@ -4,6 +4,7 @@
#include <optional>
#include <stdexcept>
#include <system_error>
+#include <type_traits>
namespace gpioplus
{
@@ -46,7 +47,7 @@
Event::Event(const Chip& chip, uint32_t line_offset, HandleFlags handle_flags,
EventFlags event_flags, const char* consumer_label) :
fd(build(chip, line_offset, handle_flags, event_flags, consumer_label),
- chip.getFd().getSys())
+ std::false_type(), chip.getFd().getSys())
{
}
diff --git a/src/gpioplus/handle.cpp b/src/gpioplus/handle.cpp
index 30dc12f..30b43d8 100644
--- a/src/gpioplus/handle.cpp
+++ b/src/gpioplus/handle.cpp
@@ -3,6 +3,7 @@
#include <linux/gpio.h>
#include <stdexcept>
#include <system_error>
+#include <type_traits>
namespace gpioplus
{
@@ -74,7 +75,8 @@
Handle::Handle(const Chip& chip, const std::vector<Line>& lines,
HandleFlags flags, const char* consumer_label) :
- fd(build(chip, lines, flags, consumer_label), chip.getFd().getSys()),
+ fd(build(chip, lines, flags, consumer_label), std::false_type(),
+ chip.getFd().getSys()),
nlines(lines.size())
{
}
diff --git a/src/gpioplus/internal/fd.cpp b/src/gpioplus/internal/fd.cpp
index ef7c719..12a2f40 100644
--- a/src/gpioplus/internal/fd.cpp
+++ b/src/gpioplus/internal/fd.cpp
@@ -19,7 +19,21 @@
}
}
-Fd::Fd(int fd, const Sys* sys) : sys(sys), fd(fd)
+static int dup(int oldfd, const Sys* sys)
+{
+ int fd = sys->dup(oldfd);
+ if (fd < 0)
+ {
+ throw std::system_error(errno, std::generic_category(), "Duping FD");
+ }
+ return fd;
+}
+
+Fd::Fd(int fd, const Sys* sys) : sys(sys), fd(dup(fd, sys))
+{
+}
+
+Fd::Fd(int fd, std::false_type, const Sys* sys) : sys(sys), fd(fd)
{
}
@@ -35,16 +49,6 @@
}
}
-static int dup(int oldfd, const Sys* sys)
-{
- int fd = sys->dup(oldfd);
- if (fd < 0)
- {
- throw std::system_error(errno, std::generic_category(), "Duping FD");
- }
- return fd;
-}
-
Fd::Fd(const Fd& other) : sys(other.sys), fd(dup(other.fd, sys))
{
}
diff --git a/src/gpioplus/internal/fd.hpp b/src/gpioplus/internal/fd.hpp
index 7f54b81..4fb19cd 100644
--- a/src/gpioplus/internal/fd.hpp
+++ b/src/gpioplus/internal/fd.hpp
@@ -1,5 +1,6 @@
#pragma once
#include <gpioplus/internal/sys.hpp>
+#include <type_traits>
namespace gpioplus
{
@@ -11,6 +12,7 @@
public:
Fd(const char* pathname, int flags, const Sys* sys);
Fd(int fd, const Sys* sys);
+ Fd(int fd, std::false_type, const Sys* sys);
~Fd();
Fd(const Fd& other);