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
{
public:
- typedef T ValueType;
-
BasicSerializer(const Protocol &) { }
std::uint64_t get_hash() const { return Traits<T>::signature; }
class StringSerializer
{
- public:
- typedef std::string ValueType;
-
private:
BasicSerializer<std::uint16_t> length_serializer;
template<typename A>
class ArraySerializer
{
- public:
- typedef A ValueType;
-
private:
BasicSerializer<std::uint16_t> length_serializer;
typename Traits<typename A::value_type>::Serializer element_serializer;
template<typename C>
class CompoundSerializer
{
- public:
- 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 T>
+ class FieldSerializer: public Head
{
public:
template<typename N>
- struct Next
- {
- typedef Serializer<P, Serializer<P, Head, S>, typename Traits<N>::Serializer> Type;
- };
+ using Next = FieldSerializer<C, FieldSerializer<C, Head, T>, N>;
private:
- typedef typename S::ValueType P::*Pointer;
-
- Pointer ptr;
- S ser;
+ T C::*ptr;
+ typename Traits<T>::Serializer ser;
public:
- Serializer(const Head &, Pointer, const Protocol &);
+ FieldSerializer(const Head &, T C::*, 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;
- };
-
- 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; }
+ using Next = FieldSerializer<C, Serializer<C>, N>;
+
+ 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;
PacketDefBuilder(const Protocol &, PacketTypeDef<P> &, const S &);
template<typename T>
- PacketDefBuilder<P, typename S::template Next<T>::Type> operator()(T P::*);
+ PacketDefBuilder<P, typename S::template Next<T>> operator()(T P::*);
};
struct PacketHeader
{
- std::uint16_t type;
- std::uint16_t length;
+ std::uint16_t type = 0;
+ std::uint16_t length = 0;
- PacketHeader();
+ PacketHeader() = default;
PacketHeader(std::uint16_t, std::uint16_t);
};
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):
+template<typename C, typename Head, typename T>
+Protocol::FieldSerializer<C, Head, T>::FieldSerializer(const Head &h, T C::*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
+template<typename C, typename Head, typename T>
+std::uint64_t Protocol::FieldSerializer<C, Head, T>::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
+template<typename C, typename Head, typename T>
+char *Protocol::FieldSerializer<C, Head, T>::serialize(const C &com, char *buf, char *end) const
{
- buf = Head::serialize(pkt, buf, end);
- return ser.serialize(pkt.*ptr, buf, end);
+ buf = Head::serialize(com, buf, end);
+ return ser.serialize(com.*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
+template<typename C, typename Head, typename T>
+const char *Protocol::FieldSerializer<C, Head, T>::deserialize(C &com, const char *buf, const char *end) const
{
- buf = Head::deserialize(pkt, buf, end);
- return ser.deserialize(pkt.*ptr, buf, end);
+ buf = Head::deserialize(com, buf, end);
+ return ser.deserialize(com.*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>
template<typename P, typename S>
template<typename T>
-Protocol::PacketDefBuilder<P, typename S::template Next<T>::Type> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
+Protocol::PacketDefBuilder<P, typename S::template Next<T>> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
{
- typename S::template Next<T>::Type next_ser(serializer, ptr, protocol);
+ typename S::template Next<T> next_ser(serializer, ptr, protocol);
pktdef.set_serializer(next_ser);
- return PacketDefBuilder<P, typename S::template Next<T>::Type>(protocol, pktdef, next_ser);
+ return PacketDefBuilder<P, typename S::template Next<T>>(protocol, pktdef, next_ser);
}
} // namespace Net