X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFile.h;h=958347d2b40d96e38164ccb7d2e4bafbc8a80121;hb=5277a636b62a8f9c80756012de9bae80358ee30e;hp=ec4a208579b7dfda778fcb620c9b94855ead1bcc;hpb=cb394cb5128a84d55c52e6ad8c6f8a5a0db00e32;p=folly.git diff --git a/folly/File.h b/folly/File.h index ec4a2085..958347d2 100644 --- a/folly/File.h +++ b/folly/File.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,13 +14,20 @@ * limitations under the License. */ -#ifndef FOLLY_FILE_H_ -#define FOLLY_FILE_H_ +#pragma once +#include #include #include -#include -#include + +#include +#include + +#include +#include +#include +#include +#include namespace folly { @@ -32,21 +39,36 @@ 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. */ - /* implicit */ 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. */ - /* implicit */ File(const char* name, - int flags = O_RDONLY, - mode_t mode = 0644); + 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(); @@ -88,7 +110,7 @@ class File { * Returns and releases the file descriptor; no longer owned by this File. * Returns -1 if the File object didn't wrap a file. */ - int release(); + int release() noexcept; /** * Swap this File with another. @@ -96,7 +118,7 @@ class File { void swap(File& other); // movable - File(File&&); + File(File&&) noexcept; File& operator=(File&&); // FLOCK (INTERPROCESS) LOCKS @@ -129,7 +151,4 @@ class File { void swap(File& a, File& b); - -} // namespace folly - -#endif /* FOLLY_FILE_H_ */ +} // namespace folly