serialization: Support for multiple parameters

This commit makes a minimal change to the ParamsStream class to let it retrieve
multiple parameters. Followup commits after this commit clean up code using
ParamsStream and make it easier to set multiple parameters.

Currently it is only possible to attach one serialization parameter to a stream
at a time. For example, it is not possible to set a parameter controlling the
transaction format and a parameter controlling the address format at the same
time because one parameter will override the other.

This limitation is inconvenient for multiprocess code since it is not possible
to create just one type of stream and serialize any object to it. Instead it is
necessary to create different streams for different object types, which
requires extra boilerplate and makes using the new parameter fields a lot more
awkward than the older version and type fields.

Fix this problem by allowing an unlimited number of serialization stream
parameters to be set, and allowing them to be requested by type. Later
parameters will still override earlier parameters, but only if they have the
same type.

This change requires replacing the stream.GetParams() method with a
stream.GetParams<T>() method in order for serialization code to retrieve the
desired parameters. This change is more verbose, but probably a good thing for
readability because previously it could be difficult to know what type the
GetParams() method would return, and now it is more obvious.
This commit is contained in:
Ryan Ofsky
2023-11-22 14:47:00 -05:00
parent 4ae5171d42
commit f3a2b52376
5 changed files with 28 additions and 31 deletions

View File

@@ -289,7 +289,7 @@ public:
template <typename Stream>
void Serialize(Stream& s) const
{
if (s.GetParams().m_base_format == BaseFormat::RAW) {
if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
s << m_base_data;
} else {
s << Span{HexStr(Span{&m_base_data, 1})};
@@ -299,7 +299,7 @@ public:
template <typename Stream>
void Unserialize(Stream& s)
{
if (s.GetParams().m_base_format == BaseFormat::RAW) {
if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
s >> m_base_data;
} else {
std::string hex{"aa"};
@@ -327,8 +327,9 @@ class Derived : public Base
public:
std::string m_derived_data;
SERIALIZE_METHODS_PARAMS(Derived, obj, DerivedAndBaseFormat, fmt)
SERIALIZE_METHODS(Derived, obj)
{
auto& fmt = SER_PARAMS(DerivedAndBaseFormat);
READWRITE(fmt.m_base_format(AsBase<Base>(obj)));
if (ser_action.ForRead()) {