TemporaryFile and TemporaryDirectory
authorAdam Simpkins <simpkins@fb.com>
Thu, 26 Mar 2015 19:40:56 +0000 (12:40 -0700)
committerafrind <afrind@fb.com>
Thu, 2 Apr 2015 18:57:52 +0000 (11:57 -0700)
Summary:
TemporaryFile, TemporaryDirectory, and ChangeToTempDir should all be moveable
objects, but not copiable.  Define default move constructors and move
assignment operators for these classes.  This will prevent copy constructor and
copy assignment operators from being implicitly defined.

Test Plan:
Used this in a new test to write a helper function which created and returned
a new TemporaryFile object using the move constructor.

Reviewed By: yfeldblum@fb.com

Subscribers: doug, net-systems@, exa, folly-diffs@, yfeldblum

FB internal diff: D1945134

Signature: t1:1945134:1427342944:3428327e797ce4b3d362f9a2d2276de6d8b96137

folly/experimental/TestUtil.h

index adf8be39578eed9d86a1b58bb397cf18c361350a..30687bf72a994f9948d2fa70c7fb741c7455e323 100644 (file)
@@ -48,6 +48,10 @@ class TemporaryFile {
                          bool closeOnDestruction = true);
   ~TemporaryFile();
 
+  // Movable, but not copiable
+  TemporaryFile(TemporaryFile&&) = default;
+  TemporaryFile& operator=(TemporaryFile&&) = default;
+
   int fd() const { return fd_; }
   const fs::path& path() const;
 
@@ -81,6 +85,10 @@ class TemporaryDirectory {
                               Scope scope = Scope::DELETE_ON_DESTRUCTION);
   ~TemporaryDirectory();
 
+  // Movable, but not copiable
+  TemporaryDirectory(TemporaryDirectory&&) = default;
+  TemporaryDirectory& operator=(TemporaryDirectory&&) = default;
+
   const fs::path& path() const { return path_; }
 
  private:
@@ -97,6 +105,10 @@ public:
   ChangeToTempDir();
   ~ChangeToTempDir();
 
+  // Movable, but not copiable
+  ChangeToTempDir(ChangeToTempDir&&) = default;
+  ChangeToTempDir& operator=(ChangeToTempDir&&) = default;
+
   const fs::path& path() const { return dir_.path(); }
 
 private: