Brad Bishop | 9cc42ab | 2018-12-12 16:36:51 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "types.hpp" |
| 4 | #include "utils.hpp" |
| 5 | |
| 6 | #include <any> |
| 7 | #include <map> |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <type_traits> |
| 11 | #include <utility> |
| 12 | |
| 13 | namespace sdbusplus |
| 14 | { |
| 15 | namespace bus |
| 16 | { |
| 17 | class bus; |
| 18 | } |
| 19 | } // namespace sdbusplus |
| 20 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace inventory |
| 24 | { |
| 25 | namespace manager |
| 26 | { |
| 27 | |
| 28 | template <typename T> |
| 29 | struct HasProperties |
| 30 | { |
| 31 | private: |
| 32 | using yes = char; |
| 33 | struct no |
| 34 | { |
| 35 | char array[2]; |
| 36 | }; |
| 37 | |
| 38 | template <typename U> |
| 39 | static constexpr yes test(typename U::PropertiesVariant*); |
| 40 | template <typename U> |
| 41 | static constexpr no test(...); |
| 42 | |
| 43 | public: |
| 44 | static constexpr auto value = sizeof(test<T>(0)) == sizeof(yes); |
| 45 | }; |
| 46 | |
| 47 | template <typename T, typename Enable = void> |
| 48 | struct MakeInterface |
| 49 | { |
| 50 | static std::any op(sdbusplus::bus::bus& bus, const char* path, |
| 51 | const Interface&) |
| 52 | { |
| 53 | return std::any(std::make_shared<T>(bus, path)); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | template <typename T> |
| 58 | struct MakeInterface<T, std::enable_if_t<HasProperties<T>::value>> |
| 59 | { |
| 60 | static std::any op(sdbusplus::bus::bus& bus, const char* path, |
| 61 | const Interface& props) |
| 62 | { |
| 63 | using InterfaceVariant = |
| 64 | std::map<std::string, typename T::PropertiesVariant>; |
| 65 | |
| 66 | InterfaceVariant v; |
| 67 | for (const auto& p : props) |
| 68 | { |
| 69 | v.emplace(p.first, |
| 70 | convertVariant<typename T::PropertiesVariant>(p.second)); |
| 71 | } |
| 72 | |
| 73 | return std::any(std::make_shared<T>(bus, path, v)); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | template <typename T, typename Enable = void> |
| 78 | struct AssignInterface |
| 79 | { |
| 80 | static void op(const Interface&, std::any&) |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame] | 81 | {} |
Brad Bishop | 9cc42ab | 2018-12-12 16:36:51 -0500 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | template <typename T> |
| 85 | struct AssignInterface<T, std::enable_if_t<HasProperties<T>::value>> |
| 86 | { |
| 87 | static void op(const Interface& props, std::any& holder) |
| 88 | { |
| 89 | auto& iface = *std::any_cast<std::shared_ptr<T>&>(holder); |
| 90 | for (const auto& p : props) |
| 91 | { |
| 92 | iface.setPropertyByName( |
| 93 | p.first, |
| 94 | convertVariant<typename T::PropertiesVariant>(p.second)); |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | template <typename T, typename Ops, typename Enable = void> |
| 100 | struct SerializeInterface |
| 101 | { |
| 102 | static void op(const std::string& path, const std::string& iface, |
| 103 | const std::any&) |
| 104 | { |
| 105 | Ops::serialize(path, iface); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | template <typename T, typename Ops> |
| 110 | struct SerializeInterface<T, Ops, std::enable_if_t<HasProperties<T>::value>> |
| 111 | { |
| 112 | static void op(const std::string& path, const std::string& iface, |
| 113 | const std::any& holder) |
| 114 | { |
| 115 | const auto& object = *std::any_cast<const std::shared_ptr<T>&>(holder); |
| 116 | Ops::serialize(path, iface, object); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | template <typename T, typename Ops, typename Enable = void> |
| 121 | struct DeserializeInterface |
| 122 | { |
| 123 | static void op(const std::string& path, const std::string& iface, std::any&) |
| 124 | { |
| 125 | Ops::deserialize(path, iface); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | template <typename T, typename Ops> |
| 130 | struct DeserializeInterface<T, Ops, std::enable_if_t<HasProperties<T>::value>> |
| 131 | { |
| 132 | static void op(const std::string& path, const std::string& iface, |
| 133 | std::any& holder) |
| 134 | { |
| 135 | auto& object = *std::any_cast<std::shared_ptr<T>&>(holder); |
| 136 | Ops::deserialize(path, iface, object); |
| 137 | } |
| 138 | }; |
| 139 | |
| 140 | struct DummyInterface |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame] | 141 | {}; |
Brad Bishop | 9cc42ab | 2018-12-12 16:36:51 -0500 | [diff] [blame] | 142 | using MakeInterfaceType = |
| 143 | std::add_pointer_t<decltype(MakeInterface<DummyInterface>::op)>; |
| 144 | using AssignInterfaceType = |
| 145 | std::add_pointer_t<decltype(AssignInterface<DummyInterface>::op)>; |
| 146 | template <typename Ops> |
| 147 | using SerializeInterfaceType = |
| 148 | std::add_pointer_t<decltype(SerializeInterface<DummyInterface, Ops>::op)>; |
| 149 | template <typename Ops> |
| 150 | using DeserializeInterfaceType = |
| 151 | std::add_pointer_t<decltype(DeserializeInterface<DummyInterface, Ops>::op)>; |
| 152 | |
| 153 | } // namespace manager |
| 154 | } // namespace inventory |
| 155 | } // namespace phosphor |