From: Yedidya Feldblum Date: Mon, 13 Jul 2015 20:25:25 +0000 (-0700) Subject: File ctor should take StringPiece. X-Git-Tag: v0.51.0~18 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e20eed09e0d77047b8e1efbcbfc1cb46e6cf0d97;p=folly.git File ctor should take StringPiece. Summary: [Folly] File ctor should take StringPiece. This way we can use it with `std::string` and `folly::fbstring` a touch more easily. Reviewed By: @luciang Differential Revision: D2235870 --- diff --git a/folly/File.cpp b/folly/File.cpp index 253d0054..3c535733 100644 --- a/folly/File.cpp +++ b/folly/File.cpp @@ -53,6 +53,12 @@ File::File(const char* name, int flags, mode_t mode) ownsFd_ = true; } +File::File(const std::string& name, int flags, mode_t mode) + : File(name.c_str(), flags, mode) {} + +File::File(StringPiece name, int flags, mode_t mode) + : File(name.str(), flags, mode) {} + File::File(File&& other) noexcept : fd_(other.fd_) , ownsFd_(other.ownsFd_) { diff --git a/folly/File.h b/folly/File.h index cf8fdc7f..45925142 100644 --- a/folly/File.h +++ b/folly/File.h @@ -17,12 +17,15 @@ #ifndef FOLLY_FILE_H_ #define FOLLY_FILE_H_ +#include #include #include -#include #include +#include + #include +#include namespace folly { @@ -46,6 +49,9 @@ class File { * Open and create a file object. Throws on error. */ explicit File(const char* name, int flags = O_RDONLY, mode_t mode = 0666); + explicit File( + const std::string& name, int flags = O_RDONLY, mode_t mode = 0666); + explicit File(StringPiece name, int flags = O_RDONLY, mode_t mode = 0666); ~File(); diff --git a/folly/test/FileTest.cpp b/folly/test/FileTest.cpp index 613982d0..3a120c54 100644 --- a/folly/test/FileTest.cpp +++ b/folly/test/FileTest.cpp @@ -55,6 +55,15 @@ TEST(File, Simple) { } } +TEST(File, SimpleStringPiece) { + char buf = 'x'; + File f(StringPiece("/etc/hosts")); + EXPECT_NE(-1, f.fd()); + EXPECT_EQ(1, ::read(f.fd(), &buf, 1)); + f.close(); + EXPECT_EQ(-1, f.fd()); +} + TEST(File, OwnsFd) { // Wrap a file descriptor, make sure that ownsFd works // We'll test that the file descriptor is closed by closing the writing