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();
}
bool Buffer::serialize(Serializer* serializer) const {
void Buffer::serialize(Serializer* serializer) const {
serializer->write(bytes());
serializer->writeRaw(data(), bytes());
return true;
}
bool Buffer::deserialize(Deserializer* deserializer) {

View File

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

View File

@ -32,37 +32,9 @@ namespace metacall {
// Meta
//
qword serialize(Serializer*, ...);
template <typename T>
struct HasGlobalSerializer {
enum {
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 HasSerializer {
template <typename U, void (U::*)(Serializer*) const>
struct Signature { };
template <typename U>
@ -77,7 +49,7 @@ struct HasLocalSerializer {
};
template <typename T>
struct HasLocalDeserializer {
struct HasDeserializer {
template <typename U, bool (U::*)(Deserializer*)>
struct Signature { };

View File

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

View File

@ -31,8 +31,8 @@ namespace metacall {
//
template <typename T, typename C, typename A>
bool serialize(Serializer* serializer, const std::basic_string<T, C, A>& value) {
return serialize(serializer, value.c_str());
void serialize(Serializer* serializer, const std::basic_string<T, C, A>& value) {
serialize(serializer, value.c_str());
}
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>
bool serialize(Serializer* serializer, const std::vector<T, A>& value) {
void serialize(Serializer* serializer, const std::vector<T, A>& value) {
serializer->write(value.size());
for (const typename std::vector<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
serializer->write(*iter);
}
return true;
}
template <typename T, typename A>
@ -93,13 +92,11 @@ bool deserialize(Deserializer* deserializer, std::vector<T, A>* value) {
//
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());
for (const typename std::list<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
serializer->write(*iter);
}
return true;
}
template <typename T, typename A>
@ -129,7 +126,7 @@ bool deserialize(Deserializer* deserializer, std::list<T, A>* value) {
//
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.second);
return true;
@ -146,13 +143,11 @@ bool deserialize(Deserializer* deserializer, std::pair<T1, T2>* value) {
//
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());
for (const typename std::map<K, V, C, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
serializer->write(*iter);
}
return true;
}
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>
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;
serializer->write(length);
serializer->writeRaw(value, length * sizeof(T));
return true;
}
template <typename T>
@ -70,16 +69,16 @@ static bool deserializeStr(Deserializer* deserializer, const T ** value) {
// C strings
//
bool serialize(Serializer* serializer, const char value[]) {
return serializeStr(serializer, value);
void serialize(Serializer* serializer, const char value[]) {
serializeStr(serializer, value);
}
bool deserialize(Deserializer* deserializer, const char ** value) {
return deserializeStr(deserializer, value);
}
bool serialize(Serializer* serializer, const wchar_t value[]) {
return serializeStr(serializer, value);
void serialize(Serializer* serializer, const wchar_t value[]) {
serializeStr(serializer, value);
}
bool deserialize(Deserializer* deserializer, const wchar_t ** value) {

View File

@ -32,9 +32,9 @@ namespace metacall {
// C string
//
bool serialize(Serializer* serializer, const char value[]);
void serialize(Serializer* serializer, 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);
@ -43,7 +43,7 @@ bool deserialize(Deserializer* deserializer, const wchar_t ** value);
//
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>
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>
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>
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>
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>
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>
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>
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>
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>
bool deserialize(Deserializer* deserializer, std::map<K, V, C, A>* value);

View File

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

View File

@ -57,8 +57,8 @@ public:
Serializer(Buffer* data);
template <typename T>
bool write(const T& data);
bool writeRaw(const void* data, int size);
void write(const T& data);
void writeRaw(const void* data, int size);
bool setOffset(int offset, bool relative);
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);
}