浏览代码

Use std::unique_ptr for ProgressUpdate instead of raw pointer

Tatsuhiro Tsujikawa 12 年之前
父节点
当前提交
4f7d1c395b

+ 2 - 2
src/BtAllowedFastMessage.cc

@@ -78,9 +78,9 @@ struct ThisProgressUpdate : public ProgressUpdate {
 };
 } // namespace
 
-ProgressUpdate* BtAllowedFastMessage::getProgressUpdate()
+std::unique_ptr<ProgressUpdate> BtAllowedFastMessage::getProgressUpdate()
 {
-  return new ThisProgressUpdate(getPeer(), getIndex());
+  return make_unique<ThisProgressUpdate>(getPeer(), getIndex());
 }
 
 } // namespace aria2

+ 1 - 1
src/BtAllowedFastMessage.h

@@ -52,7 +52,7 @@ public:
 
   virtual void doReceivedAction();
 
-  virtual ProgressUpdate* getProgressUpdate();
+  virtual std::unique_ptr<ProgressUpdate> getProgressUpdate();
 };
 
 } // namespace aria2

+ 2 - 2
src/BtChokeMessage.cc

@@ -82,9 +82,9 @@ struct ThisProgressUpdate : public ProgressUpdate {
 };
 } // namespace
 
-ProgressUpdate* BtChokeMessage::getProgressUpdate()
+std::unique_ptr<ProgressUpdate> BtChokeMessage::getProgressUpdate()
 {
-  return new ThisProgressUpdate(getPeer(), getBtMessageDispatcher());
+  return make_unique<ThisProgressUpdate>(getPeer(), getBtMessageDispatcher());
 }
 
 } // namespace aria2

+ 1 - 1
src/BtChokeMessage.h

@@ -54,7 +54,7 @@ public:
 
   virtual bool sendPredicate() const;
 
-  virtual ProgressUpdate* getProgressUpdate();
+  virtual std::unique_ptr<ProgressUpdate> getProgressUpdate();
 };
 
 } // namespace aria2

+ 2 - 2
src/BtInterestedMessage.cc

@@ -84,9 +84,9 @@ struct ThisProgressUpdate : public ProgressUpdate {
 };
 } // namespace
 
-ProgressUpdate* BtInterestedMessage::getProgressUpdate()
+std::unique_ptr<ProgressUpdate> BtInterestedMessage::getProgressUpdate()
 {
-  return new ThisProgressUpdate(getPeer());
+  return make_unique<ThisProgressUpdate>(getPeer());
 }
 
 void BtInterestedMessage::setPeerStorage(PeerStorage* peerStorage)

+ 1 - 1
src/BtInterestedMessage.h

@@ -60,7 +60,7 @@ public:
 
   virtual bool sendPredicate() const;
 
-  virtual ProgressUpdate* getProgressUpdate();
+  virtual std::unique_ptr<ProgressUpdate> getProgressUpdate();
 
   void setPeerStorage(PeerStorage* peerStorage);
 };

+ 2 - 2
src/BtNotInterestedMessage.cc

@@ -84,9 +84,9 @@ struct ThisProgressUpdate : public ProgressUpdate {
 };
 } // namespace
 
-ProgressUpdate* BtNotInterestedMessage::getProgressUpdate()
+std::unique_ptr<ProgressUpdate> BtNotInterestedMessage::getProgressUpdate()
 {
-  return new ThisProgressUpdate(getPeer());
+  return make_unique<ThisProgressUpdate>(getPeer());
 }
 
 void BtNotInterestedMessage::setPeerStorage(PeerStorage* peerStorage)

+ 1 - 1
src/BtNotInterestedMessage.h

@@ -60,7 +60,7 @@ public:
 
   virtual bool sendPredicate() const;
 
-  virtual ProgressUpdate* getProgressUpdate();
+  virtual std::unique_ptr<ProgressUpdate> getProgressUpdate();
 
   void setPeerStorage(PeerStorage* peerStorage);
 };

+ 2 - 2
src/BtPieceMessage.cc

@@ -224,8 +224,8 @@ void BtPieceMessage::pushPieceData(int64_t offset, int32_t length) const
     unsigned char* dbuf = buf;
     buf.reset(0);
     getPeerConnection()->pushBytes(dbuf, length+MESSAGE_HEADER_LENGTH,
-                                   new PieceSendUpdate(getPeer(),
-                                                       MESSAGE_HEADER_LENGTH));
+                                   make_unique<PieceSendUpdate>
+                                   (getPeer(), MESSAGE_HEADER_LENGTH));
     // To avoid upload rate overflow, we update the length here at
     // once.
     downloadContext_->updateUploadLength(length);

+ 2 - 2
src/BtUnchokeMessage.cc

@@ -75,9 +75,9 @@ struct ThisProgressUpdate : public ProgressUpdate {
 };
 } // namespace
 
-ProgressUpdate* BtUnchokeMessage::getProgressUpdate()
+std::unique_ptr<ProgressUpdate> BtUnchokeMessage::getProgressUpdate()
 {
-  return new ThisProgressUpdate(getPeer());
+  return make_unique<ThisProgressUpdate>(getPeer());
 }
 
 } // namespace aria2

+ 1 - 1
src/BtUnchokeMessage.h

@@ -56,7 +56,7 @@ public:
 
   virtual bool sendPredicate() const;
 
-  virtual ProgressUpdate* getProgressUpdate();
+  virtual std::unique_ptr<ProgressUpdate> getProgressUpdate();
 };
 
 } // namespace aria2

+ 2 - 2
src/PeerConnection.cc

@@ -86,12 +86,12 @@ PeerConnection::~PeerConnection()
 }
 
 void PeerConnection::pushBytes(unsigned char* data, size_t len,
-                               ProgressUpdate* progressUpdate)
+                               std::unique_ptr<ProgressUpdate> progressUpdate)
 {
   if(encryptionEnabled_) {
     encryptor_->encrypt(len, data, data);
   }
-  socketBuffer_.pushBytes(data, len, progressUpdate);
+  socketBuffer_.pushBytes(data, len, std::move(progressUpdate));
 }
 
 bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength)

+ 2 - 3
src/PeerConnection.h

@@ -97,9 +97,8 @@ public:
   // Pushes data into send buffer. After this call, this object gets
   // ownership of data, so caller must not delete or alter it.
   void pushBytes(unsigned char* data, size_t len,
-                 ProgressUpdate* progressUpdate = 0);
-
-  void pushStr(const std::string& data);
+                 std::unique_ptr<ProgressUpdate> progressUpdate =
+                 std::unique_ptr<ProgressUpdate>{});
 
   bool receiveMessage(unsigned char* data, size_t& dataLength);
 

+ 5 - 0
src/SimpleBtMessage.cc

@@ -63,4 +63,9 @@ void SimpleBtMessage::send() {
   getPeerConnection()->pushBytes(msg, msgLength, getProgressUpdate());
 }
 
+std::unique_ptr<ProgressUpdate> SimpleBtMessage::getProgressUpdate()
+{
+  return std::unique_ptr<ProgressUpdate>{};
+}
+
 } // namespace aria2

+ 1 - 1
src/SimpleBtMessage.h

@@ -51,7 +51,7 @@ public:
 
   virtual size_t getMessageLength() = 0;
 
-  virtual ProgressUpdate* getProgressUpdate() { return 0; };
+  virtual std::unique_ptr<ProgressUpdate> getProgressUpdate();
 
   virtual bool sendPredicate() const { return true; };
 

+ 13 - 18
src/SocketBuffer.cc

@@ -47,8 +47,9 @@
 namespace aria2 {
 
 SocketBuffer::ByteArrayBufEntry::ByteArrayBufEntry
-(unsigned char* bytes, size_t length, ProgressUpdate* progressUpdate)
-  : BufEntry(progressUpdate), bytes_(bytes), length_(length)
+(unsigned char* bytes, size_t length,
+ std::unique_ptr<ProgressUpdate> progressUpdate)
+  : BufEntry(std::move(progressUpdate)), bytes_(bytes), length_(length)
 {}
 
 SocketBuffer::ByteArrayBufEntry::~ByteArrayBufEntry()
@@ -77,13 +78,11 @@ const unsigned char* SocketBuffer::ByteArrayBufEntry::getData() const
   return bytes_;
 }
 
-SocketBuffer::StringBufEntry::StringBufEntry(std::string s,
-                                             ProgressUpdate* progressUpdate)
-  : BufEntry(progressUpdate), str_(std::move(s))
+SocketBuffer::StringBufEntry::StringBufEntry
+(std::string s, std::unique_ptr<ProgressUpdate> progressUpdate)
+  : BufEntry(std::move(progressUpdate)), str_(std::move(s))
 {}
 
-// SocketBuffer::StringBufEntry::StringBufEntry() {}
-
 ssize_t SocketBuffer::StringBufEntry::send
 (const std::shared_ptr<SocketCore>& socket, size_t offset)
 {
@@ -105,30 +104,26 @@ const unsigned char* SocketBuffer::StringBufEntry::getData() const
   return reinterpret_cast<const unsigned char*>(str_.c_str());
 }
 
-void SocketBuffer::StringBufEntry::swap(std::string& s)
-{
-  str_.swap(s);
-}
-
 SocketBuffer::SocketBuffer(const std::shared_ptr<SocketCore>& socket):
   socket_(socket), offset_(0) {}
 
 SocketBuffer::~SocketBuffer() {}
 
 void SocketBuffer::pushBytes(unsigned char* bytes, size_t len,
-                             ProgressUpdate* progressUpdate)
+                             std::unique_ptr<ProgressUpdate> progressUpdate)
 {
   if(len > 0) {
-    bufq_.push_back(make_unique<ByteArrayBufEntry>(bytes, len,
-                                                   progressUpdate));
+    bufq_.push_back(make_unique<ByteArrayBufEntry>
+                    (bytes, len, std::move(progressUpdate)));
   }
 }
 
-void SocketBuffer::pushStr(std::string data, ProgressUpdate* progressUpdate)
+void SocketBuffer::pushStr(std::string data,
+                           std::unique_ptr<ProgressUpdate> progressUpdate)
 {
   if(!data.empty()) {
-    bufq_.push_back(make_unique<StringBufEntry>(std::move(data),
-                                                progressUpdate));
+    bufq_.push_back(make_unique<StringBufEntry>
+                    (std::move(data), std::move(progressUpdate)));
   }
 }
 

+ 11 - 13
src/SocketBuffer.h

@@ -54,12 +54,9 @@ class SocketBuffer {
 private:
   class BufEntry {
   public:
-    BufEntry(ProgressUpdate* progressUpdate)
-    : progressUpdate_(progressUpdate) {}
-    virtual ~BufEntry()
-    {
-      delete progressUpdate_;
-    }
+    BufEntry(std::unique_ptr<ProgressUpdate> progressUpdate)
+      : progressUpdate_(std::move(progressUpdate)) {}
+    virtual ~BufEntry() {}
     virtual ssize_t send
     (const std::shared_ptr<SocketCore>& socket, size_t offset) = 0;
     virtual bool final(size_t offset) const = 0;
@@ -72,13 +69,13 @@ private:
       }
     }
   private:
-    ProgressUpdate* progressUpdate_;
+    std::unique_ptr<ProgressUpdate> progressUpdate_;
   };
 
   class ByteArrayBufEntry:public BufEntry {
   public:
     ByteArrayBufEntry(unsigned char* bytes, size_t length,
-                      ProgressUpdate* progressUpdate);
+                      std::unique_ptr<ProgressUpdate> progressUpdate);
     virtual ~ByteArrayBufEntry();
     virtual ssize_t send
     (const std::shared_ptr<SocketCore>& socket, size_t offset);
@@ -93,14 +90,12 @@ private:
   class StringBufEntry:public BufEntry {
   public:
     StringBufEntry(std::string s,
-                   ProgressUpdate* progressUpdate);
-    StringBufEntry();
+                   std::unique_ptr<ProgressUpdate> progressUpdate);
     virtual ssize_t send
     (const std::shared_ptr<SocketCore>& socket, size_t offset);
     virtual bool final(size_t offset) const;
     virtual size_t getLength() const;
     virtual const unsigned char* getData() const;
-    void swap(std::string& s);
   private:
     std::string str_;
   };
@@ -129,13 +124,16 @@ public:
   // each time the data is sent. It will be deleted by this object. It
   // can be null.
   void pushBytes(unsigned char* bytes, size_t len,
-                 ProgressUpdate* progressUpdate = 0);
+                 std::unique_ptr<ProgressUpdate> progressUpdate =
+                 std::unique_ptr<ProgressUpdate>{});
 
   // Feeds data into queue. This function doesn't send data.  If
   // progressUpdate is not null, its update() function will be called
   // each time the data is sent. It will be deleted by this object. It
   // can be null.
-  void pushStr(std::string data, ProgressUpdate* progressUpdate = 0);
+  void pushStr(std::string data,
+               std::unique_ptr<ProgressUpdate> progressUpdate =
+               std::unique_ptr<ProgressUpdate>{});
 
   // Sends data in queue.  Returns the number of bytes sent.
   ssize_t send();