Add serialization for primitive boost::optional<T>.

This commit is contained in:
Sean Bowe
2016-03-28 00:16:22 -06:00
parent b2cf9ba300
commit 291b191bd7
2 changed files with 81 additions and 0 deletions

View File

@@ -21,6 +21,7 @@
#include <vector>
#include <boost/array.hpp>
#include <boost/optional.hpp>
class CScript;
@@ -508,6 +509,13 @@ extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVe
template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion);
template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion);
/**
* optional
*/
template<typename T> unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion);
template<typename Stream, typename T> void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion);
template<typename Stream, typename T> void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion);
/**
* array
*/
@@ -707,6 +715,52 @@ void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
}
/**
* optional
*/
template<typename T>
unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion)
{
if (item) {
return 1 + GetSerializeSize(*item, nType, nVersion);
} else {
return 1;
}
}
template<typename Stream, typename T>
void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion)
{
// If the value is there, put 0x01 and then serialize the value.
// If it's not, put 0x00.
if (item) {
unsigned char discriminant = 0x01;
Serialize(os, discriminant, nType, nVersion);
Serialize(os, *item, nType, nVersion);
} else {
unsigned char discriminant = 0x00;
Serialize(os, discriminant, nType, nVersion);
}
}
template<typename Stream, typename T>
void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion)
{
unsigned char discriminant = 0x00;
Unserialize(is, discriminant, nType, nVersion);
if (discriminant == 0x00) {
item = boost::none;
} else {
T object;
Unserialize(is, object, nType, nVersion);
item = object;
}
}
/**
* array
*/