Use std::unique_ptr for ProgressUpdate instead of raw pointer

pull/103/head
Tatsuhiro Tsujikawa 2013-07-01 21:59:54 +09:00
parent 529b9fdceb
commit 4f7d1c395b
17 changed files with 51 additions and 54 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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