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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef IPC_IPC_MESSAGE_UTILS_H_ 5 #ifndef IPC_IPC_MESSAGE_UTILS_H_
6 #define IPC_IPC_MESSAGE_UTILS_H_ 6 #define IPC_IPC_MESSAGE_UTILS_H_
7 7
8 #include <limits.h> 8 #include <limits.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <stdint.h> 10 #include <stdint.h>
11 11
12 #include <algorithm> 12 #include <algorithm>
13 #include <map> 13 #include <map>
14 #include <memory> 14 #include <memory>
15 #include <set> 15 #include <set>
16 #include <string> 16 #include <string>
17 #include <tuple> 17 #include <tuple>
18 #include <vector> 18 #include <vector>
19 19
20 #include "base/containers/flat_map.h"
20 #include "base/containers/small_map.h" 21 #include "base/containers/small_map.h"
21 #include "base/containers/stack_container.h" 22 #include "base/containers/stack_container.h"
22 #include "base/files/file.h" 23 #include "base/files/file.h"
23 #include "base/format_macros.h" 24 #include "base/format_macros.h"
24 #include "base/optional.h" 25 #include "base/optional.h"
25 #include "base/strings/string16.h" 26 #include "base/strings/string16.h"
26 #include "base/strings/string_util.h" 27 #include "base/strings/string_util.h"
27 #include "base/strings/stringprintf.h" 28 #include "base/strings/stringprintf.h"
28 #include "build/build_config.h" 29 #include "build/build_config.h"
29 #include "ipc/ipc_message_start.h" 30 #include "ipc/ipc_message_start.h"
(...skipping 862 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 l->append(" "); 893 l->append(" ");
893 LogParam((p[i]), l); 894 LogParam((p[i]), l);
894 } 895 }
895 } 896 }
896 }; 897 };
897 898
898 template <typename NormalMap, 899 template <typename NormalMap,
899 int kArraySize, 900 int kArraySize,
900 typename EqualKey, 901 typename EqualKey,
901 typename MapInit> 902 typename MapInit>
902 struct ParamTraits<base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> > { 903 struct ParamTraits<base::small_map<NormalMap, kArraySize, EqualKey, MapInit>> {
903 typedef base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> param_type; 904 using param_type = base::small_map<NormalMap, kArraySize, EqualKey, MapInit>;
904 typedef typename param_type::key_type K; 905 using K = typename param_type::key_type;
905 typedef typename param_type::data_type V; 906 using V = typename param_type::data_type;
906 static void GetSize(base::PickleSizer* sizer, const param_type& p) { 907 static void GetSize(base::PickleSizer* sizer, const param_type& p) {
907 GetParamSize(sizer, static_cast<int>(p.size())); 908 GetParamSize(sizer, static_cast<int>(p.size()));
908 typename param_type::const_iterator iter; 909 typename param_type::const_iterator iter;
909 for (iter = p.begin(); iter != p.end(); ++iter) { 910 for (iter = p.begin(); iter != p.end(); ++iter) {
910 GetParamSize(sizer, iter->first); 911 GetParamSize(sizer, iter->first);
911 GetParamSize(sizer, iter->second); 912 GetParamSize(sizer, iter->second);
912 } 913 }
913 } 914 }
914 static void Write(base::Pickle* m, const param_type& p) { 915 static void Write(base::Pickle* m, const param_type& p) {
915 WriteParam(m, static_cast<int>(p.size())); 916 WriteParam(m, static_cast<int>(p.size()));
(...skipping 13 matching lines...) Expand all
929 K key; 930 K key;
930 if (!ReadParam(m, iter, &key)) 931 if (!ReadParam(m, iter, &key))
931 return false; 932 return false;
932 V& value = (*r)[key]; 933 V& value = (*r)[key];
933 if (!ReadParam(m, iter, &value)) 934 if (!ReadParam(m, iter, &value))
934 return false; 935 return false;
935 } 936 }
936 return true; 937 return true;
937 } 938 }
938 static void Log(const param_type& p, std::string* l) { 939 static void Log(const param_type& p, std::string* l) {
939 l->append("<base::SmallMap>"); 940 l->append("<base::small_map>");
940 } 941 }
941 }; 942 };
942 943
944 template <class Key, class Mapped, class Compare>
945 struct ParamTraits<base::flat_map<Key, Mapped, Compare>> {
946 using param_type = base::flat_map<Key, Mapped, Compare>;
947 static void GetSize(base::PickleSizer* sizer, const param_type& p) {
948 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
949 for (const auto& iter : p) {
950 GetParamSize(sizer, iter.first);
951 GetParamSize(sizer, iter.second);
952 }
953 }
954 static void Write(base::Pickle* m, const param_type& p) {
955 WriteParam(m, static_cast<int>(p.size()));
danakj 2017/04/19 17:32:16 same
956 typename param_type::const_iterator iter;
957 for (const auto& iter : p) {
958 WriteParam(m, iter.first);
959 WriteParam(m, iter.second);
960 }
961 }
962 static bool Read(const base::Pickle* m,
963 base::PickleIterator* iter,
964 param_type* r) {
965 int size;
966 if (!iter->ReadLength(&size))
967 return false;
968
969 // Construct by creating in a vector and moving into the flat_map. Properly
970 // serialized flat_maps will be in-order so this will be O(n). Incorrectly
971 // serialized ones will still be handled properly.
972 std::vector<typename param_type::value_type> vect;
973 vect.resize(size);
974 for (int i = 0; i < size; ++i) {
975 if (!ReadParam(m, iter, &vect[i].first))
976 return false;
977 if (!ReadParam(m, iter, &vect[i].second))
978 return false;
979 }
980
981 *r = param_type(std::move(vect), base::KEEP_FIRST_OF_DUPES);
982 return true;
983 }
984 static void Log(const param_type& p, std::string* l) {
985 l->append("<base::flat_map>");
986 }
987 };
988
943 template <class P> 989 template <class P>
944 struct ParamTraits<std::unique_ptr<P>> { 990 struct ParamTraits<std::unique_ptr<P>> {
945 typedef std::unique_ptr<P> param_type; 991 typedef std::unique_ptr<P> param_type;
946 static void GetSize(base::PickleSizer* sizer, const param_type& p) { 992 static void GetSize(base::PickleSizer* sizer, const param_type& p) {
947 bool valid = !!p; 993 bool valid = !!p;
948 GetParamSize(sizer, valid); 994 GetParamSize(sizer, valid);
949 if (valid) 995 if (valid)
950 GetParamSize(sizer, *p); 996 GetParamSize(sizer, *p);
951 } 997 }
952 static void Write(base::Pickle* m, const param_type& p) { 998 static void Write(base::Pickle* m, const param_type& p) {
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
1136 template <class ReplyParamType> 1182 template <class ReplyParamType>
1137 inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, 1183 inline void LogReplyParamsToMessage(const ReplyParamType& reply_params,
1138 const Message* msg) {} 1184 const Message* msg) {}
1139 1185
1140 inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} 1186 inline void ConnectMessageAndReply(const Message* msg, Message* reply) {}
1141 #endif 1187 #endif
1142 1188
1143 } // namespace IPC 1189 } // namespace IPC
1144 1190
1145 #endif // IPC_IPC_MESSAGE_UTILS_H_ 1191 #endif // IPC_IPC_MESSAGE_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698