Simplifying some templates

This commit is contained in:
Alex Yatskov 2012-02-23 19:47:28 -08:00
parent e16c9b8402
commit 1f5dc98de4
10 changed files with 73 additions and 109 deletions

View File

@ -99,10 +99,9 @@ int Buffer::bytes() const {
return m_data.size(); return m_data.size();
} }
bool Buffer::serialize(Serializer* serializer) const { void Buffer::serialize(Serializer* serializer) const {
serializer->write(bytes()); serializer->write(bytes());
serializer->writeRaw(data(), bytes()); serializer->writeRaw(data(), bytes());
return true;
} }
bool Buffer::deserialize(Deserializer* deserializer) { bool Buffer::deserialize(Deserializer* deserializer) {

View File

@ -48,7 +48,7 @@ public:
void* data(); void* data();
int bytes() const; int bytes() const;
bool serialize(Serializer* serializer) const; void serialize(Serializer* serializer) const;
bool deserialize(Deserializer* deserializer); bool deserialize(Deserializer* deserializer);
private: private:

View File

@ -32,37 +32,9 @@ namespace metacall {
// Meta // Meta
// //
qword serialize(Serializer*, ...);
template <typename T> template <typename T>
struct HasGlobalSerializer { struct HasSerializer {
enum { template <typename U, void (U::*)(Serializer*) const>
Value = sizeof(bool) == sizeof(
serialize(
static_cast<Serializer*>(NULL),
*static_cast<const T*>(NULL)
)
)
};
};
qword deserialize(Deserializer*, ...);
template <typename T>
struct HasGlobalDeserializer {
enum {
Value = sizeof(bool) == sizeof(
deserialize(
static_cast<Deserializer*>(NULL),
static_cast<T*>(NULL)
)
)
};
};
template <typename T>
struct HasLocalSerializer {
template <typename U, bool (U::*)(Serializer*) const>
struct Signature { }; struct Signature { };
template <typename U> template <typename U>
@ -77,7 +49,7 @@ struct HasLocalSerializer {
}; };
template <typename T> template <typename T>
struct HasLocalDeserializer { struct HasDeserializer {
template <typename U, bool (U::*)(Deserializer*)> template <typename U, bool (U::*)(Deserializer*)>
struct Signature { }; struct Signature { };

View File

@ -55,10 +55,9 @@ struct PacketHeader {
{ {
} }
bool serialize(Serializer* serializer) const { void serialize(Serializer* serializer) const {
return serializer->write(magic);
serializer->write(magic) && serializer->write(id);
serializer->write(id) &&
serializer->write(size); serializer->write(size);
} }
@ -90,10 +89,9 @@ struct PacketInvokeRequest {
Id = PACKET_ID_FUNCTION_CALL_REQUEST Id = PACKET_ID_FUNCTION_CALL_REQUEST
}; };
bool serialize(Serializer* serializer) const { void serialize(Serializer* serializer) const {
return serializer->write(function);
serializer->write(function) && serializer->write(taskId);
serializer->write(taskId) &&
serializer->write(data); serializer->write(data);
} }
@ -130,10 +128,9 @@ struct PacketInvokeReply {
FLAG_INVALID_ARGS = 1 << 1 FLAG_INVALID_ARGS = 1 << 1
}; };
bool serialize(Serializer* serializer) const { void serialize(Serializer* serializer) const {
return serializer->write(flags);
serializer->write(flags) && serializer->write(taskId);
serializer->write(taskId) &&
serializer->write(data); serializer->write(data);
} }

View File

@ -31,8 +31,8 @@ namespace metacall {
// //
template <typename T, typename C, typename A> template <typename T, typename C, typename A>
bool serialize(Serializer* serializer, const std::basic_string<T, C, A>& value) { void serialize(Serializer* serializer, const std::basic_string<T, C, A>& value) {
return serialize(serializer, value.c_str()); serialize(serializer, value.c_str());
} }
template <typename T, typename C, typename A> template <typename T, typename C, typename A>
@ -57,13 +57,12 @@ bool deserialize(Deserializer* deserializer, std::basic_string<T, C, A>* value)
// //
template <typename T, typename A> template <typename T, typename A>
bool serialize(Serializer* serializer, const std::vector<T, A>& value) { void serialize(Serializer* serializer, const std::vector<T, A>& value) {
serializer->write(value.size()); serializer->write(value.size());
for (const typename std::vector<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) { for (const typename std::vector<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
serializer->write(*iter); serializer->write(*iter);
} }
return true;
} }
template <typename T, typename A> template <typename T, typename A>
@ -93,13 +92,11 @@ bool deserialize(Deserializer* deserializer, std::vector<T, A>* value) {
// //
template <typename T, typename A> template <typename T, typename A>
bool serialize(Serializer* serializer, const std::list<T, A>& value) { void serialize(Serializer* serializer, const std::list<T, A>& value) {
serializer->write(value.size()); serializer->write(value.size());
for (const typename std::list<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) { for (const typename std::list<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
serializer->write(*iter); serializer->write(*iter);
} }
return true;
} }
template <typename T, typename A> template <typename T, typename A>
@ -129,7 +126,7 @@ bool deserialize(Deserializer* deserializer, std::list<T, A>* value) {
// //
template <typename T1, typename T2> template <typename T1, typename T2>
bool serialize(Serializer* serializer, const std::pair<T1, T2>& value) { void serialize(Serializer* serializer, const std::pair<T1, T2>& value) {
serializer->write(value.first); serializer->write(value.first);
serializer->write(value.second); serializer->write(value.second);
return true; return true;
@ -146,13 +143,11 @@ bool deserialize(Deserializer* deserializer, std::pair<T1, T2>* value) {
// //
template <typename K, typename V, typename C, typename A> template <typename K, typename V, typename C, typename A>
bool serialize(Serializer* serializer, const std::map<K, V, C, A>& value) { void serialize(Serializer* serializer, const std::map<K, V, C, A>& value) {
serializer->write(value.size()); serializer->write(value.size());
for (const typename std::map<K, V, C, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) { for (const typename std::map<K, V, C, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
serializer->write(*iter); serializer->write(*iter);
} }
return true;
} }
template <typename K, typename V, typename C, typename A> template <typename K, typename V, typename C, typename A>

View File

@ -41,11 +41,10 @@ static int strLen(const wchar_t str[]) {
} }
template <typename T> template <typename T>
static bool serializeStr(Serializer* serializer, const T value[]) { static void serializeStr(Serializer* serializer, const T value[]) {
const int length = value == NULL ? 0 : strLen(value) + 1; const int length = value == NULL ? 0 : strLen(value) + 1;
serializer->write(length); serializer->write(length);
serializer->writeRaw(value, length * sizeof(T)); serializer->writeRaw(value, length * sizeof(T));
return true;
} }
template <typename T> template <typename T>
@ -70,16 +69,16 @@ static bool deserializeStr(Deserializer* deserializer, const T ** value) {
// C strings // C strings
// //
bool serialize(Serializer* serializer, const char value[]) { void serialize(Serializer* serializer, const char value[]) {
return serializeStr(serializer, value); serializeStr(serializer, value);
} }
bool deserialize(Deserializer* deserializer, const char ** value) { bool deserialize(Deserializer* deserializer, const char ** value) {
return deserializeStr(deserializer, value); return deserializeStr(deserializer, value);
} }
bool serialize(Serializer* serializer, const wchar_t value[]) { void serialize(Serializer* serializer, const wchar_t value[]) {
return serializeStr(serializer, value); serializeStr(serializer, value);
} }
bool deserialize(Deserializer* deserializer, const wchar_t ** value) { bool deserialize(Deserializer* deserializer, const wchar_t ** value) {

View File

@ -32,9 +32,9 @@ namespace metacall {
// C string // C string
// //
bool serialize(Serializer* serializer, const char value[]); void serialize(Serializer* serializer, const char value[]);
bool deserialize(Deserializer* deserializer, const char ** value); bool deserialize(Deserializer* deserializer, const char ** value);
bool serialize(Serializer* serializer, const wchar_t value[]); void serialize(Serializer* serializer, const wchar_t value[]);
bool deserialize(Deserializer* deserializer, const wchar_t ** value); bool deserialize(Deserializer* deserializer, const wchar_t ** value);
@ -43,7 +43,7 @@ bool deserialize(Deserializer* deserializer, const wchar_t ** value);
// //
template <typename T, typename C, typename A> template <typename T, typename C, typename A>
bool serialize(Serializer* serializer, const std::basic_string<T, C, A>& value); void serialize(Serializer* serializer, const std::basic_string<T, C, A>& value);
template <typename T, typename C, typename A> template <typename T, typename C, typename A>
bool deserialize(Deserializer* deserializer, std::basic_string<T, C, A>* value); bool deserialize(Deserializer* deserializer, std::basic_string<T, C, A>* value);
@ -53,7 +53,7 @@ bool deserialize(Deserializer* deserializer, std::basic_string<T, C, A>* value);
// //
template <typename T, typename A> template <typename T, typename A>
bool serialize(Serializer* serializer, const std::vector<T, A>& value); void serialize(Serializer* serializer, const std::vector<T, A>& value);
template <typename T, typename A> template <typename T, typename A>
bool deserialize(Deserializer* deserializer, std::vector<T, A>* value); bool deserialize(Deserializer* deserializer, std::vector<T, A>* value);
@ -63,7 +63,7 @@ bool deserialize(Deserializer* deserializer, std::vector<T, A>* value);
// //
template <typename T, typename A> template <typename T, typename A>
bool serialize(Serializer* serializer, const std::list<T, A>& value); void serialize(Serializer* serializer, const std::list<T, A>& value);
template <typename T, typename A> template <typename T, typename A>
bool deserialize(Deserializer* deserializer, std::list<T, A>* value); bool deserialize(Deserializer* deserializer, std::list<T, A>* value);
@ -73,7 +73,7 @@ bool deserialize(Deserializer* deserializer, std::list<T, A>* value);
// //
template <typename T1, typename T2> template <typename T1, typename T2>
bool serialize(Serializer* serializer, const std::pair<T1, T2>& value); void serialize(Serializer* serializer, const std::pair<T1, T2>& value);
template <typename T1, typename T2> template <typename T1, typename T2>
bool deserialize(Deserializer* deserializer, std::pair<T1, T2>* value); bool deserialize(Deserializer* deserializer, std::pair<T1, T2>* value);
@ -83,7 +83,7 @@ bool deserialize(Deserializer* deserializer, std::pair<T1, T2>* value);
// //
template <typename K, typename V, typename C, typename A> template <typename K, typename V, typename C, typename A>
bool serialize(Serializer* serializer, const std::map<K, V, C, A>& value); void serialize(Serializer* serializer, const std::map<K, V, C, A>& value);
template <typename K, typename V, typename C, typename A> template <typename K, typename V, typename C, typename A>
bool deserialize(Deserializer* deserializer, std::map<K, V, C, A>* value); bool deserialize(Deserializer* deserializer, std::map<K, V, C, A>* value);

View File

@ -30,7 +30,7 @@ namespace metacall {
// TypeReader // TypeReader
// //
template <typename T, bool L, bool G> template <typename T, bool S>
struct TypeReader { struct TypeReader {
static bool read(Deserializer* deserializer, T* data) { static bool read(Deserializer* deserializer, T* data) {
return data->deserialize(deserializer); return data->deserialize(deserializer);
@ -38,14 +38,7 @@ struct TypeReader {
}; };
template <typename T> template <typename T>
struct TypeReader<T, false, true> { struct TypeReader<T, false> {
static bool read(Deserializer* deserializer, T* data) {
return deserialize(deserializer, data);
}
};
template <typename T>
struct TypeReader<T, false, false> {
static bool read(Deserializer* deserializer, T* data) { static bool read(Deserializer* deserializer, T* data) {
const T* const temp = reinterpret_cast<const T*>( const T* const temp = reinterpret_cast<const T*>(
deserializer->readRaw(sizeof(T)) deserializer->readRaw(sizeof(T))
@ -61,11 +54,7 @@ struct TypeReader<T, false, false> {
template <typename T> template <typename T>
bool Deserializer::read(T* data) { bool Deserializer::read(T* data) {
return TypeReader< return deserialize(this, data);
T,
HasLocalDeserializer<T>::Value,
HasGlobalDeserializer<T>::Value
>::read(this, data);
} }
@ -73,34 +62,38 @@ bool Deserializer::read(T* data) {
// TypeWriter // TypeWriter
// //
template <typename T, bool L, bool G> template <typename T, bool S>
struct TypeWriter { struct TypeWriter {
static bool write(Serializer* serializer, const T& data) { static void write(Serializer* serializer, const T& data) {
return data.serialize(serializer); data.serialize(serializer);
} }
}; };
template <typename T> template <typename T>
struct TypeWriter<T, false, true> { struct TypeWriter<T, false> {
static bool write(Serializer* serializer, const T& data) { static void write(Serializer* serializer, const T& data) {
return serialize(serializer, data); serializer->writeRaw(&data, sizeof(T));
} }
}; };
template <typename T> template <typename T>
struct TypeWriter<T, false, false> { void Serializer::write(const T& data) {
static bool write(Serializer* serializer, const T& data) { serialize(this, data);
return serializer->writeRaw(&data, sizeof(T));
} }
};
//
// Handlers
//
template <typename T> template <typename T>
bool Serializer::write(const T& data) { void serialize(Serializer* serializer, const T& data) {
return TypeWriter< TypeWriter<T, HasSerializer<T>::Value>::write(serializer, data);
T, }
HasLocalSerializer<T>::Value,
HasGlobalSerializer<T>::Value template <typename T>
>::write(this, data); bool deserialize(Deserializer* deserializer, T* data) {
return TypeReader<T, HasDeserializer<T>::Value>::read(deserializer, data);
} }

View File

@ -75,13 +75,11 @@ Serializer::Serializer(Buffer* data) :
{ {
} }
bool Serializer::writeRaw(const void* data, int size) { void Serializer::writeRaw(const void* data, int size) {
if (m_data != NULL) { if (m_data != NULL) {
m_data->addToBack(static_cast<const byte*>(data), size); m_data->addToBack(static_cast<const byte*>(data), size);
m_offset += size; m_offset += size;
} }
return true;
} }
bool Serializer::setOffset(int offset, bool relative) { bool Serializer::setOffset(int offset, bool relative) {

View File

@ -57,8 +57,8 @@ public:
Serializer(Buffer* data); Serializer(Buffer* data);
template <typename T> template <typename T>
bool write(const T& data); void write(const T& data);
bool writeRaw(const void* data, int size); void writeRaw(const void* data, int size);
bool setOffset(int offset, bool relative); bool setOffset(int offset, bool relative);
int offset() const; int offset() const;
@ -68,4 +68,15 @@ private:
}; };
//
// Handlers
//
template <typename T>
void serialize(Serializer* serializer, const T& data);
template <typename T>
bool deserialize(Deserializer* deserializer, T* data);
} }