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

Side by Side Diff: ipc/ipc_message_utils.h

Issue 2825853002: Improvements to uses of base::SmallMap (Closed)
Patch Set: Fixes 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"
25 #include "base/numerics/safe_conversions.h"
24 #include "base/optional.h" 26 #include "base/optional.h"
25 #include "base/strings/string16.h" 27 #include "base/strings/string16.h"
26 #include "base/strings/string_util.h" 28 #include "base/strings/string_util.h"
27 #include "base/strings/stringprintf.h" 29 #include "base/strings/stringprintf.h"
28 #include "build/build_config.h" 30 #include "build/build_config.h"
29 #include "ipc/ipc_message_start.h" 31 #include "ipc/ipc_message_start.h"
30 #include "ipc/ipc_param_traits.h" 32 #include "ipc/ipc_param_traits.h"
31 #include "ipc/ipc_sync_message.h" 33 #include "ipc/ipc_sync_message.h"
32 34
33 namespace base { 35 namespace base {
(...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 l->append(" "); 894 l->append(" ");
893 LogParam((p[i]), l); 895 LogParam((p[i]), l);
894 } 896 }
895 } 897 }
896 }; 898 };
897 899
898 template <typename NormalMap, 900 template <typename NormalMap,
899 int kArraySize, 901 int kArraySize,
900 typename EqualKey, 902 typename EqualKey,
901 typename MapInit> 903 typename MapInit>
902 struct ParamTraits<base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> > { 904 struct ParamTraits<base::small_map<NormalMap, kArraySize, EqualKey, MapInit>> {
903 typedef base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> param_type; 905 using param_type = base::small_map<NormalMap, kArraySize, EqualKey, MapInit>;
904 typedef typename param_type::key_type K; 906 using K = typename param_type::key_type;
905 typedef typename param_type::data_type V; 907 using V = typename param_type::data_type;
906 static void GetSize(base::PickleSizer* sizer, const param_type& p) { 908 static void GetSize(base::PickleSizer* sizer, const param_type& p) {
907 GetParamSize(sizer, static_cast<int>(p.size())); 909 GetParamSize(sizer, static_cast<int>(p.size()));
908 typename param_type::const_iterator iter; 910 typename param_type::const_iterator iter;
909 for (iter = p.begin(); iter != p.end(); ++iter) { 911 for (iter = p.begin(); iter != p.end(); ++iter) {
910 GetParamSize(sizer, iter->first); 912 GetParamSize(sizer, iter->first);
911 GetParamSize(sizer, iter->second); 913 GetParamSize(sizer, iter->second);
912 } 914 }
913 } 915 }
914 static void Write(base::Pickle* m, const param_type& p) { 916 static void Write(base::Pickle* m, const param_type& p) {
915 WriteParam(m, static_cast<int>(p.size())); 917 WriteParam(m, static_cast<int>(p.size()));
(...skipping 13 matching lines...) Expand all
929 K key; 931 K key;
930 if (!ReadParam(m, iter, &key)) 932 if (!ReadParam(m, iter, &key))
931 return false; 933 return false;
932 V& value = (*r)[key]; 934 V& value = (*r)[key];
933 if (!ReadParam(m, iter, &value)) 935 if (!ReadParam(m, iter, &value))
934 return false; 936 return false;
935 } 937 }
936 return true; 938 return true;
937 } 939 }
938 static void Log(const param_type& p, std::string* l) { 940 static void Log(const param_type& p, std::string* l) {
939 l->append("<base::SmallMap>"); 941 l->append("<base::small_map>");
940 } 942 }
941 }; 943 };
942 944
945 template <class Key, class Mapped, class Compare>
946 struct ParamTraits<base::flat_map<Key, Mapped, Compare>> {
947 using param_type = base::flat_map<Key, Mapped, Compare>;
948 static void GetSize(base::PickleSizer* sizer, const param_type& p) {
949 GetParamSize(sizer, base::checked_cast<int>(p.size()));
950 for (const auto& iter : p) {
951 GetParamSize(sizer, iter.first);
952 GetParamSize(sizer, iter.second);
953 }
954 }
955 static void Write(base::Pickle* m, const param_type& p) {
956 WriteParam(m, base::checked_cast<int>(p.size()));
957 typename param_type::const_iterator iter;
estark 2017/04/20 00:59:49 unneeded?
958 for (const auto& iter : p) {
959 WriteParam(m, iter.first);
960 WriteParam(m, iter.second);
961 }
962 }
963 static bool Read(const base::Pickle* m,
964 base::PickleIterator* iter,
965 param_type* r) {
966 int size;
967 if (!iter->ReadLength(&size))
968 return false;
969
970 // Construct by creating in a vector and moving into the flat_map. Properly
971 // serialized flat_maps will be in-order so this will be O(n). Incorrectly
972 // serialized ones will still be handled properly.
973 std::vector<typename param_type::value_type> vect;
974 vect.resize(size);
dcheng 2017/04/20 01:30:47 Hmm... too bad there's no great way to do this wit
brettw 2017/04/20 04:24:24 I could certainly write reserve() and I did the fi
975 for (int i = 0; i < size; ++i) {
976 if (!ReadParam(m, iter, &vect[i].first))
977 return false;
978 if (!ReadParam(m, iter, &vect[i].second))
979 return false;
980 }
981
982 *r = param_type(std::move(vect), base::KEEP_FIRST_OF_DUPES);
983 return true;
984 }
985 static void Log(const param_type& p, std::string* l) {
986 l->append("<base::flat_map>");
987 }
988 };
989
943 template <class P> 990 template <class P>
944 struct ParamTraits<std::unique_ptr<P>> { 991 struct ParamTraits<std::unique_ptr<P>> {
945 typedef std::unique_ptr<P> param_type; 992 typedef std::unique_ptr<P> param_type;
946 static void GetSize(base::PickleSizer* sizer, const param_type& p) { 993 static void GetSize(base::PickleSizer* sizer, const param_type& p) {
947 bool valid = !!p; 994 bool valid = !!p;
948 GetParamSize(sizer, valid); 995 GetParamSize(sizer, valid);
949 if (valid) 996 if (valid)
950 GetParamSize(sizer, *p); 997 GetParamSize(sizer, *p);
951 } 998 }
952 static void Write(base::Pickle* m, const param_type& p) { 999 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> 1183 template <class ReplyParamType>
1137 inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, 1184 inline void LogReplyParamsToMessage(const ReplyParamType& reply_params,
1138 const Message* msg) {} 1185 const Message* msg) {}
1139 1186
1140 inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} 1187 inline void ConnectMessageAndReply(const Message* msg, Message* reply) {}
1141 #endif 1188 #endif
1142 1189
1143 } // namespace IPC 1190 } // namespace IPC
1144 1191
1145 #endif // IPC_IPC_MESSAGE_UTILS_H_ 1192 #endif // IPC_IPC_MESSAGE_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698