CatapultServer  v0.5.0.1 (Elephant)
IdentifierGroupSerializer.h
Go to the documentation of this file.
1 
21 #pragma once
23 #include "catapult/io/PodIoUtils.h"
25 
26 namespace catapult { namespace cache {
27 
29  template<typename TDescriptor>
31  private:
32  using KeyType = typename TDescriptor::KeyType;
33  using ValueType = typename TDescriptor::ValueType; // this is expected to be IdentifierGroup
34 
35  public:
37  static std::string SerializeValue(const ValueType& value) {
38  io::StringOutputStream output(Size(value));
39 
40  io::Write(output, value.key());
41  io::Write64(output, static_cast<uint64_t>(value.size()));
42  for (const auto& identifier : value.identifiers())
43  Write(output, identifier);
44 
45  return output.str();
46  }
47 
49  static ValueType DeserializeValue(const RawBuffer& buffer) {
51  auto key = io::Read<typename ValueType::GroupingKeyType>(input);
52  ValueType value(key);
53 
54  auto size = io::Read64(input);
55  for (auto i = 0u; i < size; ++i) {
56  typename ValueType::Identifiers::key_type identifier;
57  Read(input, identifier);
58  value.add(identifier);
59  }
60 
61  return value;
62  }
63 
64  private:
65  template<typename T>
66  static void Read(io::InputStream& input, T& value) {
67  io::Read(input, value);
68  }
69 
70  template<size_t N, typename TTag>
71  static void Read(io::InputStream& input, utils::ByteArray<N, TTag>& value) {
72  input.read(value);
73  }
74 
75  template<typename T>
76  static void Write(io::OutputStream& input, const T& value) {
77  io::Write(input, value);
78  }
79 
80  template<size_t N, typename TTag>
81  static void Write(io::OutputStream& input, const utils::ByteArray<N, TTag>& value) {
82  input.write(value);
83  }
84 
85  private:
86  static size_t Size(const ValueType& value) {
87  return sizeof(KeyType) + value.identifiers().size() * sizeof(ValueType);
88  }
89  };
90 }}
catapult::io::Read
void Read(TIo &input, utils::BasicBaseValue< TValue, TTag, TBaseValue > &value)
Reads base value from input.
Definition: PodIoUtils.h:60
catapult::io::StringOutputStream::str
const std::string & str() const
Returns underlying string.
Definition: StringOutputStream.h:44
catapult::cache::IdentifierGroupSerializer::Write
static void Write(io::OutputStream &input, const utils::ByteArray< N, TTag > &value)
Definition: IdentifierGroupSerializer.h:81
catapult::io::OutputStream::write
virtual void write(const RawBuffer &buffer)=0
catapult::io::InputStream
Reader interface.
Definition: Stream.h:27
catapult::cache::IdentifierGroupSerializer::SerializeValue
static std::string SerializeValue(const ValueType &value)
Serializes value to string.
Definition: IdentifierGroupSerializer.h:37
catapult::cache::IdentifierGroupSerializer::Read
static void Read(io::InputStream &input, utils::ByteArray< N, TTag > &value)
Definition: IdentifierGroupSerializer.h:71
catapult::cache::IdentifierGroupSerializer::Read
static void Read(io::InputStream &input, T &value)
Definition: IdentifierGroupSerializer.h:66
catapult::cache::IdentifierGroupSerializer::Write
static void Write(io::OutputStream &input, const T &value)
Definition: IdentifierGroupSerializer.h:76
catapult::io::Write
void Write(TIo &output, const utils::BasicBaseValue< TValue, TTag, TBaseValue > &value)
Writes base value into output.
Definition: PodIoUtils.h:30
size
uint64_t size
Definition: MemoryCounters.cpp:65
catapult::io::Write64
void Write64(TIo &output, uint64_t value)
Writes value into output.
Definition: PodIoUtils.h:36
catapult::cache::IdentifierGroupSerializer::Size
static size_t Size(const ValueType &value)
Definition: IdentifierGroupSerializer.h:86
catapult::cache::IdentifierGroupSerializer
Serializer for identifier group elements.
Definition: IdentifierGroupSerializer.h:30
catapult::io::BufferInputStreamAdapter
Adapt a typed buffer to be used as an input stream.
Definition: BufferInputStreamAdapter.h:30
StringOutputStream.h
catapult::io::InputStream::read
virtual void read(const MutableRawBuffer &buffer)=0
catapult::cache::IdentifierGroupSerializer< SecretLockInfoCacheTypes::HeightGroupingTypesDescriptor >::KeyType
typename SecretLockInfoCacheTypes::HeightGroupingTypesDescriptor ::KeyType KeyType
Definition: IdentifierGroupSerializer.h:32
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::io::Read64
auto Read64(TIo &input)
Reads value from input.
Definition: PodIoUtils.h:66
PodIoUtils.h
catapult::io::StringOutputStream
String output stream.
Definition: StringOutputStream.h:27
catapult::cache::IdentifierGroupSerializer::DeserializeValue
static ValueType DeserializeValue(const RawBuffer &buffer)
Deserializes value from buffer.
Definition: IdentifierGroupSerializer.h:49
catapult::utils::ByteArray
Base class for wrappers of byte array types, to provide some type-safety.
Definition: ByteArray.h:29
BufferInputStreamAdapter.h
catapult::io::OutputStream
Writer interface.
Definition: Stream.h:41
catapult::utils::RawBuffer
BasicRawBuffer< const uint8_t > RawBuffer
A const binary buffer.
Definition: RawBuffer.h:62
catapult::cache::IdentifierGroupSerializer< SecretLockInfoCacheTypes::HeightGroupingTypesDescriptor >::ValueType
typename SecretLockInfoCacheTypes::HeightGroupingTypesDescriptor ::ValueType ValueType
Definition: IdentifierGroupSerializer.h:33