-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
177 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#ifndef MEDIAPIPE_FRAMEWORK_FORMATS_SHARED_FD_H_ | ||
#define MEDIAPIPE_FRAMEWORK_FORMATS_SHARED_FD_H_ | ||
|
||
#include <cstddef> | ||
#include <memory> | ||
#include <utility> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "mediapipe/framework/formats/unique_fd.h" | ||
|
||
namespace mediapipe { | ||
|
||
// Provides a shared ownership for a file descriptor. | ||
// | ||
// File descriptor is closed as soon as last SharedFd is destroyed. | ||
// (Uses `std::shared_ptr` internally and can be used in the same way: copy, | ||
// move, assign/compare with nullptr, use in conditional statements.) | ||
class SharedFd { | ||
public: | ||
// `fd` a valid file descriptor. | ||
explicit SharedFd(UniqueFd fd) | ||
: fd_(std::make_shared<UniqueFd>(std::move(fd))) {} | ||
|
||
// Constructs empty SharedFd (fd == nullptr evaluates to true) | ||
SharedFd() = default; | ||
|
||
~SharedFd() = default; | ||
|
||
// Copyable | ||
SharedFd(const SharedFd&) = default; | ||
SharedFd& operator=(const SharedFd&) = default; | ||
|
||
// Moveable | ||
SharedFd(SharedFd&& other) = default; | ||
SharedFd& operator=(SharedFd&& other) = default; | ||
|
||
// Resets this SharedFd object (fd == nullptr will evaluate to true). | ||
SharedFd& operator=(std::nullptr_t other) { | ||
fd_ = other; | ||
return *this; | ||
} | ||
|
||
bool operator==(std::nullptr_t other) const { return fd_ == other; } | ||
bool operator!=(std::nullptr_t other) const { return !operator==(other); }; | ||
|
||
// SharedFd can be used in conditional statements: | ||
// ``` | ||
// if (fd) { | ||
// int raw_fd = fd.Get(); | ||
// } | ||
// ``` | ||
explicit operator bool() const { return operator!=(nullptr); } | ||
|
||
// Gets raw file descriptor for read purposes. | ||
int Get() const { return fd_->Get(); } | ||
|
||
// Duplicates file descriptor. | ||
absl::StatusOr<UniqueFd> Dup() const { return fd_->Dup(); } | ||
|
||
private: | ||
std::shared_ptr<UniqueFd> fd_; | ||
}; | ||
|
||
} // namespace mediapipe | ||
|
||
#endif // MEDIAPIPE_FRAMEWORK_FORMATS_SHARED_FD_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "mediapipe/framework/formats/shared_fd.h" | ||
|
||
#include <utility> | ||
|
||
#include "mediapipe/framework/formats/unique_fd.h" | ||
#include "mediapipe/framework/port/gtest.h" | ||
#include "mediapipe/framework/port/status_matchers.h" | ||
#include "mediapipe/util/fd_test_util.h" | ||
|
||
namespace mediapipe { | ||
namespace { | ||
|
||
TEST(SharedFdTest, CanCreateFromUniqueFd) { | ||
int raw_fd = GetValidFd(); | ||
{ | ||
auto fd = SharedFd(UniqueFd(raw_fd)); | ||
EXPECT_TRUE(IsFdValid(fd.Get())); | ||
} | ||
EXPECT_FALSE(IsFdValid(raw_fd)); | ||
} | ||
|
||
TEST(SharedFdTest, CanCopyAndMoveFd) { | ||
int raw_fd = GetValidFd(); | ||
auto fd = SharedFd(UniqueFd(raw_fd)); | ||
{ | ||
SharedFd copied_fd = fd; | ||
EXPECT_TRUE(IsFdValid(copied_fd.Get())); | ||
} | ||
EXPECT_TRUE(IsFdValid(fd.Get())); | ||
|
||
{ | ||
SharedFd moved_fd = std::move(fd); | ||
EXPECT_TRUE(IsFdValid(moved_fd.Get())); | ||
} | ||
EXPECT_FALSE(IsFdValid(raw_fd)); | ||
} | ||
|
||
TEST(SharedFdTest, CanBeAssignedAndComparedWithNullptr) { | ||
SharedFd fd; | ||
EXPECT_FALSE(fd); | ||
EXPECT_EQ(fd, nullptr); | ||
|
||
int raw_fd = GetValidFd(); | ||
fd = SharedFd(UniqueFd(raw_fd)); | ||
|
||
EXPECT_NE(fd, nullptr); | ||
EXPECT_TRUE(fd); | ||
|
||
fd = nullptr; | ||
EXPECT_FALSE(IsFdValid(raw_fd)); | ||
EXPECT_EQ(fd, nullptr); | ||
EXPECT_FALSE(fd); | ||
} | ||
|
||
TEST(SharedFdTest, CanDup) { | ||
int raw_fd = GetValidFd(); | ||
auto fd = SharedFd(UniqueFd(GetValidFd())); | ||
MP_ASSERT_OK_AND_ASSIGN(UniqueFd dup_fd, fd.Dup()); | ||
EXPECT_NE(dup_fd.Get(), raw_fd); | ||
EXPECT_TRUE(IsFdValid(dup_fd.Get())); | ||
} | ||
|
||
} // namespace | ||
} // namespace mediapipe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef MEDIAPIPE_UTIL_FD_TEST_UTIL_H_ | ||
#define MEDIAPIPE_UTIL_FD_TEST_UTIL_H_ | ||
|
||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
namespace mediapipe { | ||
|
||
// Returns a valid system file descriptor. | ||
inline int GetValidFd() { return dup(STDOUT_FILENO); } | ||
|
||
// Helper function to check if the file descriptor is valid (still open). | ||
inline int IsFdValid(int fd) { return fcntl(fd, F_GETFD) != -1; } | ||
|
||
} // namespace mediapipe | ||
|
||
#endif // MEDIAPIPE_UTIL_FD_TEST_UTIL_H_ |