Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1024)

Unified Diff: ipc/ipc_message_utils.h

Issue 2825853002: Improvements to uses of base::SmallMap (Closed)
Patch Set: Unit tests Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ipc/ipc_message_utils.h
diff --git a/ipc/ipc_message_utils.h b/ipc/ipc_message_utils.h
index 2d51c984aa0c0fdf005b355c989bfb6774b72bf0..406a1a519f2e42ce266b371e6eeb2479e2387764 100644
--- a/ipc/ipc_message_utils.h
+++ b/ipc/ipc_message_utils.h
@@ -17,6 +17,7 @@
#include <tuple>
#include <vector>
+#include "base/containers/flat_map.h"
#include "base/containers/small_map.h"
#include "base/containers/stack_container.h"
#include "base/files/file.h"
@@ -899,10 +900,10 @@ template <typename NormalMap,
int kArraySize,
typename EqualKey,
typename MapInit>
-struct ParamTraits<base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> > {
- typedef base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> param_type;
- typedef typename param_type::key_type K;
- typedef typename param_type::data_type V;
+struct ParamTraits<base::small_map<NormalMap, kArraySize, EqualKey, MapInit>> {
+ using param_type = base::small_map<NormalMap, kArraySize, EqualKey, MapInit>;
+ using K = typename param_type::key_type;
+ using V = typename param_type::data_type;
static void GetSize(base::PickleSizer* sizer, const param_type& p) {
GetParamSize(sizer, static_cast<int>(p.size()));
typename param_type::const_iterator iter;
@@ -936,7 +937,52 @@ struct ParamTraits<base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> > {
return true;
}
static void Log(const param_type& p, std::string* l) {
- l->append("<base::SmallMap>");
+ l->append("<base::small_map>");
+ }
+};
+
+template <class Key, class Mapped, class Compare>
+struct ParamTraits<base::flat_map<Key, Mapped, Compare>> {
+ using param_type = base::flat_map<Key, Mapped, Compare>;
+ static void GetSize(base::PickleSizer* sizer, const param_type& p) {
+ GetParamSize(sizer, static_cast<int>(p.size()));
danakj 2017/04/19 17:32:16 checked_cast or saturated_cast (leaning toward the
brettw 2017/04/19 17:55:04 Done. estark: This file is full of these casts, s
+ for (const auto& iter : p) {
+ GetParamSize(sizer, iter.first);
+ GetParamSize(sizer, iter.second);
+ }
+ }
+ static void Write(base::Pickle* m, const param_type& p) {
+ WriteParam(m, static_cast<int>(p.size()));
danakj 2017/04/19 17:32:16 same
+ typename param_type::const_iterator iter;
+ for (const auto& iter : p) {
+ WriteParam(m, iter.first);
+ WriteParam(m, iter.second);
+ }
+ }
+ static bool Read(const base::Pickle* m,
+ base::PickleIterator* iter,
+ param_type* r) {
+ int size;
+ if (!iter->ReadLength(&size))
+ return false;
+
+ // Construct by creating in a vector and moving into the flat_map. Properly
+ // serialized flat_maps will be in-order so this will be O(n). Incorrectly
+ // serialized ones will still be handled properly.
+ std::vector<typename param_type::value_type> vect;
+ vect.resize(size);
+ for (int i = 0; i < size; ++i) {
+ if (!ReadParam(m, iter, &vect[i].first))
+ return false;
+ if (!ReadParam(m, iter, &vect[i].second))
+ return false;
+ }
+
+ *r = param_type(std::move(vect), base::KEEP_FIRST_OF_DUPES);
+ return true;
+ }
+ static void Log(const param_type& p, std::string* l) {
+ l->append("<base::flat_map>");
}
};

Powered by Google App Engine
This is Rietveld 408576698