Skip to content

Commit bd6af53

Browse files
committed
Merge bitcoin#20480: Replace boost::variant with std::variant
faa8f68 Replace boost::variant with std::variant (MarcoFalke) Pull request description: Now that we can use std::variant from the vanilla standard library, drop the third-party boost variant dependency ACKs for top commit: fjahr: Code review ACK faa8f68 fanquake: ACK faa8f68 Tree-SHA512: 6e3aecd33b00c2e31a763f999247944d5b2ce5e3018f1965c516c1000cd08ff6703a8d50fb0be64883153da2925ae72986b8a6b96586db74057bd05d6f4986e6
2 parents c4458cc + faa8f68 commit bd6af53

16 files changed

+56
-66
lines changed

src/key_io.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
#include <bech32.h>
99
#include <util/strencodings.h>
1010

11-
#include <boost/variant/apply_visitor.hpp>
12-
#include <boost/variant/static_visitor.hpp>
13-
11+
#include <algorithm>
1412
#include <assert.h>
1513
#include <string.h>
16-
#include <algorithm>
1714

18-
namespace
19-
{
20-
class DestinationEncoder : public boost::static_visitor<std::string>
15+
namespace {
16+
class DestinationEncoder
2117
{
2218
private:
2319
const CChainParams& m_params;
@@ -209,7 +205,7 @@ std::string EncodeExtKey(const CExtKey& key)
209205

210206
std::string EncodeDestination(const CTxDestination& dest)
211207
{
212-
return boost::apply_visitor(DestinationEncoder(Params()), dest);
208+
return std::visit(DestinationEncoder(Params()), dest);
213209
}
214210

215211
CTxDestination DecodeDestination(const std::string& str)

src/qt/addresstablemodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <wallet/wallet.h>
1212

1313
#include <algorithm>
14-
#include <typeinfo>
1514

1615
#include <QFont>
1716
#include <QDebug>
@@ -82,8 +81,9 @@ class AddressTablePriv
8281
{
8382
for (const auto& address : wallet.getAddresses())
8483
{
85-
if (pk_hash_only && address.dest.type() != typeid(PKHash))
84+
if (pk_hash_only && !std::holds_alternative<PKHash>(address.dest)) {
8685
continue;
86+
}
8787
AddressTableEntry::Type addressType = translateTransactionType(
8888
QString::fromStdString(address.purpose), address.is_mine);
8989
cachedAddressTable.append(AddressTableEntry(addressType,
@@ -261,7 +261,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value,
261261
} else if(index.column() == Address) {
262262
CTxDestination newAddress = DecodeDestination(value.toString().toStdString());
263263
// Refuse to set invalid address, set error status and return false
264-
if(boost::get<CNoDestination>(&newAddress))
264+
if(std::get_if<CNoDestination>(&newAddress))
265265
{
266266
editStatus = INVALID_ADDRESS;
267267
return false;

src/qt/coincontroldialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ void CoinControlDialog::updateLabels(CCoinControl& m_coin_control, WalletModel *
455455
else if(ExtractDestination(out.txout.scriptPubKey, address))
456456
{
457457
CPubKey pubkey;
458-
PKHash *pkhash = boost::get<PKHash>(&address);
458+
PKHash* pkhash = std::get_if<PKHash>(&address);
459459
if (pkhash && model->wallet().getPubKey(out.txout.scriptPubKey, ToKeyID(*pkhash), pubkey))
460460
{
461461
nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);

src/qt/signverifymessagedialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
120120
ui->statusLabel_SM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again."));
121121
return;
122122
}
123-
const PKHash* pkhash = boost::get<PKHash>(&destination);
123+
const PKHash* pkhash = std::get_if<PKHash>(&destination);
124124
if (!pkhash) {
125125
ui->addressIn_SM->setValid(false);
126126
ui->statusLabel_SM->setStyleSheet("QLabel { color: red; }");

src/qt/transactionrecord.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interface
123123
continue;
124124
}
125125

126-
if (!boost::get<CNoDestination>(&wtx.txout_address[nOut]))
126+
if (!std::get_if<CNoDestination>(&wtx.txout_address[nOut]))
127127
{
128128
// Sent to Bitcoin Address
129129
sub.type = TransactionRecord::SendToAddress;

src/rpc/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ CTxDestination AddAndGetMultisigDestination(const int required, const std::vecto
209209
return dest;
210210
}
211211

212-
class DescribeAddressVisitor : public boost::static_visitor<UniValue>
212+
class DescribeAddressVisitor
213213
{
214214
public:
215215
explicit DescribeAddressVisitor() {}
@@ -267,7 +267,7 @@ class DescribeAddressVisitor : public boost::static_visitor<UniValue>
267267

268268
UniValue DescribeAddress(const CTxDestination& dest)
269269
{
270-
return boost::apply_visitor(DescribeAddressVisitor(), dest);
270+
return std::visit(DescribeAddressVisitor(), dest);
271271
}
272272

273273
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)

src/script/descriptor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ class AddressDescriptor final : public DescriptorImpl
565565

566566
Optional<OutputType> GetOutputType() const override
567567
{
568-
switch (m_destination.which()) {
568+
switch (m_destination.index()) {
569569
case 1 /* PKHash */:
570570
case 2 /* ScriptHash */: return OutputType::LEGACY;
571571
case 3 /* WitnessV0ScriptHash */:
@@ -593,7 +593,7 @@ class RawDescriptor final : public DescriptorImpl
593593
{
594594
CTxDestination dest;
595595
ExtractDestination(m_script, dest);
596-
switch (dest.which()) {
596+
switch (dest.index()) {
597597
case 1 /* PKHash */:
598598
case 2 /* ScriptHash */: return OutputType::LEGACY;
599599
case 3 /* WitnessV0ScriptHash */:

src/script/signingprovider.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,18 @@ CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination&
179179
{
180180
// Only supports destinations which map to single public keys, i.e. P2PKH,
181181
// P2WPKH, and P2SH-P2WPKH.
182-
if (auto id = boost::get<PKHash>(&dest)) {
182+
if (auto id = std::get_if<PKHash>(&dest)) {
183183
return ToKeyID(*id);
184184
}
185-
if (auto witness_id = boost::get<WitnessV0KeyHash>(&dest)) {
185+
if (auto witness_id = std::get_if<WitnessV0KeyHash>(&dest)) {
186186
return ToKeyID(*witness_id);
187187
}
188-
if (auto script_hash = boost::get<ScriptHash>(&dest)) {
188+
if (auto script_hash = std::get_if<ScriptHash>(&dest)) {
189189
CScript script;
190190
CScriptID script_id(*script_hash);
191191
CTxDestination inner_dest;
192192
if (store.GetCScript(script_id, script) && ExtractDestination(script, inner_dest)) {
193-
if (auto inner_witness_id = boost::get<WitnessV0KeyHash>(&inner_dest)) {
193+
if (auto inner_witness_id = std::get_if<WitnessV0KeyHash>(&inner_dest)) {
194194
return ToKeyID(*inner_witness_id);
195195
}
196196
}

src/script/standard.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::v
261261
return true;
262262
}
263263

264-
namespace
265-
{
266-
class CScriptVisitor : public boost::static_visitor<CScript>
264+
namespace {
265+
class CScriptVisitor
267266
{
268267
public:
269268
CScript operator()(const CNoDestination& dest) const
@@ -300,7 +299,7 @@ class CScriptVisitor : public boost::static_visitor<CScript>
300299

301300
CScript GetScriptForDestination(const CTxDestination& dest)
302301
{
303-
return boost::apply_visitor(CScriptVisitor(), dest);
302+
return std::visit(CScriptVisitor(), dest);
304303
}
305304

306305
CScript GetScriptForRawPubKey(const CPubKey& pubKey)
@@ -320,5 +319,5 @@ CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
320319
}
321320

322321
bool IsValidDestination(const CTxDestination& dest) {
323-
return dest.which() != 0;
322+
return dest.index() != 0;
324323
}

src/script/standard.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
#include <script/interpreter.h>
1010
#include <uint256.h>
1111

12-
#include <boost/variant.hpp>
13-
1412
#include <string>
15-
13+
#include <variant>
1614

1715
static const bool DEFAULT_ACCEPT_DATACARRIER = true;
1816

@@ -211,7 +209,7 @@ struct WitnessUnknown
211209
* (taproot outputs do not require their own type as long as no wallet support exists)
212210
* A CTxDestination is the internal data type encoded in a bitcoin address
213211
*/
214-
typedef boost::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown> CTxDestination;
212+
using CTxDestination = std::variant<CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown>;
215213

216214
/** Check whether a CTxDestination is a CNoDestination. */
217215
bool IsValidDestination(const CTxDestination& dest);

0 commit comments

Comments
 (0)