MapUtil.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2012-present 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. #pragma once
  17. #include <folly/Conv.h>
  18. #include <folly/Optional.h>
  19. #include <folly/functional/Invoke.h>
  20. #include <tuple>
  21. namespace folly {
  22. /**
  23. * Given a map and a key, return the value corresponding to the key in the map,
  24. * or a given default value if the key doesn't exist in the map.
  25. */
  26. template <typename Map, typename Key>
  27. typename Map::mapped_type get_default(const Map& map, const Key& key) {
  28. auto pos = map.find(key);
  29. return (pos != map.end()) ? (pos->second) : (typename Map::mapped_type{});
  30. }
  31. template <
  32. class Map,
  33. typename Key = typename Map::key_type,
  34. typename Value = typename Map::mapped_type,
  35. typename std::enable_if<!is_invocable<Value>::value>::type* = nullptr>
  36. typename Map::mapped_type
  37. get_default(const Map& map, const Key& key, Value&& dflt) {
  38. using M = typename Map::mapped_type;
  39. auto pos = map.find(key);
  40. return (pos != map.end()) ? (pos->second) : M(std::forward<Value>(dflt));
  41. }
  42. /**
  43. * Give a map and a key, return the value corresponding to the key in the map,
  44. * or a given default value if the key doesn't exist in the map.
  45. */
  46. template <
  47. class Map,
  48. typename Key = typename Map::key_type,
  49. typename Func,
  50. typename = typename std::enable_if<
  51. is_invocable_r<typename Map::mapped_type, Func>::value>::type>
  52. typename Map::mapped_type
  53. get_default(const Map& map, const Key& key, Func&& dflt) {
  54. auto pos = map.find(key);
  55. return pos != map.end() ? pos->second : dflt();
  56. }
  57. /**
  58. * Given a map and a key, return the value corresponding to the key in the map,
  59. * or throw an exception of the specified type.
  60. */
  61. template <
  62. class E = std::out_of_range,
  63. class Map,
  64. typename Key = typename Map::key_type>
  65. const typename Map::mapped_type& get_or_throw(
  66. const Map& map,
  67. const Key& key,
  68. const std::string& exceptionStrPrefix = std::string()) {
  69. auto pos = map.find(key);
  70. if (pos != map.end()) {
  71. return pos->second;
  72. }
  73. throw E(folly::to<std::string>(exceptionStrPrefix, key));
  74. }
  75. template <
  76. class E = std::out_of_range,
  77. class Map,
  78. typename Key = typename Map::key_type>
  79. typename Map::mapped_type& get_or_throw(
  80. Map& map,
  81. const Key& key,
  82. const std::string& exceptionStrPrefix = std::string()) {
  83. auto pos = map.find(key);
  84. if (pos != map.end()) {
  85. return pos->second;
  86. }
  87. throw E(folly::to<std::string>(exceptionStrPrefix, key));
  88. }
  89. /**
  90. * Given a map and a key, return a Optional<V> if the key exists and None if the
  91. * key does not exist in the map.
  92. */
  93. template <class Map, typename Key = typename Map::key_type>
  94. folly::Optional<typename Map::mapped_type> get_optional(
  95. const Map& map,
  96. const Key& key) {
  97. auto pos = map.find(key);
  98. if (pos != map.end()) {
  99. return folly::Optional<typename Map::mapped_type>(pos->second);
  100. } else {
  101. return folly::none;
  102. }
  103. }
  104. /**
  105. * Given a map and a key, return a reference to the value corresponding to the
  106. * key in the map, or the given default reference if the key doesn't exist in
  107. * the map.
  108. */
  109. template <class Map, typename Key = typename Map::key_type>
  110. const typename Map::mapped_type& get_ref_default(
  111. const Map& map,
  112. const Key& key,
  113. const typename Map::mapped_type& dflt) {
  114. auto pos = map.find(key);
  115. return (pos != map.end() ? pos->second : dflt);
  116. }
  117. /**
  118. * Passing a temporary default value returns a dangling reference when it is
  119. * returned. Lifetime extension is broken by the indirection.
  120. * The caller must ensure that the default value outlives the reference returned
  121. * by get_ref_default().
  122. */
  123. template <class Map, typename Key = typename Map::key_type>
  124. const typename Map::mapped_type& get_ref_default(
  125. const Map& map,
  126. const Key& key,
  127. typename Map::mapped_type&& dflt) = delete;
  128. template <class Map, typename Key = typename Map::key_type>
  129. const typename Map::mapped_type& get_ref_default(
  130. const Map& map,
  131. const Key& key,
  132. const typename Map::mapped_type&& dflt) = delete;
  133. /**
  134. * Given a map and a key, return a reference to the value corresponding to the
  135. * key in the map, or the given default reference if the key doesn't exist in
  136. * the map.
  137. */
  138. template <
  139. class Map,
  140. typename Key = typename Map::key_type,
  141. typename Func,
  142. typename = typename std::enable_if<
  143. is_invocable_r<const typename Map::mapped_type&, Func>::value>::type,
  144. typename = typename std::enable_if<
  145. std::is_reference<invoke_result_t<Func>>::value>::type>
  146. const typename Map::mapped_type&
  147. get_ref_default(const Map& map, const Key& key, Func&& dflt) {
  148. auto pos = map.find(key);
  149. return (pos != map.end() ? pos->second : dflt());
  150. }
  151. /**
  152. * Given a map and a key, return a pointer to the value corresponding to the
  153. * key in the map, or nullptr if the key doesn't exist in the map.
  154. */
  155. template <class Map, typename Key = typename Map::key_type>
  156. const typename Map::mapped_type* get_ptr(const Map& map, const Key& key) {
  157. auto pos = map.find(key);
  158. return (pos != map.end() ? &pos->second : nullptr);
  159. }
  160. /**
  161. * Non-const overload of the above.
  162. */
  163. template <class Map, typename Key = typename Map::key_type>
  164. typename Map::mapped_type* get_ptr(Map& map, const Key& key) {
  165. auto pos = map.find(key);
  166. return (pos != map.end() ? &pos->second : nullptr);
  167. }
  168. // TODO: Remove the return type computations when clang 3.5 and gcc 5.1 are
  169. // the minimum supported versions.
  170. namespace detail {
  171. template <
  172. class T,
  173. size_t pathLength,
  174. class = typename std::enable_if<(pathLength > 0)>::type>
  175. struct NestedMapType {
  176. using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
  177. };
  178. template <class T>
  179. struct NestedMapType<T, 1> {
  180. using type = typename T::mapped_type;
  181. };
  182. template <typename... KeysDefault>
  183. struct DefaultType;
  184. template <typename Default>
  185. struct DefaultType<Default> {
  186. using type = Default;
  187. };
  188. template <typename Key, typename... KeysDefault>
  189. struct DefaultType<Key, KeysDefault...> {
  190. using type = typename DefaultType<KeysDefault...>::type;
  191. };
  192. template <class... KeysDefault>
  193. auto extract_default(const KeysDefault&... keysDefault) ->
  194. typename DefaultType<KeysDefault...>::type const& {
  195. return std::get<sizeof...(KeysDefault) - 1>(std::tie(keysDefault...));
  196. }
  197. } // namespace detail
  198. /**
  199. * Given a map of maps and a path of keys, return a Optional<V> if the nested
  200. * key exists and None if the nested keys does not exist in the map.
  201. */
  202. template <class Map, class Key1, class Key2, class... Keys>
  203. auto get_optional(
  204. const Map& map,
  205. const Key1& key1,
  206. const Key2& key2,
  207. const Keys&... keys)
  208. -> folly::Optional<
  209. typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type> {
  210. auto pos = map.find(key1);
  211. return pos != map.end() ? get_optional(pos->second, key2, keys...)
  212. : folly::none;
  213. }
  214. /**
  215. * Given a map of maps and a path of keys, return a pointer to the nested value,
  216. * or nullptr if the key doesn't exist in the map.
  217. */
  218. template <class Map, class Key1, class Key2, class... Keys>
  219. auto get_ptr(
  220. const Map& map,
  221. const Key1& key1,
  222. const Key2& key2,
  223. const Keys&... keys) ->
  224. typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type const* {
  225. auto pos = map.find(key1);
  226. return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
  227. }
  228. template <class Map, class Key1, class Key2, class... Keys>
  229. auto get_ptr(Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
  230. -> typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type* {
  231. auto pos = map.find(key1);
  232. return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
  233. }
  234. /**
  235. * Given a map and a path of keys, return the value corresponding to the nested
  236. * value, or a given default value if the path doesn't exist in the map.
  237. * The default value is the last parameter, and is copied when returned.
  238. */
  239. template <
  240. class Map,
  241. class Key1,
  242. class Key2,
  243. class... KeysDefault,
  244. typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type>
  245. auto get_default(
  246. const Map& map,
  247. const Key1& key1,
  248. const Key2& key2,
  249. const KeysDefault&... keysDefault) ->
  250. typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type {
  251. if (const auto* ptr = get_ptr(map, key1)) {
  252. return get_default(*ptr, key2, keysDefault...);
  253. }
  254. return detail::extract_default(keysDefault...);
  255. }
  256. /**
  257. * Given a map and a path of keys, return a reference to the value corresponding
  258. * to the nested value, or the given default reference if the path doesn't exist
  259. * in the map.
  260. * The default value is the last parameter, and must be a lvalue reference.
  261. */
  262. template <
  263. class Map,
  264. class Key1,
  265. class Key2,
  266. class... KeysDefault,
  267. typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type,
  268. typename = typename std::enable_if<std::is_lvalue_reference<
  269. typename detail::DefaultType<KeysDefault...>::type>::value>::type>
  270. auto get_ref_default(
  271. const Map& map,
  272. const Key1& key1,
  273. const Key2& key2,
  274. KeysDefault&&... keysDefault) ->
  275. typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type
  276. const& {
  277. if (const auto* ptr = get_ptr(map, key1)) {
  278. return get_ref_default(*ptr, key2, keysDefault...);
  279. }
  280. return detail::extract_default(keysDefault...);
  281. }
  282. } // namespace folly