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 <sdbusplus/message/types.hpp> |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame^] | 4 | |
| 5 | #include <cstring> |
Brad Bishop | d3d188d | 2018-12-11 16:49:29 -0500 | [diff] [blame] | 6 | #include <stdexcept> |
| 7 | #include <string> |
| 8 | #include <type_traits> |
| 9 | #include <utility> |
| 10 | |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace inventory |
| 14 | { |
| 15 | namespace manager |
| 16 | { |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 17 | /** @struct MakeVariantVisitor |
| 18 | * @brief Return a variant if the visited type is a possible variant type. |
| 19 | * |
| 20 | * @tparam V - The desired variant type. |
| 21 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 22 | template <typename V> |
| 23 | struct MakeVariantVisitor |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 24 | { |
| 25 | /** @struct Make |
| 26 | * @brief Return variant visitor. |
| 27 | * |
| 28 | * @tparam T - The variant type to return. |
| 29 | * @tparam Arg - The type being visited in the source variant. |
| 30 | * @tparam Enable - Overload resolution removal. |
| 31 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 32 | template <typename T, typename Arg, typename Enable = void> |
| 33 | struct Make |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 34 | { |
| 35 | static auto make(Arg&& arg) |
| 36 | { |
William A. Kennington III | 6e94b65 | 2018-11-06 17:11:28 -0800 | [diff] [blame] | 37 | throw std::runtime_error( |
| 38 | "Invalid conversion in MakeVariantVisitor"); |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 39 | return T(); |
| 40 | } |
| 41 | }; |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 42 | |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 43 | /** @struct Make |
| 44 | * @brief Return variant visitor. |
| 45 | * |
| 46 | * struct Make specialization if Arg is in T (int -> variant<int, char>). |
| 47 | */ |
| 48 | template <typename T, typename Arg> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 49 | struct Make< |
| 50 | T, Arg, |
| 51 | typename std::enable_if<std::is_convertible<Arg, T>::value>::type> |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 52 | { |
| 53 | static auto make(Arg&& arg) |
| 54 | { |
| 55 | return T(std::forward<Arg>(arg)); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | /** @brief Make variant visitor. */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 60 | template <typename Arg> |
| 61 | auto operator()(Arg&& arg) const |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 62 | { |
| 63 | return Make<V, Arg>::make(arg); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | /** @brief Convert variants with different contained types. |
| 68 | * |
| 69 | * @tparam V - The desired variant type. |
| 70 | * @tparam Arg - The source variant type. |
| 71 | * |
| 72 | * @param[in] v - The source variant. |
| 73 | * @returns - The converted variant. |
| 74 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 75 | template <typename V, typename Arg> |
| 76 | auto convertVariant(Arg&& v) |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 77 | { |
Patrick Williams | 26f8668 | 2020-05-13 11:36:19 -0500 | [diff] [blame] | 78 | return std::visit(MakeVariantVisitor<V>(), v); |
Brad Bishop | 9bbfcb1 | 2017-02-04 10:51:06 -0500 | [diff] [blame] | 79 | } |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 80 | |
| 81 | /** @struct CompareFirst |
| 82 | * @brief std::pair binary comparison adapter. |
| 83 | * |
| 84 | * Adapt a binary comparison function to a comparison of |
| 85 | * the first pair element. |
| 86 | * |
| 87 | * @tparam Compare - The function object type being adapted. |
| 88 | */ |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 89 | template <typename Compare> |
| 90 | struct CompareFirst |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 91 | { |
| 92 | /** @brief Construct a CompareFirst adapter. |
| 93 | * |
| 94 | * @param[in] c - The function object being adapted. |
| 95 | */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 96 | explicit CompareFirst(Compare&& c) : compare(std::forward<Compare>(c)) |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame^] | 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) |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame^] | 173 | {} |
Brad Bishop | 8652158 | 2017-02-15 01:10:15 -0500 | [diff] [blame] | 174 | |
| 175 | /** @brief Check for the prefix and remove if found. |
| 176 | * |
| 177 | * @param[in] s - The string to check for and remove prefix from. |
| 178 | */ |
| 179 | auto relPath(const std::string& s) const |
| 180 | { |
| 181 | if (s.find(prefix) == 0) |
| 182 | { |
| 183 | return s.substr(strlen(prefix)); |
| 184 | } |
| 185 | |
| 186 | return s; |
| 187 | } |
| 188 | |
| 189 | /** @brief Comparison method. |
| 190 | * |
| 191 | * @param[in] l - The first string. |
| 192 | * @param[in] r - The second string. |
| 193 | * |
| 194 | * @returns - The result of the comparison. |
| 195 | */ |
| 196 | bool operator()(const std::string& l, const std::string& r) const |
| 197 | { |
| 198 | return relPath(l) < relPath(r); |
| 199 | } |
| 200 | |
| 201 | /* The path prefix to remove when comparing two paths. */ |
| 202 | const char* prefix; |
| 203 | }; |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 204 | } // namespace manager |
| 205 | } // namespace inventory |
| 206 | } // namespace phosphor |
| 207 | |
| 208 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |