Skip to content

Commit 539db35

Browse files
sipafurszy
authored andcommitted
Support deserializing into temporaries
Currently, the READWRITE macro cannot be passed any non-const temporaries, as the SerReadWrite function only accepts lvalue references. Deserializing into a temporary is very common, however. See for example things like 's >> VARINT(n)'. The VARINT macro produces a temporary wrapper that holds a reference to n. Fix this by accepting non-const rvalue references instead of lvalue references. We don't propagate the rvalue-ness down, as there are no useful optimizations that only apply to temporaries. Then use this new functionality to get rid of many (but not all) uses of the 'REF' macro (which casts away constness).
1 parent 36db7fd commit 539db35

File tree

8 files changed

+27
-28
lines changed

8 files changed

+27
-28
lines changed

src/coins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Coin
7979
nHeight = code >> 2;
8080
fCoinBase = code & 2;
8181
fCoinStake = code & 1;
82-
::Unserialize(s, REF(CTxOutCompressor(out)));
82+
::Unserialize(s, CTxOutCompressor(out));
8383
}
8484

8585
bool IsSpent() const {

src/compressor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class CScriptCompressor
7878
s >> VARINT(nSize);
7979
if (nSize < nSpecialScripts) {
8080
std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
81-
s >> REF(CFlatData(vch));
81+
s >> CFlatData(vch);
8282
Decompress(nSize, vch);
8383
return;
8484
}
@@ -89,7 +89,7 @@ class CScriptCompressor
8989
s.ignore(nSize);
9090
} else {
9191
script.resize(nSize);
92-
s >> REF(CFlatData(script));
92+
s >> CFlatData(script);
9393
}
9494
}
9595
};

src/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class CHashVerifier : public CHashWriter
340340
}
341341

342342
template<typename T>
343-
CHashVerifier<Source>& operator>>(T& obj)
343+
CHashVerifier<Source>& operator>>(T&& obj)
344344
{
345345
// Unserialize from this stream
346346
::Unserialize(*this, obj);

src/script/bitcoinconsensus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TxInputStream
4040
}
4141

4242
template<typename T>
43-
TxInputStream& operator>>(T& obj)
43+
TxInputStream& operator>>(T&& obj)
4444
{
4545
::Unserialize(*this, obj);
4646
return *this;

src/serialize.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,10 @@ I ReadVarInt(Stream& is)
394394
}
395395
}
396396

397-
#define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
398-
#define VARINT(obj) REF(WrapVarInt(REF(obj)))
399-
#define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
400-
#define LIMITED_STRING(obj, n) REF(LimitedString<n>(REF(obj)))
397+
#define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))
398+
#define VARINT(obj) WrapVarInt(REF(obj))
399+
#define COMPACTSIZE(obj) CCompactSize(REF(obj))
400+
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
401401

402402
/**
403403
* Wrapper for serializing arrays and POD.
@@ -680,8 +680,8 @@ inline void Serialize(Stream& os, const T& a)
680680
a.Serialize(os);
681681
}
682682

683-
template <typename Stream, typename T>
684-
inline void Unserialize(Stream& is, T& a)
683+
template<typename Stream, typename T>
684+
inline void Unserialize(Stream& is, T&& a)
685685
{
686686
a.Unserialize(is);
687687
}
@@ -1058,10 +1058,10 @@ void SerializeMany(Stream& s)
10581058
}
10591059

10601060
template<typename Stream, typename Arg, typename... Args>
1061-
void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
1061+
void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
10621062
{
1063-
::Serialize(s, std::forward<Arg>(arg));
1064-
::SerializeMany(s, std::forward<Args>(args)...);
1063+
::Serialize(s, arg);
1064+
::SerializeMany(s, args...);
10651065
}
10661066

10671067
template<typename Stream>
@@ -1070,20 +1070,20 @@ inline void UnserializeMany(Stream& s)
10701070
}
10711071

10721072
template<typename Stream, typename Arg, typename... Args>
1073-
inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
1073+
inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
10741074
{
10751075
::Unserialize(s, arg);
10761076
::UnserializeMany(s, args...);
10771077
}
10781078

10791079
template<typename Stream, typename... Args>
1080-
inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
1080+
inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, const Args&... args)
10811081
{
1082-
::SerializeMany(s, std::forward<Args>(args)...);
1082+
::SerializeMany(s, args...);
10831083
}
10841084

10851085
template<typename Stream, typename... Args>
1086-
inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
1086+
inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&... args)
10871087
{
10881088
::UnserializeMany(s, args...);
10891089
}

src/streams.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class CBaseDataStream
279279
}
280280

281281
template <typename T>
282-
CBaseDataStream& operator>>(T& obj)
282+
CBaseDataStream& operator>>(T&& obj)
283283
{
284284
// Unserialize from this stream
285285
::Unserialize(*this, obj);
@@ -503,8 +503,8 @@ class CAutoFile
503503
return (*this);
504504
}
505505

506-
template <typename T>
507-
CAutoFile& operator>>(T& obj)
506+
template<typename T>
507+
CAutoFile& operator>>(T&& obj)
508508
{
509509
// Unserialize from this stream
510510
if (!file)
@@ -650,9 +650,8 @@ class CBufferedFile
650650
return true;
651651
}
652652

653-
template <typename T>
654-
CBufferedFile& operator>>(T& obj)
655-
{
653+
template<typename T>
654+
CBufferedFile& operator>>(T&& obj) {
656655
// Unserialize from this stream
657656
::Unserialize(*this, obj);
658657
return (*this);

src/txdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ class CCoins
573573
vout.assign(vAvail.size(), CTxOut());
574574
for (unsigned int i = 0; i < vAvail.size(); i++) {
575575
if (vAvail[i])
576-
::Unserialize(s, REF(CTxOutCompressor(vout[i])));
576+
::Unserialize(s, CTxOutCompressor(vout[i]));
577577
}
578578
// coinbase height
579579
::Unserialize(s, VARINT(nHeight));

src/undo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TxInUndoDeserializer
5858
int nVersionDummy;
5959
::Unserialize(s, VARINT(nVersionDummy));
6060
}
61-
::Unserialize(s, REF(CTxOutCompressor(REF(txout->out))));
61+
::Unserialize(s, CTxOutCompressor(REF(txout->out)));
6262
}
6363

6464
TxInUndoDeserializer(Coin* coin) : txout(coin) {}
@@ -80,7 +80,7 @@ class CTxUndo
8080
uint64_t count = vprevout.size();
8181
::Serialize(s, COMPACTSIZE(REF(count)));
8282
for (const auto& prevout : vprevout) {
83-
::Serialize(s, REF(TxInUndoSerializer(&prevout)));
83+
::Serialize(s, TxInUndoSerializer(&prevout));
8484
}
8585
}
8686

@@ -95,7 +95,7 @@ class CTxUndo
9595
}
9696
vprevout.resize(count);
9797
for (auto& prevout : vprevout) {
98-
::Unserialize(s, REF(TxInUndoDeserializer(&prevout)));
98+
::Unserialize(s, TxInUndoDeserializer(&prevout));
9999
}
100100
}
101101
};

0 commit comments

Comments
 (0)