struct Traits;
template<typename C>
- struct CompoundTypeDef
- {
- virtual ~CompoundTypeDef() = default;
-
- virtual std::uint64_t get_hash() const = 0;
- virtual char *serialize(const C &, char *, char *) const = 0;
- virtual const char *deserialize(C &, const char *, const char *) const = 0;
- };
-
- template<typename C, typename S>
- struct CompoundDef: public CompoundTypeDef<C>
- {
- S serializer;
-
- CompoundDef(const S &);
-
- std::uint64_t get_hash() const override;
- char *serialize(const C &, char *, char *) const override;
- const char *deserialize(C &, const char *, const char *) const override;
- };
+ class Serializer;
template<typename T>
class BasicSerializer
typedef C ValueType;
private:
- const CompoundTypeDef<C> &def;
+ const Serializer<C> &serializer;
public:
CompoundSerializer(const Protocol &);
+ std::uint64_t get_hash() const;
char *serialize(const C &, char *, char *) const;
const char *deserialize(C &, const char *, const char *) const;
};
- template<typename P, typename Head, typename S>
- class Serializer: public Head
+ template<typename C, typename Head, typename S>
+ class FieldSerializer: public Head
{
public:
template<typename N>
struct Next
{
- typedef Serializer<P, Serializer<P, Head, S>, typename Traits<N>::Serializer> Type;
+ typedef FieldSerializer<C, FieldSerializer<C, Head, S>, typename Traits<N>::Serializer> Type;
};
private:
- typedef typename S::ValueType P::*Pointer;
+ typedef typename S::ValueType C::*Pointer;
Pointer ptr;
S ser;
public:
- Serializer(const Head &, Pointer, const Protocol &);
+ FieldSerializer(const Head &, Pointer, const Protocol &);
std::uint64_t get_hash() const;
- char *serialize(const P &, char *, char *) const;
- const char *deserialize(P &, const char *, const char *) const;
+ char *serialize(const C &, char *, char *) const;
+ const char *deserialize(C &, const char *, const char *) const;
};
- template<typename P>
- class NullSerializer
+ template<typename C>
+ class Serializer
{
public:
template<typename N>
struct Next
{
- typedef Serializer<P, NullSerializer, typename Traits<N>::Serializer> Type;
+ typedef FieldSerializer<C, Serializer<C>, typename Traits<N>::Serializer> Type;
};
- std::uint64_t get_hash() const { return 0; }
- char *serialize(const P &, char *b, char *) const { return b; }
- const char *deserialize(P &, const char *b, const char *) const { return b; }
+ virtual ~Serializer() = default;
+
+ virtual std::uint64_t get_hash() const { return 0; }
+ virtual char *serialize(const C &, char *b, char *) const { return b; }
+ virtual const char *deserialize(C &, const char *b, const char *) const { return b; }
};
class PacketDefBase
class PacketTypeDef: public PacketDefBase
{
private:
- CompoundTypeDef<P> *compound;
+ Serializer<P> *serializer;
public:
PacketTypeDef(unsigned);
template<typename S>
void set_serializer(const S &);
- const CompoundTypeDef<P> &get_compound() const { return *compound; }
+ const Serializer<P> &get_serializer() const { return *serializer; }
- std::uint64_t get_hash() const override { return compound->get_hash(); }
+ std::uint64_t get_hash() const override { return serializer->get_hash(); }
char *serialize(const P &, char *, char *) const;
const char *deserialize(P &, const char *, const char *) const;
const char *dispatch(ReceiverBase &, const char *, const char *) const override;
protected:
template<typename P>
- PacketDefBuilder<P, NullSerializer<P> > add(unsigned);
+ PacketDefBuilder<P, Serializer<P>> add(unsigned);
template<typename P>
- PacketDefBuilder<P, NullSerializer<P> > add();
+ PacketDefBuilder<P, Serializer<P>> add();
const PacketDefBase &get_packet_by_class_id(unsigned) const;
const PacketDefBase &get_packet_by_id(unsigned) const;
}
template<typename P>
-Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add(unsigned id)
+Protocol::PacketDefBuilder<P, Protocol::Serializer<P>> Protocol::add(unsigned id)
{
PacketTypeDef<P> *pdef = new PacketTypeDef<P>(id);
add_packet(pdef);
- return PacketDefBuilder<P, NullSerializer<P> >(*this, *pdef, NullSerializer<P>());
+ return PacketDefBuilder<P, Serializer<P>>(*this, *pdef, Serializer<P>());
}
template<typename P>
-Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
+Protocol::PacketDefBuilder<P, Protocol::Serializer<P>> Protocol::add()
{
return add<P>(next_packet_id++);
}
};
template<typename T>
-struct Protocol::Traits<std::vector<T> >
+struct Protocol::Traits<std::vector<T>>
{
static const std::uint16_t signature = 'A';
- typedef ArraySerializer<std::vector<T> > Serializer;
+ typedef ArraySerializer<std::vector<T>> Serializer;
};
-
-template<typename C, typename S>
-Protocol::CompoundDef<C, S>::CompoundDef(const S &s):
- serializer(s)
-{ }
-
-template<typename C, typename S>
-std::uint64_t Protocol::CompoundDef<C, S>::get_hash() const
-{
- return hash_round<64>(serializer.get_hash(), 'C');
-}
-
-template<typename C, typename S>
-char *Protocol::CompoundDef<C, S>::serialize(const C &com, char *buf, char *end) const
-{
- return serializer.serialize(com, buf, end);
-}
-
-template<typename C, typename S>
-const char *Protocol::CompoundDef<C, S>::deserialize(C &com, const char *buf, const char *end) const
-{
- return serializer.deserialize(com, buf, end);
-}
-
-
inline std::uint64_t Protocol::StringSerializer::get_hash() const
{
return Traits<std::string>::signature;
template<typename C>
Protocol::CompoundSerializer<C>::CompoundSerializer(const Protocol &proto):
- def(proto.get_packet_by_class<C>().get_compound())
+ serializer(proto.get_packet_by_class<C>().get_serializer())
{ }
+template<typename C>
+std::uint64_t Protocol::CompoundSerializer<C>::get_hash() const
+{
+ return hash_round<64>(serializer.get_hash(), 'C');
+}
+
template<typename C>
char *Protocol::CompoundSerializer<C>::serialize(const C &com, char *buf, char *end) const
{
- return def.serialize(com, buf, end);
+ return serializer.serialize(com, buf, end);
}
template<typename C>
const char *Protocol::CompoundSerializer<C>::deserialize(C &com, const char *buf, const char *end) const
{
- return def.deserialize(com, buf, end);
+ return serializer.deserialize(com, buf, end);
}
template<typename P, typename Head, typename S>
-Protocol::Serializer<P, Head, S>::Serializer(const Head &h, Pointer p, const Protocol &proto):
+Protocol::FieldSerializer<P, Head, S>::FieldSerializer(const Head &h, Pointer p, const Protocol &proto):
Head(h),
ptr(p),
ser(proto)
{ }
template<typename P, typename Head, typename S>
-std::uint64_t Protocol::Serializer<P, Head, S>::get_hash() const
+std::uint64_t Protocol::FieldSerializer<P, Head, S>::get_hash() const
{
return hash_update<64>(Head::get_hash(), ser.get_hash());
}
template<typename P, typename Head, typename S>
-char *Protocol::Serializer<P, Head, S>::serialize(const P &pkt, char *buf, char *end) const
+char *Protocol::FieldSerializer<P, Head, S>::serialize(const P &pkt, char *buf, char *end) const
{
buf = Head::serialize(pkt, buf, end);
return ser.serialize(pkt.*ptr, buf, end);
}
template<typename P, typename Head, typename S>
-const char *Protocol::Serializer<P, Head, S>::deserialize(P &pkt, const char *buf, const char *end) const
+const char *Protocol::FieldSerializer<P, Head, S>::deserialize(P &pkt, const char *buf, const char *end) const
{
buf = Head::deserialize(pkt, buf, end);
return ser.deserialize(pkt.*ptr, buf, end);
template<typename P>
Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
PacketDefBase(i),
- compound(new CompoundDef<P, NullSerializer<P> >(NullSerializer<P>()))
+ serializer(new Serializer<P>)
{ }
template<typename P>
Protocol::PacketTypeDef<P>::~PacketTypeDef()
{
- delete compound;
+ delete serializer;
}
template<typename P>
template<typename S>
void Protocol::PacketTypeDef<P>::set_serializer(const S &ser)
{
- delete compound;
- compound = new CompoundDef<P, S>(ser);
+ delete serializer;
+ serializer = new S(ser);
}
template<typename P>
char *Protocol::PacketTypeDef<P>::serialize(const P &pkt, char *buf, char *end) const
{
- return compound->serialize(pkt, buf, end);
+ return serializer->serialize(pkt, buf, end);
}
template<typename P>
const char *Protocol::PacketTypeDef<P>::deserialize(P &pkt, const char *buf, const char *end) const
{
- return compound->deserialize(pkt, buf, end);
+ return serializer->deserialize(pkt, buf, end);
}
template<typename P>