| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BLIMP_HELIUM_STREAM_HELPERS_H_ | |
| 6 #define BLIMP_HELIUM_STREAM_HELPERS_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/stl_util.h" | |
| 13 #include "blimp/helium/blimp_helium_export.h" | |
| 14 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 15 | |
| 16 namespace google { | |
| 17 namespace protobuf { | |
| 18 namespace io { | |
| 19 class CodedInputStream; | |
| 20 class CodedOutputStream; | |
| 21 } // namespace io | |
| 22 } // namespace protobuf | |
| 23 } // namespace google | |
| 24 | |
| 25 namespace blimp { | |
| 26 namespace helium { | |
| 27 | |
| 28 class VersionVector; | |
| 29 | |
| 30 void BLIMP_HELIUM_EXPORT | |
| 31 WriteToStream(const int32_t& value, | |
| 32 google::protobuf::io::CodedOutputStream* output_stream); | |
| 33 bool BLIMP_HELIUM_EXPORT | |
| 34 ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 35 int32_t* value); | |
| 36 | |
| 37 void BLIMP_HELIUM_EXPORT | |
| 38 WriteToStream(const std::string& value, | |
| 39 google::protobuf::io::CodedOutputStream* output_stream); | |
| 40 bool BLIMP_HELIUM_EXPORT | |
| 41 ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 42 std::string* value); | |
| 43 | |
| 44 void BLIMP_HELIUM_EXPORT | |
| 45 WriteToStream(const VersionVector& value, | |
| 46 google::protobuf::io::CodedOutputStream* output_stream); | |
| 47 bool BLIMP_HELIUM_EXPORT | |
| 48 ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 49 VersionVector* value); | |
| 50 | |
| 51 void BLIMP_HELIUM_EXPORT | |
| 52 WriteToStream(const uint64_t& value, | |
| 53 google::protobuf::io::CodedOutputStream* output_stream); | |
| 54 bool BLIMP_HELIUM_EXPORT | |
| 55 ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 56 uint64_t* value); | |
| 57 | |
| 58 template <typename KeyType, typename ValueType> | |
| 59 void WriteToStream(const std::map<KeyType, ValueType> value_map, | |
| 60 google::protobuf::io::CodedOutputStream* output_stream); | |
| 61 | |
| 62 template <typename KeyType, typename ValueType> | |
| 63 bool ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 64 std::map<KeyType, ValueType>* value_map); | |
| 65 | |
| 66 template <typename KeyType, typename ValueType> | |
| 67 void WriteToStream(const std::map<KeyType, ValueType> value_map, | |
| 68 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 69 output_stream->WriteVarint32(value_map.size()); | |
| 70 for (const auto& kv_pair : value_map) { | |
| 71 WriteToStream(kv_pair.first, output_stream); | |
| 72 WriteToStream(kv_pair.second, output_stream); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 template <typename KeyType, typename ValueType> | |
| 77 bool ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 78 std::map<KeyType, ValueType>* value_map) { | |
| 79 std::map<KeyType, ValueType> output; | |
| 80 | |
| 81 uint32_t num_entries; | |
| 82 if (!input_stream->ReadVarint32(&num_entries)) { | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 output.clear(); | |
| 87 for (uint32_t i = 0; i < num_entries; ++i) { | |
| 88 KeyType key; | |
| 89 ValueType value; | |
| 90 | |
| 91 if (!ReadFromStream(input_stream, &key) || | |
| 92 !ReadFromStream(input_stream, &value) || | |
| 93 base::ContainsKey(output, key)) { | |
| 94 return false; | |
| 95 } | |
| 96 | |
| 97 output.insert(std::make_pair(std::move(key), std::move(value))); | |
| 98 } | |
| 99 | |
| 100 *value_map = std::move(output); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 } // namespace helium | |
| 105 } // namespace blimp | |
| 106 | |
| 107 #endif // BLIMP_HELIUM_STREAM_HELPERS_H_ | |
| OLD | NEW |