danicoin/src/Serialization/ISerializer.h

81 lines
2.3 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-09-15 17:33:47 +00:00
#pragma once
#include <string>
#include <cstdint>
2015-07-15 12:23:00 +00:00
#include <Common/StringView.h>
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2014-09-15 17:33:47 +00:00
class ISerializer {
public:
enum SerializerType {
INPUT,
OUTPUT
};
virtual ~ISerializer() {}
virtual SerializerType type() const = 0;
2015-07-15 12:23:00 +00:00
virtual bool beginObject(Common::StringView name) = 0;
virtual void endObject() = 0;
2015-07-30 15:22:07 +00:00
virtual bool beginArray(size_t& size, Common::StringView name) = 0;
2015-07-15 12:23:00 +00:00
virtual void endArray() = 0;
virtual bool operator()(uint8_t& value, Common::StringView name) = 0;
2015-07-30 15:22:07 +00:00
virtual bool operator()(int16_t& value, Common::StringView name) = 0;
virtual bool operator()(uint16_t& value, Common::StringView name) = 0;
2015-07-15 12:23:00 +00:00
virtual bool operator()(int32_t& value, Common::StringView name) = 0;
virtual bool operator()(uint32_t& value, Common::StringView name) = 0;
virtual bool operator()(int64_t& value, Common::StringView name) = 0;
virtual bool operator()(uint64_t& value, Common::StringView name) = 0;
virtual bool operator()(double& value, Common::StringView name) = 0;
virtual bool operator()(bool& value, Common::StringView name) = 0;
virtual bool operator()(std::string& value, Common::StringView name) = 0;
// read/write binary block
2015-07-30 15:22:07 +00:00
virtual bool binary(void* value, size_t size, Common::StringView name) = 0;
2015-07-15 12:23:00 +00:00
virtual bool binary(std::string& value, Common::StringView name) = 0;
2014-09-15 17:33:47 +00:00
template<typename T>
2015-07-15 12:23:00 +00:00
bool operator()(T& value, Common::StringView name);
2014-09-15 17:33:47 +00:00
};
template<typename T>
2015-07-15 12:23:00 +00:00
bool ISerializer::operator()(T& value, Common::StringView name) {
return serialize(value, name, *this);
}
template<typename T>
bool serialize(T& value, Common::StringView name, ISerializer& serializer) {
if (!serializer.beginObject(name)) {
return false;
}
serialize(value, serializer);
serializer.endObject();
return true;
2014-09-15 17:33:47 +00:00
}
template<typename T>
2015-07-15 12:23:00 +00:00
void serialize(T& value, ISerializer& serializer) {
value.serialize(serializer);
2014-09-15 17:33:47 +00:00
}
2015-07-30 15:22:07 +00:00
#ifdef __clang__
template<> inline
bool ISerializer::operator()(size_t& value, Common::StringView name) {
return operator()(*reinterpret_cast<uint64_t*>(&value), name);
}
#endif
2015-07-15 12:23:00 +00:00
#define KV_MEMBER(member) s(member, #member);
2014-09-15 17:33:47 +00:00
}