Browse Source

Peer: Use std::unique_ptr for res_

Tatsuhiro Tsujikawa 12 years ago
parent
commit
b759725a61
2 changed files with 4 additions and 7 deletions
  1. 2 5
      src/Peer.cc
  2. 2 2
      src/Peer.h

+ 2 - 5
src/Peer.cc

@@ -53,7 +53,6 @@ Peer::Peer(std::string ipaddr, uint16_t port, bool incoming):
   firstContactTime_(global::wallclock()),
   dropStartTime_(0),
   seeder_(false),
-  res_(nullptr),
   incoming_(incoming),
   localPeer_(false),
   disconnectedGracefully_(false)
@@ -73,8 +72,7 @@ void Peer::usedBy(cuid_t cuid)
 
 void Peer::allocateSessionResource(int32_t pieceLength, int64_t totalLength)
 {
-  delete res_;
-  res_ = new PeerSessionResource(pieceLength, totalLength);
+  res_ = make_unique<PeerSessionResource>(pieceLength, totalLength);
   res_->getNetStat().downloadStart();
   updateSeeder();
 }
@@ -87,8 +85,7 @@ void Peer::reconfigureSessionResource(int32_t pieceLength, int64_t totalLength)
 
 void Peer::releaseSessionResource()
 {
-  delete res_;
-  res_ = nullptr;
+  res_.reset();
 }
 
 void Peer::setPeerId(const unsigned char* peerId)

+ 2 - 2
src/Peer.h

@@ -74,7 +74,7 @@ private:
 
   bool seeder_;
 
-  PeerSessionResource* res_;
+  std::unique_ptr<PeerSessionResource> res_;
 
   // If true, port is assumed not to be a listening port.
   bool incoming_;
@@ -129,7 +129,7 @@ public:
   // Returns true iff res_ != 0.
   bool isActive() const
   {
-    return res_ != nullptr;
+    return res_.get() != nullptr;
   }
 
   void setPeerId(const unsigned char* peerId);