Skip to content

Commit 6c4fecf

Browse files
committed
Merge #11351: Refactor: Modernize disallowed copy constructors/assignment
2a07f87 Refactor: Modernize disallowed copy constructors/assignment (Dan Raviv) Pull request description: Use C++11's better capability of expressing an interface of a non-copyable class by publicly deleting its copy ctor and assignment operator instead of just declaring them private. Tree-SHA512: 878f446be5a136bb2a90643aaeaca62948b575e6ef71ccc5b4b8f373e66f36ced00665128f36504e0ccfee639863d969329c4276154ef9f2a9de9137f0801e01
2 parents 49f3d57 + 2a07f87 commit 6c4fecf

File tree

7 files changed

+30
-34
lines changed

7 files changed

+30
-34
lines changed

src/coins.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ class CCoinsViewCache : public CCoinsViewBacked
214214
public:
215215
CCoinsViewCache(CCoinsView *baseIn);
216216

217+
/**
218+
* By deleting the copy constructor, we prevent accidentally using it when one intends to create a cache on top of a base cache.
219+
*/
220+
CCoinsViewCache(const CCoinsViewCache &) = delete;
221+
217222
// Standard CCoinsView methods
218223
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
219224
bool HaveCoin(const COutPoint &outpoint) const override;
@@ -290,11 +295,6 @@ class CCoinsViewCache : public CCoinsViewBacked
290295

291296
private:
292297
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
293-
294-
/**
295-
* By making the copy constructor private, we prevent accidentally using it when one intends to create a cache on top of a base cache.
296-
*/
297-
CCoinsViewCache(const CCoinsViewCache &);
298298
};
299299

300300
//! Utility function to add all of a transaction's outputs to a cache.

src/net.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,13 +702,11 @@ class CNode
702702

703703
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string &addrNameIn = "", bool fInboundIn = false);
704704
~CNode();
705+
CNode(const CNode&) = delete;
706+
CNode& operator=(const CNode&) = delete;
705707

706708
private:
707-
CNode(const CNode&);
708-
void operator=(const CNode&);
709709
const NodeId id;
710-
711-
712710
const uint64_t nLocalHostNonce;
713711
// Services offered to this peer
714712
const ServiceFlags nLocalServices;

src/streams.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,6 @@ class CDataStream
455455
class CAutoFile
456456
{
457457
private:
458-
// Disallow copies
459-
CAutoFile(const CAutoFile&);
460-
CAutoFile& operator=(const CAutoFile&);
461-
462458
const int nType;
463459
const int nVersion;
464460

@@ -475,6 +471,10 @@ class CAutoFile
475471
fclose();
476472
}
477473

474+
// Disallow copies
475+
CAutoFile(const CAutoFile&) = delete;
476+
CAutoFile& operator=(const CAutoFile&) = delete;
477+
478478
void fclose()
479479
{
480480
if (file) {
@@ -564,10 +564,6 @@ class CAutoFile
564564
class CBufferedFile
565565
{
566566
private:
567-
// Disallow copies
568-
CBufferedFile(const CBufferedFile&);
569-
CBufferedFile& operator=(const CBufferedFile&);
570-
571567
const int nType;
572568
const int nVersion;
573569

@@ -609,6 +605,10 @@ class CBufferedFile
609605
fclose();
610606
}
611607

608+
// Disallow copies
609+
CBufferedFile(const CBufferedFile&) = delete;
610+
CBufferedFile& operator=(const CBufferedFile&) = delete;
611+
612612
int GetVersion() const { return nVersion; }
613613
int GetType() const { return nType; }
614614

src/support/lockedpool.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class Arena
5050
Arena(void *base, size_t size, size_t alignment);
5151
virtual ~Arena();
5252

53+
Arena(const Arena& other) = delete; // non construction-copyable
54+
Arena& operator=(const Arena&) = delete; // non copyable
55+
5356
/** Memory statistics. */
5457
struct Stats
5558
{
@@ -85,9 +88,6 @@ class Arena
8588
*/
8689
bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
8790
private:
88-
Arena(const Arena& other) = delete; // non construction-copyable
89-
Arena& operator=(const Arena&) = delete; // non copyable
90-
9191
/** Map of chunk address to chunk information. This class makes use of the
9292
* sorted order to merge previous and next chunks during deallocation.
9393
*/
@@ -153,6 +153,9 @@ class LockedPool
153153
explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = nullptr);
154154
~LockedPool();
155155

156+
LockedPool(const LockedPool& other) = delete; // non construction-copyable
157+
LockedPool& operator=(const LockedPool&) = delete; // non copyable
158+
156159
/** Allocate size bytes from this arena.
157160
* Returns pointer on success, or 0 if memory is full or
158161
* the application tried to allocate 0 bytes.
@@ -168,9 +171,6 @@ class LockedPool
168171
/** Get pool usage statistics */
169172
Stats stats() const;
170173
private:
171-
LockedPool(const LockedPool& other) = delete; // non construction-copyable
172-
LockedPool& operator=(const LockedPool&) = delete; // non copyable
173-
174174
std::unique_ptr<LockedPageAllocator> allocator;
175175

176176
/** Create an arena from locked pages */

src/txdb.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ class CBlockTreeDB : public CDBWrapper
110110
{
111111
public:
112112
explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
113-
private:
114-
CBlockTreeDB(const CBlockTreeDB&);
115-
void operator=(const CBlockTreeDB&);
116-
public:
113+
114+
CBlockTreeDB(const CBlockTreeDB&) = delete;
115+
CBlockTreeDB& operator=(const CBlockTreeDB&) = delete;
116+
117117
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
118118
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
119119
bool ReadLastBlockFile(int &nFile);

src/wallet/db.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ class CDB
156156
explicit CDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
157157
~CDB() { Close(); }
158158

159+
CDB(const CDB&) = delete;
160+
CDB& operator=(const CDB&) = delete;
161+
159162
void Flush();
160163
void Close();
161164
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
@@ -168,10 +171,6 @@ class CDB
168171
/* verifies the database file */
169172
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc);
170173

171-
private:
172-
CDB(const CDB&);
173-
void operator=(const CDB&);
174-
175174
public:
176175
template <typename K, typename T>
177176
bool Read(const K& key, T& value)

src/wallet/walletdb.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class CWalletDB
167167
m_dbw(dbw)
168168
{
169169
}
170+
CWalletDB(const CWalletDB&) = delete;
171+
CWalletDB& operator=(const CWalletDB&) = delete;
170172

171173
bool WriteName(const std::string& strAddress, const std::string& strName);
172174
bool EraseName(const std::string& strAddress);
@@ -244,9 +246,6 @@ class CWalletDB
244246
private:
245247
CDB batch;
246248
CWalletDBWrapper& m_dbw;
247-
248-
CWalletDB(const CWalletDB&);
249-
void operator=(const CWalletDB&);
250249
};
251250

252251
//! Compacts BDB state so that wallet.dat is self-contained (if there are changes)

0 commit comments

Comments
 (0)