X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FFile.h;h=86a152a2caaa356f8a248b07c4c4b5db4704c91b;hp=09a3b6bf4e087f9a0f53e6faef8bb2ec70528011;hb=0c20289b724cf462078cf70d0ea1487f67b45fa8;hpb=dee8a5180aa542d98d1b71c74f83a006e4627952 diff --git a/folly/File.h b/folly/File.h index 09a3b6bf..86a152a2 100644 --- a/folly/File.h +++ b/folly/File.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2013-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,12 +19,15 @@ #include #include #include -#include #include +#include +#include +#include #include #include +#include namespace folly { @@ -36,22 +39,37 @@ class File { /** * Creates an empty File object, for late initialization. */ - File(); + File() noexcept; /** * Create a File object from an existing file descriptor. * Takes ownership of the file descriptor if ownsFd is true. */ - explicit File(int fd, bool ownsFd = false); + explicit File(int fd, bool ownsFd = false) noexcept; /** * Open and create a file object. Throws on error. + * Owns the file descriptor implicitly. */ 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); + /** + * All the constructors that are not noexcept can throw std::system_error. + * This is a helper method to use folly::Expected to chain a file open event + * to something else you want to do with the open fd. + */ + template + static Expected makeFile(Args&&... args) noexcept { + try { + return File(std::forward(args)...); + } catch (const std::system_error& se) { + return makeUnexpected(exception_wrapper(std::current_exception(), se)); + } + } + ~File(); /** @@ -133,5 +151,4 @@ class File { void swap(File& a, File& b); - -} // namespace folly +} // namespace folly