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

Side by Side Diff: util/stdlib/map_insert.h

Issue 1044273002: Improve map insertions (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 5 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
(Empty)
1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef CRASHPAD_UTIL_STDLIB_MAP_INSERT_H_
16 #define CRASHPAD_UTIL_STDLIB_MAP_INSERT_H_
17
18 #include <map>
19 #include <utility>
20
21 namespace crashpad {
22
23 //! \brief Inserts a mapping from \a key to \a value into \a map, or replaces
24 //! an existing mapping so that \a key maps to \value.
25 //!
26 //! This behaves similarly to `std::map<>::insert_or_assign()` proposed for
27 //! C++17, except that the \a old_value parameter is added.
28 //!
29 //! \param[inout] map The map to operate on.
30 //! \param[in] key The key that should be mapped to \a value.
31 //! \param[in] value The value that \a key should map to.
32 //! \param[out] old_value If \a key was previously present in \a map, this will
33 //! be set to its previous value. This parameter is optional and may be
34 //! `nullptr` if this information is not required.
35 //!
36 //! \return `false` if \a key was previously present in \a map. If \a old_value
37 //! is not `nullptr`, it will be set to the previous value. `true` if \a
38 //! key was not present in the map and was inserted.
39 template <typename T>
40 bool MapInsertOrReplace(T* map,
41 const typename T::key_type& key,
42 const typename T::mapped_type& value,
43 typename T::mapped_type* old_value) {
44 const auto result = map->insert(std::make_pair(key, value));
45 if (!result.second) {
46 if (old_value) {
47 *old_value = result.first->second;
48 }
49 result.first->second = value;
50 }
51 return result.second;
52 }
53
54 } // namespace crashpad
55
56 #endif // CRASHPAD_UTIL_STDLIB_MAP_INSERT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698