30 #include <type_traits>
39 class has_encode_decode {
40 template<
class C, C>
struct S {};
41 template<
class X>
static true_type check(S<
size_t (X::*)(Packet&)
const, &X::encode>*,
42 S<
size_t (X::*)(Packet&), &X::decode>*);
43 template<
class X>
static false_type check(...);
45 static const bool value = decltype(check<T>(0, 0))::value;
51 using detail::has_encode_decode;
54 template<
class T,
class E =
void>
75 struct Codec<T, typename enable_if<has_encode_decode<T>::value>::type> {
78 return data.encode(p);
83 return data.decode(p);
114 size_t ret =
UVarint(str.length()).encode(p);
116 return ret + str.length();
124 size_t read_bytes = 0;
126 if((read_bytes = v.decode(p)) == 0)
return 0;
129 str = std::string(reinterpret_cast<const char*>(p.
getPointer()), v.get());
131 p.
seek(v.get(), SEEK_CUR);
132 return read_bytes + v.get();
Packet & seek(off_t offset, int whence)
Seeks the packet to some offset using SEEK_SET, SEEK_CUR or SEEK_END.
size_t decode(Packet &p, T &data)
Decodes bytes from p into data, returns the number of bytes read or 0 on error.
Definition: nrg_codec.h:63
Default undeclared template instance to cause a compilation error for non-integer types...
Definition: nrg_varint.h:83
size_t remaining() const
Get the amount of data that can be read from the internal pointer's current position.
Definition: nrg_packet.h:134
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
void read(T &v)
Generic read function with endian conversion, be careful with types like size_t that differ across pl...
Definition: nrg_packet.h:115
const uint8_t * getPointer() const
Returns the internal pointer.
Definition: nrg_packet.h:137
size_t decode(Packet &p, T &data)
Calls the decode method of data to decode itself from the Packet p.
Definition: nrg_codec.h:82
Encodes and Decodes any type into a Packet object.
Definition: nrg_codec.h:55
size_t encode(Packet &p, const T &data)
Calls the encode method of data to encode itself into the Packet p.
Definition: nrg_codec.h:77
PacketReadable & readArray(uint8_t *v, size_t size)
Read an array of size size into v, make sure it's big enough!
size_t decode(Packet &p, char(&data)[len])
Decodes bytes from p into data, returns the number of bytes read or 0 on error.
Definition: nrg_codec.h:98
size_t encode(Packet &p, const std::string &str)
Encodes data into p by prefixing the string with its length as a Varint.
Definition: nrg_codec.h:113
PacketWritable & writeArray(const void *v, size_t size)
Write an array of size size, no endian conversion is performed.
Common defines and includes used by all the other nrg header files.
size_t decode(Packet &p, std::string &str)
Decodes bytes from p into data, returns the number of bytes read or 0 on error.
Definition: nrg_codec.h:120
void write(const T &v)
Generic write function with endian conversion, be careful with types like size_t that differ across p...
Definition: nrg_packet.h:94
size_t encode(Packet &p, const T &data)
Encodes data into p, and retuns the number of bytes written.
Definition: nrg_codec.h:57
Template implementation of variable-length integers, following the same format as Google's Protobufs...
size_t encode(Packet &p, const char(&data)[len])
Encodes data into p, and retuns the number of bytes written.
Definition: nrg_codec.h:92