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

Side by Side Diff: src/shared/Communication.h

Issue 4806567450902528: Issue 1794 - add handling of std::vector<std::string> by Communication::{Input,Output}Buffer (Closed)
Patch Set: remove WriteStrings and ReadStrings Created Feb. 9, 2015, 2:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #ifndef COMMUNICATION_H 1 #ifndef COMMUNICATION_H
2 #define COMMUNICATION_H 2 #define COMMUNICATION_H
3 3
4 #include <memory> 4 #include <memory>
5 #include <sstream> 5 #include <sstream>
6 #include <stdexcept> 6 #include <stdexcept>
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 #include <Windows.h> 10 #include <Windows.h>
(...skipping 20 matching lines...) Expand all
31 PROC_SET_PREF, 31 PROC_SET_PREF,
32 PROC_GET_PREF, 32 PROC_GET_PREF,
33 PROC_IS_FIRST_RUN_ACTION_NEEDED, 33 PROC_IS_FIRST_RUN_ACTION_NEEDED,
34 PROC_CHECK_FOR_UPDATES, 34 PROC_CHECK_FOR_UPDATES,
35 PROC_GET_DOCUMENTATION_LINK, 35 PROC_GET_DOCUMENTATION_LINK,
36 PROC_TOGGLE_PLUGIN_ENABLED, 36 PROC_TOGGLE_PLUGIN_ENABLED,
37 PROC_GET_HOST, 37 PROC_GET_HOST,
38 PROC_COMPARE_VERSIONS 38 PROC_COMPARE_VERSIONS
39 }; 39 };
40 enum ValueType : uint32_t { 40 enum ValueType : uint32_t {
41 TYPE_PROC, TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL 41 TYPE_PROC, TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL, TYP E_STRINGS
42 }; 42 };
43 typedef uint32_t SizeType; 43 typedef uint32_t SizeType;
44 44
45 class InputBuffer 45 class InputBuffer
46 { 46 {
47 public: 47 public:
48 InputBuffer() : buffer(), hasType(false) {} 48 InputBuffer() : buffer(), hasType(false) {}
49 InputBuffer(const std::string& data) : buffer(data), hasType(false) {} 49 InputBuffer(const std::string& data) : buffer(data), hasType(false) {}
50 InputBuffer(const InputBuffer& copy) 50 InputBuffer(const InputBuffer& copy)
51 { 51 {
52 hasType = copy.hasType; 52 hasType = copy.hasType;
53 buffer = std::istringstream(copy.buffer.str()); 53 buffer = std::istringstream(copy.buffer.str());
54 currentType = copy.currentType; 54 currentType = copy.currentType;
55 } 55 }
56 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); } 56 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); }
57 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } 57 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); }
58 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } 58 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); }
59 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } 59 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); }
60 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } 60 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); }
61 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } 61 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); }
62 InputBuffer& operator>>(std::vector<std::string>& value) { return ReadString s(value); }
62 InputBuffer& operator=(const InputBuffer& copy) 63 InputBuffer& operator=(const InputBuffer& copy)
63 { 64 {
64 hasType = copy.hasType; 65 hasType = copy.hasType;
65 buffer = std::istringstream(copy.buffer.str()); 66 buffer = std::istringstream(copy.buffer.str());
66 currentType = copy.currentType; 67 currentType = copy.currentType;
67 return *this; 68 return *this;
68 } 69 }
69 ValueType GetType(); 70 ValueType GetType();
70 private: 71 private:
71 std::istringstream buffer; 72 std::istringstream buffer;
72 ValueType currentType; 73 ValueType currentType;
73 bool hasType; 74 bool hasType;
74 75
75 void CheckType(ValueType expectedType); 76 void CheckType(ValueType expectedType);
76 77
77 template<class T> 78 template<class T>
78 InputBuffer& ReadString(T& value, ValueType expectedType) 79 InputBuffer& ReadString(T& value, ValueType expectedType)
79 { 80 {
80 CheckType(expectedType); 81 CheckType(expectedType);
81 82
82 SizeType length; 83 SizeType length;
83 ReadBinary(length); 84 ReadBinary(length);
84 85
85 std::auto_ptr<T::value_type> data(new T::value_type[length]); 86 std::auto_ptr<T::value_type> data(new T::value_type[length]);
86 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l ength); 87 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l ength);
Eric 2015/02/09 16:18:57 This should be 'const_cast'. It should also be do
sergei 2015/02/09 16:55:37 Here we are not reading the data into the constant
Eric 2015/02/09 17:34:02 Sorry. Too hasty about 'string'. Nevertheless, us
87 if (buffer.fail()) 88 if (buffer.fail())
88 throw new std::runtime_error("Unexpected end of input buffer"); 89 throw new std::runtime_error("Unexpected end of input buffer");
89 90
90 value.assign(data.get(), length); 91 value.assign(data.get(), length);
Eric 2015/02/09 16:18:57 This is the place to use either plain assignment o
91 return *this; 92 return *this;
92 } 93 }
93 94
95 InputBuffer& ReadStrings(std::vector<std::string>& value)
96 {
97 value.clear();
98 CheckType(ValueType::TYPE_STRINGS);
99
100 SizeType length;
101 ReadBinary(length);
102 value.resize(length);
103 for (SizeType i = 0; i < length; ++i)
104 {
105 operator>>(value[i]);
106 }
107 return *this;
108 }
109
94 template<class T> 110 template<class T>
95 InputBuffer& Read(T& value, ValueType expectedType) 111 InputBuffer& Read(T& value, ValueType expectedType)
96 { 112 {
97 CheckType(expectedType); 113 CheckType(expectedType);
98 ReadBinary(value); 114 ReadBinary(value);
99 return *this; 115 return *this;
100 } 116 }
101 117
102 template<class T> 118 template<class T>
103 void ReadBinary(T& value) 119 void ReadBinary(T& value)
(...skipping 16 matching lines...) Expand all
120 std::string Get() 136 std::string Get()
121 { 137 {
122 return buffer.str(); 138 return buffer.str();
123 } 139 }
124 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); } 140 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); }
125 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e, TYPE_STRING); } 141 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e, TYPE_STRING); }
126 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val ue, TYPE_WSTRING); } 142 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val ue, TYPE_WSTRING); }
127 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } 143 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); }
128 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } 144 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); }
129 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } 145 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); }
146 OutputBuffer& operator<<(const std::vector<std::string>& value) { return Wri teStrings(value); }
130 private: 147 private:
131 std::ostringstream buffer; 148 std::ostringstream buffer;
132 149
133 // Disallow copying 150 // Disallow copying
134 const OutputBuffer& operator=(const OutputBuffer&); 151 const OutputBuffer& operator=(const OutputBuffer&);
135 152
136 template<class T> 153 template<class T>
137 OutputBuffer& WriteString(const T& value, ValueType type) 154 OutputBuffer& WriteString(const T& value, ValueType type)
138 { 155 {
139 WriteBinary(type); 156 WriteBinary(type);
140 157
141 SizeType length = static_cast<SizeType>(value.size()); 158 SizeType length = static_cast<SizeType>(value.size());
142 WriteBinary(length); 159 WriteBinary(length);
143 160
144 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length); 161 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length);
Eric 2015/02/09 16:18:57 Please take the opportunity to change this to 'con
sergei 2015/02/09 16:55:37 We should not change this to `const_cast`, here, a
145 if (buffer.fail()) 162 if (buffer.fail())
146 throw new std::runtime_error("Unexpected error writing to output buffer" ); 163 throw new std::runtime_error("Unexpected error writing to output buffer" );
147 164
148 return *this; 165 return *this;
149 } 166 }
150 167
168 OutputBuffer& WriteStrings(const std::vector<std::string>& value)
169 {
170 WriteBinary(ValueType::TYPE_STRINGS);
171 WriteBinary(static_cast<SizeType>(value.size()));
172 for (const auto& str : value)
173 {
174 operator<<(str);
175 }
176 return *this;
177 }
178
151 template<class T> 179 template<class T>
152 OutputBuffer& Write(const T value, ValueType type) 180 OutputBuffer& Write(const T value, ValueType type)
153 { 181 {
154 WriteBinary(type); 182 WriteBinary(type);
155 WriteBinary(value); 183 WriteBinary(value);
156 return *this; 184 return *this;
157 } 185 }
158 186
159 template<class T> 187 template<class T>
160 void WriteBinary(const T& value) 188 void WriteBinary(const T& value)
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 221
194 InputBuffer ReadMessage(); 222 InputBuffer ReadMessage();
195 void WriteMessage(OutputBuffer& message); 223 void WriteMessage(OutputBuffer& message);
196 224
197 protected: 225 protected:
198 HANDLE pipe; 226 HANDLE pipe;
199 }; 227 };
200 } 228 }
201 229
202 #endif 230 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld