Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brad Bishop | d3d188d | 2018-12-11 16:49:29 -0500 | [diff] [blame] | 3 | #include <cstring> |
| 4 | #include <sdbusplus/message/types.hpp> |
| 5 | #include <stdexcept> |
| 6 | #include <string> |
| 7 | #include <type_traits> |
| 8 | #include <utility> |
| 9 | |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 10 | namespace phosphor |
| 11 | { |
| 12 | namespace inventory |
| 13 | { |
| 14 | namespace manager |
| 15 | { |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 16 | /** @struct MakeVariantVisitor |
| 17 | * @brief Return a variant if the visited type is a possible variant type. |
| 18 | * |
| 19 | * @tparam V - The desired variant type. |
| 20 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 21 | template <typename V> |
| 22 | struct MakeVariantVisitor |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 23 | { |
| 24 | /** @struct Make |
| 25 | * @brief Return variant visitor. |
| 26 | * |
| 27 | * @tparam T - The variant type to return. |
| 28 | * @tparam Arg - The type being visited in the source variant. |
| 29 | * @tparam Enable - Overload resolution removal. |
| 30 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 31 | template <typename T, typename Arg, typename Enable = void> |
| 32 | struct Make |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 33 | { |
| 34 | static auto make(Arg&& arg) |
| 35 | { |
William A. Kennington III | 6e94b65 | 2018-11-06 17:11:28 -0800 | [diff] [blame] | 36 | throw std::runtime_error( |
| 37 | "Invalid conversion in MakeVariantVisitor"); |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 38 | return T(); |
| 39 | } |
| 40 | }; |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 41 | |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 42 | /** @struct Make |
| 43 | * @brief Return variant visitor. |
| 44 | * |
| 45 | * struct Make specialization if Arg is in T (int -> variant<int, char>). |
| 46 | */ |
| 47 | template <typename T, typename Arg> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 48 | struct Make< |
| 49 | T, Arg, |
| 50 | typename std::enable_if<std::is_convertible<Arg, T>::value>::type> |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 51 | { |
| 52 | static auto make(Arg&& arg) |
| 53 | { |
| 54 | return T(std::forward<Arg>(arg)); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | /** @brief Make variant visitor. */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 59 | template <typename Arg> |
| 60 | auto operator()(Arg&& arg) const |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 61 | { |
| 62 | return Make<V, Arg>::make(arg); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | /** @brief Convert variants with different contained types. |
| 67 | * |
| 68 | * @tparam V - The desired variant type. |
| 69 | * @tparam Arg - The source variant type. |
| 70 | * |
| 71 | * @param[in] v - The source variant. |
| 72 | * @returns - The converted variant. |
| 73 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 74 | template <typename V, typename Arg> |
| 75 | auto convertVariant(Arg&& v) |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 76 | { |
Patrick Williams | 26f8668 | 2020-05-13 11:36:19 -0500 | [diff] [blame^] | 77 | return std::visit(MakeVariantVisitor<V>(), v); |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 78 | } |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 79 | |
| 80 | /** @struct CompareFirst |
| 81 | * @brief std::pair binary comparison adapter. |
| 82 | * |
| 83 | * Adapt a binary comparison function to a comparison of |
| 84 | * the first pair element. |
| 85 | * |
| 86 | * @tparam Compare - The function object type being adapted. |
| 87 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 88 | template <typename Compare> |
| 89 | struct CompareFirst |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 90 | { |
| 91 | /** @brief Construct a CompareFirst adapter. |
| 92 | * |
| 93 | * @param[in] c - The function object being adapted. |
| 94 | */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 95 | explicit CompareFirst(Compare&& c) : compare(std::forward<Compare>(c)) |
| 96 | { |
| 97 | } |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 98 | |
| 99 | /** @brief Compare two pairs adapter. |
| 100 | * |
| 101 | * @tparam L1 - First pair first_type. |
| 102 | * @tparam L2 - First pair second_type. |
| 103 | * @tparam R1 - Second pair first_type, convertible to L1. |
| 104 | * @tparam R2 - Second pair second_type. |
| 105 | * |
| 106 | * @param[in] l - The first pair. |
| 107 | * @param[in] r - The second pair. |
| 108 | * |
| 109 | * @returns - The result of the comparison. |
| 110 | */ |
| 111 | template <typename L1, typename L2, typename R1, typename R2> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 112 | bool operator()(const std::pair<L1, L2>& l, |
| 113 | const std::pair<R1, R2>& r) const |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 114 | { |
| 115 | return compare(l.first, r.first); |
| 116 | } |
| 117 | |
| 118 | /** @brief Compare one pair adapter. |
| 119 | * |
| 120 | * @tparam L1 - Pair first_type. |
| 121 | * @tparam L2 - Pair second_type. |
| 122 | * @tparam R - Convertible to L1 for comparison. |
| 123 | * |
| 124 | * @param[in] l - The pair. |
| 125 | * @param[in] r - To be compared to l.first. |
| 126 | * |
| 127 | * @returns - The result of the comparison. |
| 128 | */ |
| 129 | template <typename L1, typename L2, typename R> |
| 130 | bool operator()(const std::pair<L1, L2>& l, const R& r) const |
| 131 | { |
| 132 | return compare(l.first, r); |
| 133 | } |
| 134 | |
| 135 | /** @brief Compare one pair adapter. |
| 136 | * |
| 137 | * @tparam L - Convertible to R1 for comparison. |
| 138 | * @tparam R1 - Pair first_type. |
| 139 | * @tparam R2 - Pair second_type. |
| 140 | * |
| 141 | * @param[in] l - To be compared to r.first. |
| 142 | * @param[in] r - The pair. |
| 143 | * |
| 144 | * @returns - The result of the comparison. |
| 145 | */ |
| 146 | template <typename L, typename R1, typename R2> |
| 147 | bool operator()(const L& l, const std::pair<R1, R2>& r) const |
| 148 | { |
| 149 | return compare(l, r.first); |
| 150 | } |
| 151 | |
| 152 | /* @brief The function being adapted. */ |
| 153 | Compare compare; |
| 154 | }; |
| 155 | |
| 156 | /* @brief Implicit template instantation wrapper for CompareFirst. */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 157 | template <typename Compare> |
| 158 | CompareFirst<Compare> compareFirst(Compare&& c) |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 159 | { |
| 160 | return CompareFirst<Compare>(std::forward<Compare>(c)); |
| 161 | } |
| 162 | |
| 163 | /** @struct RelPathCompare |
| 164 | * @brief Compare two strings after removing an optional prefix. |
| 165 | */ |
| 166 | struct RelPathCompare |
| 167 | { |
| 168 | /** @brief Construct a RelPathCompare comparison functor. |
| 169 | * |
| 170 | * @param[in] p - The prefix to check for and remove. |
| 171 | */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 172 | explicit RelPathCompare(const char* p) : prefix(p) |
| 173 | { |
| 174 | } |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 175 | |
| 176 | /** @brief Check for the prefix and remove if found. |
| 177 | * |
| 178 | * @param[in] s - The string to check for and remove prefix from. |
| 179 | */ |
| 180 | auto relPath(const std::string& s) const |
| 181 | { |
| 182 | if (s.find(prefix) == 0) |
| 183 | { |
| 184 | return s.substr(strlen(prefix)); |
| 185 | } |
| 186 | |
| 187 | return s; |
| 188 | } |
| 189 | |
| 190 | /** @brief Comparison method. |
| 191 | * |
| 192 | * @param[in] l - The first string. |
| 193 | * @param[in] r - The second string. |
| 194 | * |
| 195 | * @returns - The result of the comparison. |
| 196 | */ |
| 197 | bool operator()(const std::string& l, const std::string& r) const |
| 198 | { |
| 199 | return relPath(l) < relPath(r); |
| 200 | } |
| 201 | |
| 202 | /* The path prefix to remove when comparing two paths. */ |
| 203 | const char* prefix; |
| 204 | }; |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 205 | } // namespace manager |
| 206 | } // namespace inventory |
| 207 | } // namespace phosphor |
| 208 | |
| 209 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |