mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
Merge #16889: Add some general std::vector utility functions
7d8d3e6a2ad827fa916e3909a18dedb9f7fdce43 Add tests for util/vector.h's Cat and Vector (Pieter Wuille) e65e61c812df90a56e3ce4a8e76c4b746766f387 Add some general std::vector utility functions (Pieter Wuille) Pull request description: This is another general improvement extracted from #16800 . Two functions are added are: * Vector(arg1,arg2,arg3,...) constructs a vector with the specified arguments as elements. The vector's type is derived from the arguments. If some of the arguments are rvalue references, they will be moved into place rather than copied (which can't be achieved using list initialization). * Cat(vector1,vector2) returns a concatenation of the two vectors, efficiently moving elements when relevant. Vector generalizes (and replaces) the `Singleton` function in src/descriptor.cpp, and `Cat` replaces the function in bech32.cpp ACKs for top commit: laanwj: ACK 7d8d3e6a2ad827fa916e3909a18dedb9f7fdce43 MarcoFalke: ACK 7d8d3e6a2ad827fa916e3909a18dedb9f7fdce43 (enjoyed reading the tests, but did not compile) Tree-SHA512: 92325f14e90d7e7d9d920421979aec22bb0d730e0291362b4326cccc76f9c2d865bec33a797c5c0201773468c3773cb50ce52c8eee4c1ec1a4d10db5cf2b9d2a
This commit is contained in:
parent
5d8f250270
commit
d8f96924f8
@ -327,6 +327,7 @@ BITCOIN_CORE_H = \
|
|||||||
util/vector.h \
|
util/vector.h \
|
||||||
util/url.h \
|
util/url.h \
|
||||||
util/validation.h \
|
util/validation.h \
|
||||||
|
util/vector.h \
|
||||||
validation.h \
|
validation.h \
|
||||||
validationinterface.h \
|
validationinterface.h \
|
||||||
versionbits.h \
|
versionbits.h \
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
#include <util/spanparsing.h>
|
#include <util/spanparsing.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
|
#include <util/vector.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@ -549,22 +550,13 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Construct a vector with one element, which is moved into it. */
|
|
||||||
template<typename T>
|
|
||||||
std::vector<T> Singleton(T elem)
|
|
||||||
{
|
|
||||||
std::vector<T> ret;
|
|
||||||
ret.emplace_back(std::move(elem));
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** A parsed addr(A) descriptor. */
|
/** A parsed addr(A) descriptor. */
|
||||||
class AddressDescriptor final : public DescriptorImpl
|
class AddressDescriptor final : public DescriptorImpl
|
||||||
{
|
{
|
||||||
const CTxDestination m_destination;
|
const CTxDestination m_destination;
|
||||||
protected:
|
protected:
|
||||||
std::string ToStringExtra() const override { return EncodeDestination(m_destination); }
|
std::string ToStringExtra() const override { return EncodeDestination(m_destination); }
|
||||||
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript*, FlatSigningProvider&) const override { return Singleton(GetScriptForDestination(m_destination)); }
|
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript*, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(m_destination)); }
|
||||||
public:
|
public:
|
||||||
AddressDescriptor(CTxDestination destination) : DescriptorImpl({}, {}, "addr"), m_destination(std::move(destination)) {}
|
AddressDescriptor(CTxDestination destination) : DescriptorImpl({}, {}, "addr"), m_destination(std::move(destination)) {}
|
||||||
bool IsSolvable() const final { return false; }
|
bool IsSolvable() const final { return false; }
|
||||||
@ -576,7 +568,7 @@ class RawDescriptor final : public DescriptorImpl
|
|||||||
const CScript m_script;
|
const CScript m_script;
|
||||||
protected:
|
protected:
|
||||||
std::string ToStringExtra() const override { return HexStr(m_script); }
|
std::string ToStringExtra() const override { return HexStr(m_script); }
|
||||||
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript*, FlatSigningProvider&) const override { return Singleton(m_script); }
|
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript*, FlatSigningProvider&) const override { return Vector(m_script); }
|
||||||
public:
|
public:
|
||||||
RawDescriptor(CScript script) : DescriptorImpl({}, {}, "raw"), m_script(std::move(script)) {}
|
RawDescriptor(CScript script) : DescriptorImpl({}, {}, "raw"), m_script(std::move(script)) {}
|
||||||
bool IsSolvable() const final { return false; }
|
bool IsSolvable() const final { return false; }
|
||||||
@ -586,9 +578,9 @@ public:
|
|||||||
class PKDescriptor final : public DescriptorImpl
|
class PKDescriptor final : public DescriptorImpl
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, const CScript*, FlatSigningProvider&) const override { return Singleton(GetScriptForRawPubKey(keys[0])); }
|
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, const CScript*, FlatSigningProvider&) const override { return Vector(GetScriptForRawPubKey(keys[0])); }
|
||||||
public:
|
public:
|
||||||
PKDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Singleton(std::move(prov)), {}, "pk") {}
|
PKDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), {}, "pk") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A parsed pkh(P) descriptor. */
|
/** A parsed pkh(P) descriptor. */
|
||||||
@ -599,10 +591,10 @@ protected:
|
|||||||
{
|
{
|
||||||
CKeyID id = keys[0].GetID();
|
CKeyID id = keys[0].GetID();
|
||||||
out.pubkeys.emplace(id, keys[0]);
|
out.pubkeys.emplace(id, keys[0]);
|
||||||
return Singleton(GetScriptForDestination(PKHash(id)));
|
return Vector(GetScriptForDestination(PKHash(id)));
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
PKHDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Singleton(std::move(prov)), {}, "pkh") {}
|
PKHDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), {}, "pkh") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A parsed multi(...) or sortedmulti(...) descriptor */
|
/** A parsed multi(...) or sortedmulti(...) descriptor */
|
||||||
@ -616,9 +608,9 @@ protected:
|
|||||||
if (m_sorted) {
|
if (m_sorted) {
|
||||||
std::vector<CPubKey> sorted_keys(keys);
|
std::vector<CPubKey> sorted_keys(keys);
|
||||||
std::sort(sorted_keys.begin(), sorted_keys.end());
|
std::sort(sorted_keys.begin(), sorted_keys.end());
|
||||||
return Singleton(GetScriptForMultisig(m_threshold, sorted_keys));
|
return Vector(GetScriptForMultisig(m_threshold, sorted_keys));
|
||||||
}
|
}
|
||||||
return Singleton(GetScriptForMultisig(m_threshold, keys));
|
return Vector(GetScriptForMultisig(m_threshold, keys));
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
MultisigDescriptor(int threshold, std::vector<std::unique_ptr<PubkeyProvider>> providers, bool sorted = false) : DescriptorImpl(std::move(providers), {}, sorted ? "sortedmulti" : "multi"), m_threshold(threshold), m_sorted(sorted) {}
|
MultisigDescriptor(int threshold, std::vector<std::unique_ptr<PubkeyProvider>> providers, bool sorted = false) : DescriptorImpl(std::move(providers), {}, sorted ? "sortedmulti" : "multi"), m_threshold(threshold), m_sorted(sorted) {}
|
||||||
@ -628,7 +620,7 @@ public:
|
|||||||
class SHDescriptor final : public DescriptorImpl
|
class SHDescriptor final : public DescriptorImpl
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript* script, FlatSigningProvider&) const override { return Singleton(GetScriptForDestination(ScriptHash(*script))); }
|
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript* script, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(ScriptHash(*script))); }
|
||||||
public:
|
public:
|
||||||
SHDescriptor(std::unique_ptr<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), "sh") {}
|
SHDescriptor(std::unique_ptr<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), "sh") {}
|
||||||
};
|
};
|
||||||
@ -653,7 +645,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ComboDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Singleton(std::move(prov)), {}, "combo") {}
|
ComboDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), {}, "combo") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user