Skip to content

Commit

Permalink
Refactor: Modernize disallowed copy constructors/assignment
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danra authored and furszy committed Jul 21, 2021
1 parent 56f10aa commit a1bef4f
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 34 deletions.
10 changes: 5 additions & 5 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ class CCoinsViewCache : public CCoinsViewBacked
public:
CCoinsViewCache(CCoinsView *baseIn);

/**
* By deleting the copy constructor, we prevent accidentally using it when one intends to create a cache on top of a base cache.
*/
CCoinsViewCache(const CCoinsViewCache &) = delete;

// Sapling methods
bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const override;
bool GetNullifier(const uint256 &nullifier) const override;
Expand Down Expand Up @@ -430,11 +435,6 @@ class CCoinsViewCache : public CCoinsViewBacked
const uint256 &currentRoot,
Tree &tree
);

/**
* By making the copy constructor private, we prevent accidentally using it when one intends to create a cache on top of a base cache.
*/
CCoinsViewCache(const CCoinsViewCache &);
};

//! Utility function to add all of a transaction's outputs to a cache.
Expand Down
6 changes: 2 additions & 4 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,10 @@ class CNode

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

private:
CNode(const CNode&);
void operator=(const CNode&);


const uint64_t nLocalHostNonce;
// Services offered to this peer
const ServiceFlags nLocalServices;
Expand Down
16 changes: 8 additions & 8 deletions src/streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,6 @@ class CDataStream : public CBaseDataStream<CSerializeData>
class CAutoFile
{
private:
// Disallow copies
CAutoFile(const CAutoFile&);
CAutoFile& operator=(const CAutoFile&);

const int nType;
const int nVersion;

Expand All @@ -428,6 +424,10 @@ class CAutoFile
fclose();
}

// Disallow copies
CAutoFile(const CAutoFile&) = delete;
CAutoFile& operator=(const CAutoFile&) = delete;

void fclose()
{
if (file) {
Expand Down Expand Up @@ -523,10 +523,6 @@ class CAutoFile
class CBufferedFile
{
private:
// Disallow copies
CBufferedFile(const CBufferedFile&);
CBufferedFile& operator=(const CBufferedFile&);

const int nType;
const int nVersion;

Expand Down Expand Up @@ -569,6 +565,10 @@ class CBufferedFile
fclose();
}

// Disallow copies
CBufferedFile(const CBufferedFile&) = delete;
CBufferedFile& operator=(const CBufferedFile&) = delete;

void fclose()
{
if (src) {
Expand Down
12 changes: 6 additions & 6 deletions src/support/lockedpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class Arena
Arena(void *base, size_t size, size_t alignment);
virtual ~Arena();

Arena(const Arena& other) = delete; // non construction-copyable
Arena& operator=(const Arena&) = delete; // non copyable

/** A chunk of memory.
*/
struct Chunk
Expand Down Expand Up @@ -106,9 +109,6 @@ class Arena
*/
bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
private:
Arena(const Arena& other) = delete; // non construction-copyable
Arena& operator=(const Arena&) = delete; // non copyable

/** Map of chunk address to chunk information. This class makes use of the
* sorted order to merge previous and next chunks during deallocation.
*/
Expand Down Expand Up @@ -173,6 +173,9 @@ class LockedPool
LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = 0);
~LockedPool();

LockedPool(const LockedPool& other) = delete; // non construction-copyable
LockedPool& operator=(const LockedPool&) = delete; // non copyable

/** Allocate size bytes from this arena.
* Returns pointer on success, or 0 if memory is full or
* the application tried to allocate 0 bytes.
Expand All @@ -188,9 +191,6 @@ class LockedPool
/** Get pool usage statistics */
Stats stats() const;
private:
LockedPool(const LockedPool& other) = delete; // non construction-copyable
LockedPool& operator=(const LockedPool&) = delete; // non copyable

std::unique_ptr<LockedPageAllocator> allocator;

/** Create an arena from locked pages */
Expand Down
6 changes: 2 additions & 4 deletions src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,9 @@ class CBlockTreeDB : public CDBWrapper
public:
CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);

private:
CBlockTreeDB(const CBlockTreeDB&);
void operator=(const CBlockTreeDB&);
CBlockTreeDB(const CBlockTreeDB&) = delete;
CBlockTreeDB& operator=(const CBlockTreeDB&) = delete;

public:
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
bool ReadBlockFileInfo(int nFile, CBlockFileInfo& fileinfo);
Expand Down
7 changes: 3 additions & 4 deletions src/wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class CDB
explicit CDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
~CDB() { Close(); }

CDB(const CDB&) = delete;
CDB& operator=(const CDB&) = delete;

void Flush();
void Close();
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
Expand All @@ -170,10 +173,6 @@ class CDB
/* verifies the database file */
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr, CDBEnv::recoverFunc_type recoverFunc);

private:
CDB(const CDB&);
void operator=(const CDB&);

public:
template <typename K, typename T>
bool Read(const K& key, T& value)
Expand Down
5 changes: 2 additions & 3 deletions src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class CWalletDB
m_dbw(dbw)
{
}
CWalletDB(const CWalletDB&) = delete;
CWalletDB& operator=(const CWalletDB&) = delete;

bool WriteName(const std::string& strAddress, const std::string& strName);
bool EraseName(const std::string& strAddress);
Expand Down Expand Up @@ -227,9 +229,6 @@ class CWalletDB
private:
CDB batch;
CWalletDBWrapper& m_dbw;

CWalletDB(const CWalletDB&);
void operator=(const CWalletDB&);
};

void NotifyBacked(const CWallet& wallet, bool fSuccess, std::string strMessage);
Expand Down

0 comments on commit a1bef4f

Please sign in to comment.