Branch data Line data Source code
1 : : // Copyright 2017-2019 the Autoware Foundation 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 : : // Co-developed by Tier IV, Inc. and Apex.AI, Inc. 16 : : /// \file 17 : : /// \brief This file includes common helper functions 18 : : 19 : : #ifndef HELPER_FUNCTIONS__MESSAGE_ADAPTERS_HPP_ 20 : : #define HELPER_FUNCTIONS__MESSAGE_ADAPTERS_HPP_ 21 : : 22 : : #include <builtin_interfaces/msg/time.hpp> 23 : : #include <string> 24 : : 25 : : namespace autoware 26 : : { 27 : : namespace common 28 : : { 29 : : namespace helper_functions 30 : : { 31 : : namespace message_field_adapters 32 : : { 33 : : /// Using alias for Time message 34 : : using TimeStamp = builtin_interfaces::msg::Time; 35 : : 36 : : /// \brief Helper class to check existance of header file in compile time: 37 : : /// https://stackoverflow.com/a/16000226/2325407 38 : : template<typename T, typename = nullptr_t> 39 : : struct HasHeader : std::false_type {}; 40 : : 41 : : template<typename T> 42 : : struct HasHeader<T, decltype((void) T::header, nullptr)>: std::true_type {}; 43 : : 44 : : /////////// Template declarations 45 : : 46 : : /// Get frame id from message. nullptr_t is used to prevent template ambiguity on 47 : : /// SFINAE specializations. Provide a default value on specializations for a friendly API. 48 : : /// \tparam T Message type. 49 : : /// \param msg Message. 50 : : /// \return Frame id of the message. 51 : : template<typename T, nullptr_t> 52 : : const std::string & get_frame_id(const T & msg) noexcept; 53 : : 54 : : /// Get a reference to the frame id from message. nullptr_t is used to prevent 55 : : /// template ambiguity on SFINAE specializations. Provide a default value on 56 : : /// specializations for a friendly API. 57 : : /// \tparam T Message type. 58 : : /// \param msg Message. 59 : : /// \return Frame id of the message. 60 : : template<typename T, nullptr_t> 61 : : std::string & get_frame_id(T & msg) noexcept; 62 : : 63 : : /// Get stamp from message. nullptr_t is used to prevent template ambiguity on 64 : : /// SFINAE specializations. Provide a default value on specializations for a friendly API. 65 : : /// \tparam T Message type. 66 : : /// \param msg Message. 67 : : /// \return Frame id of the message. 68 : : template<typename T, nullptr_t> 69 : : const TimeStamp & get_stamp(const T & msg) noexcept; 70 : : 71 : : /// Get a reference to the stamp from message. nullptr_t is used to prevent 72 : : /// template ambiguity on SFINAE specializations. Provide a default value on 73 : : /// specializations for a friendly API. 74 : : /// \tparam T Message type. 75 : : /// \param msg Message. 76 : : /// \return Frame id of the message. 77 : : template<typename T, nullptr_t> 78 : : TimeStamp & get_stamp(T & msg) noexcept; 79 : : 80 : : 81 : : /////////////// Default specializations for message types that contain a header. 82 : : template<class T, typename std::enable_if<HasHeader<T>::value, nullptr_t>::type = nullptr> 83 : 1 : const std::string & get_frame_id(const T & msg) noexcept 84 : : { 85 : 1 : return msg.header.frame_id; 86 : : } 87 : : 88 : : template<class T, typename std::enable_if<HasHeader<T>::value, nullptr_t>::type = nullptr> 89 : 3 : std::string & get_frame_id(T & msg) noexcept 90 : : { 91 : 3 : return msg.header.frame_id; 92 : : } 93 : : 94 : : template<class T, typename std::enable_if<HasHeader<T>::value, nullptr_t>::type = nullptr> 95 : 3 : TimeStamp & get_stamp(T & msg) noexcept 96 : : { 97 : 3 : return msg.header.stamp; 98 : : } 99 : : 100 : : template<class T, typename std::enable_if<HasHeader<T>::value, nullptr_t>::type = nullptr> 101 : 1 : TimeStamp get_stamp(const T & msg) noexcept 102 : : { 103 : 1 : return msg.header.stamp; 104 : : } 105 : : 106 : : } // namespace message_field_adapters 107 : : } // namespace helper_functions 108 : : } // namespace common 109 : : } // namespace autoware 110 : : 111 : : #endif // HELPER_FUNCTIONS__MESSAGE_ADAPTERS_HPP_