Переглянути джерело

Use std::unique_ptr for FileAllocationIterator

Tatsuhiro Tsujikawa 12 роки тому
батько
коміт
f8d305fe63

+ 10 - 19
src/AbstractSingleDiskAdaptor.cc

@@ -145,30 +145,21 @@ void AbstractSingleDiskAdaptor::truncate(int64_t length)
   diskWriter_->truncate(length);
 }
 
-std::shared_ptr<FileAllocationIterator>
+std::unique_ptr<FileAllocationIterator>
 AbstractSingleDiskAdaptor::fileAllocationIterator()
 {
   switch(getFileAllocationMethod()) {
 #ifdef HAVE_SOME_FALLOCATE
-  case(DiskAdaptor::FILE_ALLOC_FALLOC): {
-    std::shared_ptr<FallocFileAllocationIterator> h
-      (new FallocFileAllocationIterator
-       (diskWriter_.get(), size() ,totalLength_));
-    return h;
-  }
+  case(DiskAdaptor::FILE_ALLOC_FALLOC):
+    return make_unique<FallocFileAllocationIterator>
+      (diskWriter_.get(), size() ,totalLength_);
 #endif // HAVE_SOME_FALLOCATE
-  case(DiskAdaptor::FILE_ALLOC_TRUNC): {
-    std::shared_ptr<TruncFileAllocationIterator> h
-      (new TruncFileAllocationIterator
-       (diskWriter_.get(), size(), totalLength_));
-    return h;
-  }
-  default: {
-    std::shared_ptr<AdaptiveFileAllocationIterator> h
-      (new AdaptiveFileAllocationIterator
-       (diskWriter_.get(), size(), totalLength_));
-    return h;
-  }
+  case(DiskAdaptor::FILE_ALLOC_TRUNC):
+    return make_unique<TruncFileAllocationIterator>
+      (diskWriter_.get(), size(), totalLength_);
+  default:
+    return make_unique<AdaptiveFileAllocationIterator>
+      (diskWriter_.get(), size(), totalLength_);
   }
 }
 

+ 1 - 1
src/AbstractSingleDiskAdaptor.h

@@ -73,7 +73,7 @@ public:
 
   virtual void truncate(int64_t length);
 
-  virtual std::shared_ptr<FileAllocationIterator> fileAllocationIterator();
+  virtual std::unique_ptr<FileAllocationIterator> fileAllocationIterator();
 
   // Make sure that DiskWriter is set before calling this function.
   virtual void enableReadOnly();

+ 9 - 8
src/AdaptiveFileAllocationIterator.cc

@@ -38,6 +38,7 @@
 #include "RecoverableException.h"
 #include "LogFactory.h"
 #include "Logger.h"
+#include "a2functional.h"
 #ifdef HAVE_FALLOCATE
 # include "FallocFileAllocationIterator.h"
 #endif // HAVE_FALLOCATE
@@ -66,20 +67,20 @@ void AdaptiveFileAllocationIterator::allocateChunk()
         offset_ += len;
       }
       A2_LOG_DEBUG("File system supports fallocate.");
-      allocator_.reset
-        (new FallocFileAllocationIterator(stream_, offset_, totalLength_));
+      allocator_ = make_unique<FallocFileAllocationIterator>
+        (stream_, offset_, totalLength_);
     } catch(RecoverableException& e) {
       A2_LOG_DEBUG("File system does not support fallocate.");
-      std::shared_ptr<SingleFileAllocationIterator> salloc
-        (new SingleFileAllocationIterator(stream_, offset_, totalLength_));
+      auto salloc = make_unique<SingleFileAllocationIterator>
+        (stream_, offset_, totalLength_);
       salloc->init();
-      allocator_ = salloc;
+      allocator_ = std::move(salloc);
     }
 #else // !HAVE_FALLOCATE
-    std::shared_ptr<SingleFileAllocationIterator> salloc
-      (new SingleFileAllocationIterator(stream_, offset_, totalLength_));
+    auto salloc = make_unique<SingleFileAllocationIterator>
+      (stream_, offset_, totalLength_);
     salloc->init();
-    allocator_ = salloc;
+    allocator_ = std::move(salloc);
 #endif // !HAVE_FALLOCATE
     allocator_->allocateChunk();
   } else {

+ 1 - 1
src/AdaptiveFileAllocationIterator.h

@@ -46,7 +46,7 @@ class BinaryStream;
 class AdaptiveFileAllocationIterator:public FileAllocationIterator
 {
 private:
-  std::shared_ptr<FileAllocationIterator> allocator_;
+  std::unique_ptr<FileAllocationIterator> allocator_;
 
   BinaryStream* stream_;
 

+ 2 - 2
src/DiskAdaptor.h

@@ -78,12 +78,12 @@ public:
     fileEntries_.assign(first, last);
   }
 
-  const std::vector<std::shared_ptr<FileEntry> >& getFileEntries() const
+  const std::vector<std::shared_ptr<FileEntry>>& getFileEntries() const
   {
     return fileEntries_;
   }
 
-  virtual std::shared_ptr<FileAllocationIterator> fileAllocationIterator() = 0;
+  virtual std::unique_ptr<FileAllocationIterator> fileAllocationIterator() = 0;
 
   virtual void enableReadOnly() {}
 

+ 4 - 3
src/FileAllocationEntry.cc

@@ -42,9 +42,10 @@
 namespace aria2 {
 
 FileAllocationEntry::FileAllocationEntry(RequestGroup* requestGroup,
-                                         std::unique_ptr<Command> nextCommand):
-  RequestGroupEntry(requestGroup, std::move(nextCommand)),
-  fileAllocationIterator_(requestGroup->getPieceStorage()->getDiskAdaptor()->fileAllocationIterator())
+                                         std::unique_ptr<Command> nextCommand)
+  : RequestGroupEntry{requestGroup, std::move(nextCommand)},
+    fileAllocationIterator_{requestGroup->getPieceStorage()->getDiskAdaptor()
+                            ->fileAllocationIterator()}
 {}
 
 FileAllocationEntry:: ~FileAllocationEntry()

+ 1 - 1
src/FileAllocationEntry.h

@@ -50,7 +50,7 @@ class DownloadEngine;
 
 class FileAllocationEntry : public RequestGroupEntry, public ProgressAwareEntry {
 private:
-  std::shared_ptr<FileAllocationIterator> fileAllocationIterator_;
+  std::unique_ptr<FileAllocationIterator> fileAllocationIterator_;
 public:
   FileAllocationEntry(RequestGroup* requestGroup,
                       std::unique_ptr<Command> nextCommand =

+ 2 - 2
src/MultiDiskAdaptor.cc

@@ -481,10 +481,10 @@ int64_t MultiDiskAdaptor::size()
   return size;
 }
 
-std::shared_ptr<FileAllocationIterator>
+std::unique_ptr<FileAllocationIterator>
 MultiDiskAdaptor::fileAllocationIterator()
 {
-  return std::make_shared<MultiFileAllocationIterator>(this);
+  return make_unique<MultiFileAllocationIterator>(this);
 }
 
 void MultiDiskAdaptor::enableReadOnly()

+ 1 - 1
src/MultiDiskAdaptor.h

@@ -150,7 +150,7 @@ public:
 
   virtual int64_t size();
 
-  virtual std::shared_ptr<FileAllocationIterator> fileAllocationIterator();
+  virtual std::unique_ptr<FileAllocationIterator> fileAllocationIterator();
 
   virtual void enableReadOnly();