Removed duplicated Holder adapters
Refactor copy/pasted action/filter/interface object
adapter types into a single templated framework.
Change-Id: Iafbd814572a7db13fddc5314617e310fe5f0a062
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/utils.hpp b/utils.hpp
new file mode 100644
index 0000000..2dbd3b6
--- /dev/null
+++ b/utils.hpp
@@ -0,0 +1,150 @@
+#pragma once
+
+namespace phosphor
+{
+namespace inventory
+{
+namespace manager
+{
+namespace details
+{
+namespace holder
+{
+
+/** @struct Base
+ * @brief Adapt from any type base class.
+ *
+ * Provides an un-templated base class for use with an adapt to any type
+ * adapter to enable containers of mixed types.
+ */
+struct Base
+{
+ Base() = default;
+ virtual ~Base() = default;
+ Base(const Base&) = delete;
+ Base& operator=(const Base&) = delete;
+ Base(Base&&) = default;
+ Base& operator=(Base&&) = default;
+};
+
+/** @struct Holder
+ * @brief Adapt from any type.
+ *
+ * Adapts any type to enable containers of mixed types.
+ *
+ * @tparam T - The adapted type.
+ */
+template <typename T>
+struct Holder : public Base
+{
+ Holder() = delete;
+ virtual ~Holder() = default;
+ Holder(const Holder&) = delete;
+ Holder & operator=(const Holder&) = delete;
+ Holder(Holder&&) = default;
+ Holder& operator=(Holder&&) = default;
+ explicit Holder(T&& held) : _held(std::forward<T>(held)) {}
+
+ /** @brief Construct an adapter.
+ *
+ * @param[in] held - The object to be adapted.
+ *
+ * @returns - std::unique pointer to the adapted object.
+ *
+ * @tparam Ret - The type of the pointer to be returned.
+ * @tparam Held - The type of the object to be adapted.
+ */
+ template <typename Ret, typename Held>
+ static auto make_unique(Held&& held)
+ {
+ return std::make_unique<Ret>(
+ std::forward<Held>(held));
+ }
+
+ /** @brief Construct an adapter.
+ *
+ * @param[in] held - The object to be adapted.
+ *
+ * @returns - std::shared pointer to the adapted object.
+ *
+ * @tparam Ret - The type of the pointer to be returned.
+ * @tparam Held - The type of the object to be adapted.
+ */
+ template <typename Ret, typename Held>
+ static auto make_shared(Held&& held)
+ {
+ return std::make_shared<Ret>(
+ std::forward<Held>(held));
+ }
+
+ protected:
+ T _held;
+};
+
+/** @struct CallableBase
+ * @brief Adapt any callable function object base class.
+ *
+ * Provides an un-templated base class for use with an adapt to any
+ * callable function object type.
+ *
+ * @tparam Ret - The return type of the callable.
+ * @tparam Args - The argument types of the callable.
+ */
+template <typename Ret, typename ...Args>
+struct CallableBase
+{
+ CallableBase() = default;
+ virtual ~CallableBase() = default;
+ CallableBase(const CallableBase&) = delete;
+ CallableBase& operator=(const CallableBase&) = delete;
+ CallableBase(CallableBase&&) = default;
+ CallableBase& operator=(CallableBase&&) = default;
+
+ virtual Ret operator()(Args&&...args) const = 0;
+ virtual Ret operator()(Args&&...args)
+ {
+ return const_cast<const CallableBase&>(*this)(
+ std::forward<Args>(args)...);
+ }
+};
+
+/** @struct CallableHolder
+ * @brief Adapt from any callable type.
+ *
+ * Adapts any callable type.
+ *
+ * @tparam T - The type of the callable.
+ * @tparam Ret - The return type of the callable.
+ * @tparam Args - The argument types of the callable.
+ */
+template <typename T, typename Ret, typename ...Args>
+struct CallableHolder final :
+ public CallableBase<Ret, Args...>,
+ public Holder<T>
+{
+ CallableHolder() = delete;
+ ~CallableHolder() = default;
+ CallableHolder(const CallableHolder&) = delete;
+ CallableHolder & operator=(const CallableHolder&) = delete;
+ CallableHolder(CallableHolder&&) = default;
+ CallableHolder& operator=(CallableHolder&&) = default;
+ explicit CallableHolder(T&& func) : Holder<T>(std::forward<T>(func)) {}
+
+ virtual Ret operator()(Args&&...args) const override
+ {
+ return this->_held(std::forward<Args>(args)...);
+ }
+
+ virtual Ret operator()(Args&&...args) override
+ {
+ return this->_held(std::forward<Args>(args)...);
+ }
+};
+
+} // namespace holder
+} // namespace details
+} // namespace manager
+} // namespace inventory
+} // namespace phosphor
+
+// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4