Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/shared/Communication.h

Issue 4882650246414336: Issue 2005 - Refactor working with strings in InputBuffer and OutputBuffer
Patch Set: Created Feb. 17, 2015, 12:36 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/shared/Communication.h
diff --git a/src/shared/Communication.h b/src/shared/Communication.h
index 4caa442dfbfeef7abcb042709cdf84d07366476e..eef73e4c47461bf056b6731568745adf66446b38 100644
--- a/src/shared/Communication.h
+++ b/src/shared/Communication.h
@@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include <Windows.h>
+#include "Utils.h"
namespace Communication
{
@@ -38,7 +39,7 @@ namespace Communication
PROC_COMPARE_VERSIONS
};
enum ValueType : uint32_t {
- TYPE_PROC, TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL, TYPE_STRINGS
+ TYPE_PROC, TYPE_STRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL, TYPE_STRINGS
};
typedef uint32_t SizeType;
@@ -54,12 +55,25 @@ namespace Communication
currentType = copy.currentType;
}
InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); }
- InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_STRING); }
- InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE_WSTRING); }
+ InputBuffer& operator>>(std::string& value) { return ReadString(value); }
+ InputBuffer& operator>>(std::wstring& value)
+ {
+ std::string tmpString;
+ operator>>(tmpString);
+ value = ToUtf16String(tmpString);
+ return *this;
+ }
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); }
InputBuffer& operator>>(std::vector<std::string>& value) { return ReadStrings(value); }
+ InputBuffer& operator>>(std::vector<std::wstring>& value)
+ {
+ std::vector<std::string> tmpStrings;
Eric 2015/02/17 18:15:13 Ugh. A temporary vector is just ugly and inefficie
+ operator>>(tmpStrings);
+ value = ToUtf16Strings(tmpStrings);
+ return *this;
+ }
InputBuffer& operator=(const InputBuffer& copy)
{
hasType = copy.hasType;
@@ -75,20 +89,16 @@ namespace Communication
void CheckType(ValueType expectedType);
- template<class T>
- InputBuffer& ReadString(T& value, ValueType expectedType)
+ InputBuffer& ReadString(std::string& value)
{
- CheckType(expectedType);
+ CheckType(TYPE_STRING);
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);
+ value.resize(length, '\0');
Eric 2015/02/17 18:15:13 We don't need a fill character, since we're immedi
sergei 2015/02/18 14:13:06 I've removed it but it's still used because void r
Eric 2015/02/18 18:12:47 That's fine. Nothing about external string initial
+ buffer.read(&value[0], sizeof(std::string::value_type) * length);
Eric 2015/02/17 18:15:13 We need two things here. -- An explicit const_cast
Eric 2015/02/17 18:15:13 Separately, we don't need the sizeof expression he
sergei 2015/02/18 14:13:06 done
sergei 2015/02/18 14:13:06 I have just thought about it again and I'm not sur
sergei 2015/02/18 14:41:39 In addition, Because of http://en.cppreference.com
Eric 2015/02/18 18:12:47 From my point of view, it is _already_ suspicious
Eric 2015/02/18 18:12:47 From that same page: "data()[i] == operator[](
sergei 2015/02/19 17:26:57 I've found it, http://codereview.adblockplus.org/5
sergei 2015/02/19 17:26:57 Don't mix const methods and non-const methods. On
if (buffer.fail())
throw new std::runtime_error("Unexpected end of input buffer");
Eric 2015/02/17 18:15:13 It wasn't your error, but we should be calling bad
sergei 2015/02/18 14:13:06 Right now I'm not sure about it and it's not relev
Eric 2015/02/18 18:12:47 A separate issue for this is overkill, but very br
sergei 2015/02/19 17:26:57 The reason to change it clear, but it's not a sing
-
- value.assign(data.get(), length);
return *this;
}
@@ -138,8 +148,11 @@ namespace Communication
return buffer.str();
}
OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); }
- OutputBuffer& operator<<(const std::string& value) { return WriteString(value, TYPE_STRING); }
- OutputBuffer& operator<<(const std::wstring& value) { return WriteString(value, TYPE_WSTRING); }
+ OutputBuffer& operator<<(const std::string& value) { return WriteString(value); }
+ OutputBuffer& operator<<(const std::wstring& value)
+ {
+ return operator<<(ToUtf8String(value));
+ }
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); }
@@ -150,15 +163,13 @@ namespace Communication
// Disallow copying
const OutputBuffer& operator=(const OutputBuffer&);
- template<class T>
- OutputBuffer& WriteString(const T& value, ValueType type)
+ OutputBuffer& WriteString(const std::string& value)
{
- WriteBinary(type);
+ WriteBinary(TYPE_STRING);
SizeType length = static_cast<SizeType>(value.size());
WriteBinary(length);
-
- buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value_type) * length);
+ buffer.write(value.c_str(), sizeof(std::string::value_type) * length);
Eric 2015/02/17 18:15:13 See sizeof comment above.
sergei 2015/02/18 14:13:06 done.
if (buffer.fail())
throw new std::runtime_error("Unexpected error writing to output buffer");

Powered by Google App Engine
This is Rietveld