fixed Alex' bug!!

Added string specialization to template functions for syncbuffer class
This commit is contained in:
Joakim Kilby
2015-02-17 17:51:53 +01:00
parent 9f16d6f2ac
commit eb7fefb3c9
4 changed files with 38 additions and 6 deletions

View File

@@ -44,6 +44,18 @@ public:
_encodeOffset += size;
}
template<>
void encode(const std::string& s) {
const size_t size = sizeof(char) * s.size() + sizeof(int32_t);
assert(_encodeOffset + size < _n);
int32_t length = s.length();
memcpy(_dataStream.data() + _encodeOffset, reinterpret_cast<const char*>(&length), sizeof(int32_t));
_encodeOffset += sizeof(int32_t);
memcpy(_dataStream.data() + _encodeOffset, s.c_str(), length);
_encodeOffset += length;
}
template<typename T>
T decode() {
const size_t size = sizeof(T);
@@ -54,6 +66,19 @@ public:
return value;
}
template<>
std::string decode() {
int32_t length;
memcpy(reinterpret_cast<char*>(&length), _dataStream.data() + _decodeOffset, sizeof(int32_t));
char* tmp = new char[length + 1];
_decodeOffset += sizeof(int32_t);
memcpy(tmp, _dataStream.data() + _decodeOffset, length);
_decodeOffset += length;
tmp[length] = '\0';
std::string ret(tmp);
delete[] tmp;
return ret;
}
template<typename T>
void decode(T& value) {
@@ -63,6 +88,11 @@ public:
_decodeOffset += size;
}
template<>
void decode(std::string &s) {
s = decode<std::string>();
}
void write();
void read();