folly: replace old-style header guards with "pragma once"
[folly.git] / folly / MapUtil.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <folly/Conv.h>
20 #include <folly/Optional.h>
21
22 namespace folly {
23
24 /**
25  * Given a map and a key, return the value corresponding to the key in the map,
26  * or a given default value if the key doesn't exist in the map.
27  */
28 template <class Map>
29 typename Map::mapped_type get_default(
30     const Map& map, const typename Map::key_type& key,
31     const typename Map::mapped_type& dflt =
32     typename Map::mapped_type()) {
33   auto pos = map.find(key);
34   return (pos != map.end() ? pos->second : dflt);
35 }
36
37 /**
38  * Given a map and a key, return the value corresponding to the key in the map,
39  * or throw an exception of the specified type.
40  */
41 template <class E = std::out_of_range, class Map>
42 typename Map::mapped_type get_or_throw(
43     const Map& map, const typename Map::key_type& key,
44     const std::string& exceptionStrPrefix = std::string()) {
45   auto pos = map.find(key);
46   if (pos != map.end()) {
47     return pos->second;
48   }
49   throw E(folly::to<std::string>(exceptionStrPrefix, key));
50 }
51
52 /**
53  * Given a map and a key, return a Optional<V> if the key exists and None if the
54  * key does not exist in the map.
55  */
56 template <class Map>
57 folly::Optional<typename Map::mapped_type> get_optional(
58     const Map& map, const typename Map::key_type& key) {
59   auto pos = map.find(key);
60   if (pos != map.end()) {
61     return folly::Optional<typename Map::mapped_type>(pos->second);
62   } else {
63     return folly::none;
64   }
65 }
66
67 /**
68  * Given a map and a key, return a reference to the value corresponding to the
69  * key in the map, or the given default reference if the key doesn't exist in
70  * the map.
71  */
72 template <class Map>
73 const typename Map::mapped_type& get_ref_default(
74     const Map& map, const typename Map::key_type& key,
75     const typename Map::mapped_type& dflt) {
76   auto pos = map.find(key);
77   return (pos != map.end() ? pos->second : dflt);
78 }
79
80 /**
81  * Given a map and a key, return a pointer to the value corresponding to the
82  * key in the map, or nullptr if the key doesn't exist in the map.
83  */
84 template <class Map>
85 const typename Map::mapped_type* get_ptr(
86     const Map& map, const typename Map::key_type& key) {
87   auto pos = map.find(key);
88   return (pos != map.end() ? &pos->second : nullptr);
89 }
90
91 /**
92  * Non-const overload of the above.
93  */
94 template <class Map>
95 typename Map::mapped_type* get_ptr(
96     Map& map, const typename Map::key_type& key) {
97   auto pos = map.find(key);
98   return (pos != map.end() ? &pos->second : nullptr);
99 }
100
101 }  // namespace folly