Index: src/shared/Communication.h |
=================================================================== |
--- a/src/shared/Communication.h |
+++ b/src/shared/Communication.h |
@@ -9,30 +9,32 @@ |
#include <vector> |
#include <Windows.h> |
namespace Communication |
{ |
extern const std::wstring pipeName; |
const int bufferSize = 1024; |
- enum ValueType {TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL}; |
+ enum {TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL}; |
+ typedef int32_t ValueType; |
+ typedef uint32_t SizeType; |
class InputBuffer |
{ |
public: |
InputBuffer(const std::string& data) : buffer(data), hasType(false) {} |
InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_STRING); } |
InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE_WSTRING); } |
InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } |
InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } |
InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } |
private: |
std::istringstream buffer; |
- int32_t currentType; |
+ ValueType currentType; |
bool hasType; |
void CheckType(ValueType expectedType) |
{ |
if (!hasType) |
ReadBinary(currentType); |
if (currentType != expectedType) |
@@ -45,17 +47,17 @@ namespace Communication |
hasType = false; |
} |
template<class T> |
InputBuffer& ReadString(T& value, ValueType expectedType) |
{ |
CheckType(expectedType); |
- uint32_t length; |
+ SizeType length; |
ReadBinary(length); |
std::auto_ptr<T::value_type> data(new T::value_type[length]); |
buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * length); |
if (buffer.fail()) |
throw new std::runtime_error("Unexpected end of input buffer"); |
value.assign(data.get(), length); |
@@ -94,33 +96,36 @@ namespace Communication |
OutputBuffer& operator<<(const std::string& value) { return WriteString(value, TYPE_STRING); } |
OutputBuffer& operator<<(const std::wstring& value) { return WriteString(value, TYPE_WSTRING); } |
OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } |
OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } |
OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } |
private: |
std::ostringstream buffer; |
+ // Disallow copying |
+ const OutputBuffer& operator=(const OutputBuffer&); |
+ |
template<class T> |
- OutputBuffer& WriteString(const T& value, int32_t type) |
+ OutputBuffer& WriteString(const T& value, ValueType type) |
{ |
WriteBinary(type); |
- uint32_t length = value.size(); |
+ SizeType length = value.size(); |
WriteBinary(length); |
buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value_type) * length); |
if (buffer.fail()) |
throw new std::runtime_error("Unexpected error writing to output buffer"); |
return *this; |
} |
template<class T> |
- OutputBuffer& Write(const T value, int32_t type) |
+ OutputBuffer& Write(const T value, ValueType type) |
Felix Dahlke
2013/06/03 12:23:54
Shouldn't value be passed by reference?
Wladimir Palant
2013/06/03 12:25:55
Given that this function is meant for primitive ty
Felix Dahlke
2013/06/04 06:41:38
Right, didn't think of that. Won't hurt, but it wo
|
{ |
WriteBinary(type); |
WriteBinary(value); |
return *this; |
} |
template<class T> |
void WriteBinary(const T& value) |