1 /* Copyright 2019 The TensorFlow 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 16 // Traits classes for performing uniform lookup on different map value types. 17 // 18 // The access is computed as follows: 19 // 20 // 1. If T has a `first` or `second` field, use them. 21 // 2. Otherwise if it has `key()` or `value()` methods, use them. 22 // 3. Otherwise the program is ill-formed. 23 #ifndef TENSORFLOW_CORE_LIB_GTL_SUBTLE_MAP_TRAITS_H_ 24 #define TENSORFLOW_CORE_LIB_GTL_SUBTLE_MAP_TRAITS_H_ 25 26 #include <utility> 27 28 namespace tensorflow { 29 namespace gtl { 30 namespace subtle { 31 namespace internal_map_traits { 32 struct Rank1 {}; 33 struct Rank0 : Rank1 {}; 34 35 template <class V> 36 auto GetKey(V&& v, Rank0) -> decltype((std::forward<V>(v).first)) { 37 return std::forward<V>(v).first; 38 } 39 template <class V> 40 auto GetKey(V&& v, Rank1) -> decltype(std::forward<V>(v).key()) { 41 return std::forward<V>(v).key(); 42 } 43 44 template <class V> 45 auto GetMapped(V&& v, Rank0) -> decltype((std::forward<V>(v).second)) { 46 return std::forward<V>(v).second; 47 } 48 template <class V> 49 auto GetMapped(V&& v, Rank1) -> decltype(std::forward<V>(v).value()) { 50 return std::forward<V>(v).value(); 51 } 52 53 } // namespace internal_map_traits 54 55 // Accesses the `key_type` from a `value_type`. 56 template <typename V> 57 auto GetKey(V&& v) 58 -> decltype(internal_map_traits::GetKey(std::forward<V>(v), 59 internal_map_traits::Rank0())) { 60 return internal_map_traits::GetKey(std::forward<V>(v), 61 internal_map_traits::Rank0()); 62 } 63 64 // Accesses the `mapped_type` from a `value_type`. 65 template <typename V> 66 auto GetMapped(V&& v) 67 -> decltype(internal_map_traits::GetMapped(std::forward<V>(v), 68 internal_map_traits::Rank0())) { 69 return internal_map_traits::GetMapped(std::forward<V>(v), 70 internal_map_traits::Rank0()); 71 } 72 73 } // namespace subtle 74 } // namespace gtl 75 } // namespace tensorflow 76 77 #endif // TENSORFLOW_CORE_LIB_GTL_SUBTLE_MAP_TRAITS_H_ 78