summaryrefslogtreecommitdiffstats
path: root/utils.hpp
diff options
context:
space:
mode:
authorBrad Bishop <bradleyb@fuzziesquirrel.com>2017-02-04 10:51:06 -0500
committerBrad Bishop <bradleyb@fuzziesquirrel.com>2017-02-14 08:12:09 -0500
commit9bbfcb180ceba3c1af4ee03cc71e5c305c3a6b8b (patch)
tree93ae4cfed4b417117bc27f7c71c3cee505923dcc /utils.hpp
parent0237971c1e8cd102e2035f92f8e794b8e6711b2d (diff)
downloadphosphor-inventory-manager-9bbfcb180ceba3c1af4ee03cc71e5c305c3a6b8b.tar.gz
phosphor-inventory-manager-9bbfcb180ceba3c1af4ee03cc71e5c305c3a6b8b.zip
Set properties when constructing interfaces.
Make use of new sdbusplus support for passing a map of properties and their values to the interface constructor. Change-Id: Ib0dd406fd80c89acb723e3a376af26ba57b53d27 Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Diffstat (limited to 'utils.hpp')
-rw-r--r--utils.hpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/utils.hpp b/utils.hpp
index 33de439..60119be 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -6,7 +6,69 @@ namespace inventory
{
namespace manager
{
+/** @struct MakeVariantVisitor
+ * @brief Return a variant if the visited type is a possible variant type.
+ *
+ * @tparam V - The desired variant type.
+ */
+template <typename V>
+struct MakeVariantVisitor
+{
+ /** @struct Make
+ * @brief Return variant visitor.
+ *
+ * @tparam T - The variant type to return.
+ * @tparam Arg - The type being visited in the source variant.
+ * @tparam Enable - Overload resolution removal.
+ */
+ template <typename T, typename Arg, typename Enable = void>
+ struct Make
+ {
+ static auto make(Arg&& arg)
+ {
+ throw sdbusplus::message::variant_ns::bad_variant_access(
+ "in MakeVariantVisitor");
+ return T();
+ }
+ };
+
+ /** @struct Make
+ * @brief Return variant visitor.
+ *
+ * struct Make specialization if Arg is in T (int -> variant<int, char>).
+ */
+ template <typename T, typename Arg>
+ struct Make<T, Arg,
+ typename std::enable_if<std::is_convertible<Arg, T>::value>::type>
+ {
+ static auto make(Arg&& arg)
+ {
+ return T(std::forward<Arg>(arg));
+ }
+ };
+ /** @brief Make variant visitor. */
+ template <typename Arg>
+ auto operator()(Arg&& arg) const
+ {
+ return Make<V, Arg>::make(arg);
+ }
+};
+
+/** @brief Convert variants with different contained types.
+ *
+ * @tparam V - The desired variant type.
+ * @tparam Arg - The source variant type.
+ *
+ * @param[in] v - The source variant.
+ * @returns - The converted variant.
+ */
+template <typename V, typename Arg>
+auto convertVariant(Arg&& v)
+{
+ return sdbusplus::message::variant_ns::apply_visitor(
+ MakeVariantVisitor<V>(), v);
+}
} // namespace manager
} // namespace inventory
} // namespace phosphor
OpenPOWER on IntegriCloud