X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FFile.h;h=af6620960b6e3bb36e354a6f4417a9392f9ca25d;hp=459251428d370b37431e191493b55f57f72000d5;hb=60b2478a8e299bbae257a5fe69837696c0950ee5;hpb=e20eed09e0d77047b8e1efbcbfc1cb46e6cf0d97 diff --git a/folly/File.h b/folly/File.h index 45925142..af662096 100644 --- a/folly/File.h +++ b/folly/File.h @@ -1,5 +1,5 @@ /* - * Copyright 2015 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,18 +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 namespace folly { @@ -37,13 +39,13 @@ 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. @@ -53,6 +55,20 @@ class 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(); /** @@ -136,5 +152,3 @@ void swap(File& a, File& b); } // namespace folly - -#endif /* FOLLY_FILE_H_ */